#import "lib.typ": *

// TODO: make it _return_ the data, not display it on its own
#let by_client(experience: ()) = {
  let by_client = (:)

  for item in experience {
    let client = item.place.at(lang)
    if client not in by_client {
      by_client.insert(client, ())
    }
    by_client.at(client).push((item.title.at(lang), item.date.at(lang)))
  }

  for (client, jobs) in by_client {
    [*#client*:]
    for j in jobs {
      [- #j.at(0) #h(1fr) #j.at(1)]
    }
  }
}

#let by_experience_type(type: (), experience: ()) = {
  let by_ty = (:)
  for (id, desc) in type {
    let matching_exp_items = experience.filter(item => int(item.typeid) == int(id))
    if matching_exp_items.len() == 0 {
      return
    }

    [=== _#desc.at(lang)_]
    by_client(experience: matching_exp_items)
  }
}

#let resume(contents, main: ("experience", "education"), sidebar: ("volunteering", "languages", "skills")) = {
  show: style
  show: smartypants
  set text(lang: lang)

  let date_formatting = {
    if lang == "de" {
      "[day]. [month repr:long] [year]"
    } else {
      "[month repr:long] [day], [year]"
    }
  }
  set page(
    paper: "a4",
    margin: (x: 0.9cm, y: 1.3cm),
    footer: [
      #set text(
        fill: luma(200),
        size: 8pt,
      )
      #_columns_3[
        #smallcaps[#datetime.today().display(date_formatting)]
      ][
        #smallcaps[#contents.about.fullname]
      ][
        #context counter(page).display()
      ]
    ],
  )

  set par(justify: true)

  header(contents.about)

  let body = {
    if "summary" in main and "summary" in contents {
      section(
        title: "",
        {
          contents.summary.at(lang)
        },
      )
    }

    if "experience" in main and "experience" in contents {
      let title = (en: "Professional Experience", de: "Berufserfahrung").at(lang)
      section(title: title)[]
      by_experience_type(experience: contents.experience, type: contents.experience_types)
    }

    if "education" in main and "education" in contents {
      let title = (en: "Education", de: "Ausbildung").at(lang)
      section(title: title, entries: contents.thesis + contents.education)[]
    }

    if "volunteering" in main and "volunteering" in contents {
      let title = (en: "Volunteer Work", de: "Ehrenamt").at(lang)
      section(title: title, entries: contents.volunteering)[]
    }

    if "skills" in main and "skills" in contents {
      let title = (en: "Qualifications", de: "Qualifikationen").at(lang)
      section(
        title: title,
        {
          skill_item(item: contents.skills)
        },
      )
    }

    if "languages" in main and "languages" in contents {
      let title = (en: "Languages", de: "Sprachen").at(lang)
      section(
        title: title,
        {
          skill_item(item: contents.languages)
        },
      )
    }
  }

  let sidebar = {
    if "volunteering" in sidebar and "volunteering" in contents {
      let title = (en: "Volunteer Work", de: "Ehrenamt").at(lang)
      [== #title]
      for e in contents.at("volunteering") {
        [
          - *#e.title.at(lang)* (#e.date.at(lang))
            #par(e.bullets.at(0).at(lang)) \
        ]
      }
    }

    if "languages" in sidebar and "languages" in contents {
      let title = (en: "Languages", de: "Sprachen").at(lang)
      [== #title]
      skill_item(item: contents.languages, is_sidebar: true)
      [\ ]
    }

    if "skills" in sidebar and "skills" in contents {
      let title = (en: "Qualifications", de: "Kenntnisse").at(lang)
      [== #title]
      skill_item(item: contents.skills, is_sidebar: true)
    }
  }

  let margin = 1pt
  grid(
    columns: (2fr, 1fr),
    block(
      outset: 0pt,
      inset: (top: 0.4 * margin, right: 0pt, rest: margin),
      stroke: none,
      width: 100%,
      {
        set block(above: 10pt)
        show heading.where(level: 1): it => style(s => {
          let h = text(size: 18pt, upper(it))
          let dim = measure(h, s)
          stack(
            dir: ltr,
            h,
            place(
              dy: 7pt,
              dx: 10pt,
              horizon + left,
              line(stroke: accent-color, length: 100% - dim.width - 10pt),
            ),
          )
        })
        body
      },
    ),
    align(
      right,
      block(
        fill: luma(250),
        width: 90%,
        {
          v(15pt)
          set block(inset: (left: 5 * margin, right: 5 * margin))
          show heading: it => align(right, upper(it))
          set list(marker: "")
          show list: it => {
            set par(justify: false)
            align(right, block(it))
          }
          sidebar
          v(15pt)
        },
      ),
    ),
  )
}

#resume(yaml("content.yml"))