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

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

#let TITLE_FONT = "Bitter"
#let HEADING_FONT = TITLE_FONT
#let BODY_FONT = "Open Sans"

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

#let HEADING_LINE_GAP = 3mm
#let BODY_LINE_HEIGHT = 0.75em

#let HEADING_DETAILS_LIST_SPACING = 0.9em
#let DETAILS_TITLE_FONT_SIZE = 1.075em

#let DETAILS_LIST_SPACING = 0.85em

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

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

#let indent(content) = {
  block(inset: (left: 0.75em, right: 1em), 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, 4fr), 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: DEFAULT_STRONG)
      list(tight: false, spacing: DETAILS_LIST_SPACING, marker: [•], ..details)
    },
  )
}

// Entry for work.
#let work_entry(
  role,
  company,
  tools: none,
  tasks: none,
  start_date: none,
  end_date: none,
  location: none,
) = {
  cv_entry(left_content: {
    set par(justify: true)
    text(DETAILS_TITLE_FONT_SIZE)[*#company* | _#role _]

    if tools != none {
      set strong(delta: SMALL_STRONG)
      text[ | *#tools* ]
    }
  }, right_content: {
    [_#start_date -- #end_date _]
    // if location != none {
    // format_location(location)
    // }
  }, details: tasks)
}

#let project(
  title,
  tools,
  start_date: none,
  end_date: none,
  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 date_content = if start_date != none {
    [_#start_date -- #end_date _]
  } else {
    none
  }

  let content_title = {
    set text(size: DETAILS_TITLE_FONT_SIZE)
    if text_link != none {
      link(text_link)[#text[*#title*]]
    } else {
      text[*#title*]
    }

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

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

  cv_entry(
    left_content: content_title,
    right_content: date_content,
    details: tasks,
  )
}

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

// Set name and contact data and format headings
#let template(name, color, doc) = {
  set page(margin: PAGE_MARGINS, paper: "us-letter")
  set list(indent: 0.75em, marker: [•])
  set text(font: (BODY_FONT), BODY_FONT_SIZE)
  set par(leading: BODY_LINE_HEIGHT)
  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) = style(styles => {
      let size = measure(header_text, styles)

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

    set block(above: 0.9em, below: 0.75em)
    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)),
    )
  }
  doc
}