#let dmy-format = "[day] [month repr:long] [year]"

/// Create a cover page at the *very* beginning of document.  Has to be a show
/// rule since we want to go before preambles generated by the body template.
/// - doc-type (str):           Type of document; must be one of `masterthesis`,
///                             `bachelorthesis`, `semesterproject`, `techreport`,
///                             or `doctoralplan`.
/// - number (str, none):       Number of the document you get from the admins.  Not used when `doc-type` is `doctoralplan` or `semesterproject`.
/// - title (str):              Title of the document.
/// - author (str):             Name of the main author(s).
/// - authorinfo (content, none):Extra information of the author.  Only used when `doc-type` is `doctoralplan`.
/// - collaborator (str, none): Name of the collaborator(s), if any.
/// - collaborator-logo (content, none): Logo of the collaborator, if any
/// - supervisor (str, none):   Name of the supervisor(s), if any.
/// - second-advisor (str, none): Name of the second advisor.  Only used when `doc-type` is `doctoralplan`.
/// - date (datetime):          Date of the document.
/// - doc (content):            Body of the entire document.
#let cover-page(
  doc-type: "masterthesis",
  number: "nr undefined",
  title: "title undefined",
  author: "author undefined",
  authorinfo: none,
  admission-date: datetime.today(),
  contract-date: datetime.today(),
  additional-info: none,
  collaborator: none,
  collaborator-logo: none,
  supervisor: none,
  second-advisor: none,
  date: datetime.today().display(dmy-format),
  doc,
) = {
  let type-to-display = (
    masterthesis: [Master's Thesis],
    bachelorthesis: [Bachelor's Thesis],
    semesterproject: [Semester Project],
    techreport: [Technical Report],
    doctoralplan: [Doctoral Plan],
  )

  assert(type-to-display.keys().contains(doc-type), message: "doc-type must be
    one of: " + str(type-to-display.keys().join(", ")))

  set page(
    paper: "a4",
    margin: (x: 1.7in/2, y: 2.5in/2),
  )

  grid(
    columns: (auto, 1fr, auto),
    align: top,
    image("logos/eth.svg", height: 18mm),
    [],
    image("logos/systems.svg", height: 16mm),
  )

  v(1fr)

  let is-doctoralplan = doc-type == "doctoralplan"
  let is-semesterproject = doc-type == "semesterproject"

  let type-font(body) = if (is-doctoralplan) {
    text(size: 12pt)[#body #parbreak()]
  } else {
    text(size: 16.84pt, weight: "bold")[#body #parbreak()]
  }
  let group-font(body) = if (is-doctoralplan) {
    text(size: 12pt)[#body #parbreak()]
  } else {
    text(size: 12.63pt)[#body #parbreak()]
  }
  let incollaboration-font(body) = if (is-doctoralplan) {
    text(size: 12pt)[#body #parbreak()]
  } else {
    text(size: 10.52pt)[#body #parbreak()]
  }
  let title-font(body) = if (is-doctoralplan) {
    text(size: 18pt)[#body #parbreak()]
  } else {
    text(size: 12.63pt)[#body #parbreak()]
  }
  let author-font(body) = if (is-doctoralplan) {
    text(size: 12pt)[#body #parbreak()]
  } else {
    text(size: 12.63pt)[#body #parbreak()]
  }
  let date-font(body) = if (is-doctoralplan) {
    text(size: 12pt)[#body #parbreak()]
  } else {
    text(size: 12.63pt)[#body #parbreak()]
  }

  align(center, {
    set text(font: "Arial")
    show par: set block(spacing: 0.3em)

    type-font({
      type-to-display.at(doc-type)
      if (not is-doctoralplan and not is-semesterproject) [~Nr.~#number]
    })
    v(5mm)

    group-font[Systems Group, Department of Computer Science, ETH Zurich]
    if (collaborator != none) {
      v(4mm)
      incollaboration-font[in collaboration with]

      v(3mm)
      group-font(collaborator)
    }
    v(16mm)

    title-font(title)
    v(20mm)

    author-font({
      [by]
      v(3mm)
      author
      if (is-doctoralplan) {
        v(0.2em)
        authorinfo
      }

      if (is-doctoralplan) {
        v(8mm)
        [Supervisor: #supervisor]
        v(3mm)
        [Second Advisor: #second-advisor]
      } else if (supervisor != none) {
        v(8mm)
        [Supervised by]
        v(3mm)
        (supervisor)
      }
    })

    v(15mm)
    date-font({
      date

      if (is-doctoralplan) {
        set align(left)
        set text(style: "italic")
        v(30mm)
      [
        #author is employed as a research assistant since
        #contract-date.display(dmy-format) and was accepted as a PhD student on
        #admission-date.display(dmy-format). #additional-info
      ]
      }
    })
  })

  v(1fr)

  grid(
    columns: (auto, 1fr, auto),
    align: top,
    image("logos/dinfk.svg", height: 6.5mm),
    [],
    collaborator-logo,
  )

  doc
}
