#import "fontawesome.typ": github, fa, link-icon

#let DEFAULT_STRONG = 300;
#let SMALL_STRONG = 300;

#let TITLE_FONT = "Bitter"
#let HEADING_FONT = TITLE_FONT
#let BODY_FONT = "Raleway"

#let BODY_FONT_SIZE = 10pt
#let HEADING_FONT_SIZE = 0.95em
#let TITLE_FONT_SIZE = 2em

#let HEADING_LINE_GAP = 3mm
#let HEADING_ABOVE_GAP = 1.2em
#let HEADING_BELOW_GAP = 0.75em

#let BODY_LINE_HEIGHT = 0.7em

#let HEADING_DETAILS_LIST_SPACING = 0.9em

#let DETAILS_LIST_SPACING = 0.7em
#let DETAILS_LIST_INDENT = 1.25em

#let JOB_TITLE_FONT_SIZE = 1.05em

// rest = not top
#let PAGE_MARGINS = (right: 0.5in, top: 0.3in, left: 0.4in, bottom: 0.4in)

// Format locations.
#let format_location(location) = {
  [_#block(above: 0.7em, location)_]
}

#let indent(content) = {
  block(inset: (left: 0.75em, right: 0.5em), content)
}

// General entry that is split into a left and right half (for experience and education).
#let cv_entry(left_content: none, right_content: none, details: none) = {
  stack(
    dir: ttb,
    spacing: HEADING_DETAILS_LIST_SPACING,
    grid(columns: (15fr, 5fr), column-gutter: 0cm, {
      set strong(delta: DEFAULT_STRONG)
      set align(left)
      left_content
    }, {
      set strong(delta: SMALL_STRONG)
      set align(right)
      right_content
    }),
    {
      set strong(delta: SMALL_STRONG)
      show link: underline
      list(..details)
    },
  )
}

// Entry for work.
#let work_entry(
  role,
  company,
  tools: none,
  tasks: none,
  start_date: none,
  end_date: none,
  location: none,
  company_link: none,
) = {
  cv_entry(left_content: {
    text(JOB_TITLE_FONT_SIZE)[*#role*]

    if tools != none {
      // text[ | _ #tools _ ]
    }

    "\n"
    set strong(delta: SMALL_STRONG)

    if company_link == none {
      [_#company _]
    } else {
      [#link(company_link)[_#company _]]
    }
  }, right_content: {
    text(style: "italic")[#start_date -- #end_date]

    if location != none {
      "\n" + format_location(location)
    }
  }, details: tasks)
}

#let project(title, tools, repo_link: none, demo_link: none, tasks: none) = {
  set strong(delta: DEFAULT_STRONG)
  let text_link = if demo_link != none { demo_link } else { repo_link }

  let content_title = {
    if text_link != none {
      link(text_link)[#text(JOB_TITLE_FONT_SIZE)[*#title*]]
    } else {
      text(JOB_TITLE_FONT_SIZE)[*#title*]
    }

    if demo_link != none {
      link(demo_link)[#text(JOB_TITLE_FONT_SIZE)[ #fa(link-icon) ]]
    }

    if repo_link != none {
      link(repo_link)[#text(JOB_TITLE_FONT_SIZE)[ #fa(github) ]]
    }
    [ | _ #tools _ ]
  }

  cv_entry(left_content: content_title, details: tasks)
}

// Set name and contact data and format headings
#let template(name, color, doc) = {
  set page(margin: PAGE_MARGINS, paper: "us-letter")
  set list(
    tight: false,
    indent: DETAILS_LIST_INDENT,
    spacing: DETAILS_LIST_SPACING,
    marker: [*•*],
  )
  set text(font: (BODY_FONT), BODY_FONT_SIZE)
  set par(justify: true, leading: BODY_LINE_HEIGHT)
  set underline(offset: 0.2em)
  align(center)[
    #text(size: TITLE_FONT_SIZE, font: TITLE_FONT, fill: color)[*#name*]
    #block(above: 0em, below: 1em)
  ]

  show heading.where(level: 1): i => {
    set align(left + horizon)
    let title = smallcaps(i.body)

    let colored_line(header_text) = {
      let size = measure(header_text)

      // 100% - spacing - width of text
      line(
        length: 100% - HEADING_LINE_GAP - size.width,
        stroke: (paint: color, thickness: 2pt, cap: "round"),
      )
    }

    set block(above: HEADING_ABOVE_GAP, below: HEADING_BELOW_GAP)
    set text(size: HEADING_FONT_SIZE, fill: color, font: HEADING_FONT)
    stack(
      dir: ltr,
      spacing: HEADING_LINE_GAP,
      title,
      box(height: 2pt, fill: color, colored_line(title)),
    )
  }

  set strong(delta: SMALL_STRONG)
  doc
}

#let render_contact_data(data) = {
  align(center)[
    #let elements = for el in data {
      (link(el.link)[#{ el.service + " " + el.display }],)
    }
    #text(1em)[#elements.join(" | ")]
  ]
}