// Template Zusammenfassung
// (C) 2024, <NAME>, <NAME>
#import "helpers.typ": *

// Global variables
#let colors = (
  hellblau: rgb("#29769E"),
  dunkelblau: rgb("#1a4e69"),
  grün: rgb("#8B9654"),
  hellgrün: rgb("#BFBC8A"),
  gelb: rgb("#F2C12E"),
  rot: rgb("#A6460F"),
  orange: rgb("#D98825"),
  comment: rgb("#2D9428"),
)

#let languages = (
  de: (page: "Seite"),
  en: (page: "Page")
)

#let dateformat = "[day].[month].[year]"

// Main template
#let project(
  authors: (),
  fach: "",
  fach-long: "",
  semester: "",
  date: datetime.today(),
  landscape: false,
  column-count: 1,
  tableofcontents: (enabled: false, depth: "", columns: ""), // (depth: none, columns: 1)
  language: "de",
  font-size: 11pt,
  body,
) = {
  // == Document Configuration ==
  // PDF Metadata
  set document(
    author: authors,
    title: fach + " Zusammenfassung " + semester,
    date: date,
  )

  let font-default = (font: "Calibri", lang: language, region: "ch", size: font-size)

  let font-special = (
    ..font-default,
    font: "JetBrains Mono",
    weight: "bold",
    fill: colors.hellblau,
  )
  
  let footer = [
    #set text(font: font-special.font, size: 0.9em)
    #fach | #semester | #authors.join(" & ")
    #h(1fr)
    #languages.at(language).page #counter(page).display()
  ]

  set page(
    flipped: landscape,
    columns: column-count,
    footer: footer,
    margin: if (column-count < 2) {
      (top: 2cm, left: 1.5cm, right: 1.5cm, bottom: 2cm)
    } else {
      0.5cm
    }
  )

  set columns(column-count, gutter: 2em)

  // Default document font
  set text(..font-default)

  // Style built-in functions
  // Headings formatting
  set heading(numbering: "1.1.")
  show heading: hd => block({
    if hd.numbering != none and hd.level <= 3 {
      context counter(heading).display()
      h(1.3em)
    }
    hd.body
  })

  show heading.where(level: 1): h => {
    set text(..font-special, top-edge: 0.18em)
    line(length: 100%, stroke: 0.18em + colors.hellblau)
    upper(h)
    v(0.45em)
  }

  show heading.where(level: 2): h => {
    set text(size: 0.9em)
    upper(h)
  }

  // Remove space above H4, fixes spacing between H3 & H4
  show heading.where(level: 4): h => {
    v(-0.4em)
    h
  }

  // Table formatting
  set table(
    stroke: (x, y) => (left: if x > 0 { 0.07em }, top: if y > 0 { 0.07em }),
    inset: 0.5em,
  )

  // Recommended workaround in Typst 0.11 until table.header is styleable
  show table.cell.where(y: 0): emph

  // Unordered list, use with "- " or #list[]
  show list: set list(marker: "-", body-indent: 0.45em)

  // "Important" template, use with "_text_" or #emph[]
  show emph: set text(fill: font-special.fill, weight: font-special.weight)

  // Code, use with ```python print("Hello World")```
  show raw: set text(font: font-special.font, size: 1em)

  // Quotes
  set quote(block: true, quotes: true)
  show quote: q => {
    set align(left)
    set text(style: "italic")
    q
  }

  // Table of contents, header level 1
  show outline.entry.where(level: 1): entry => {
    v(1.1em, weak: true)
    strong(entry)
  }

  // Title page configuration
  let subtitle(subt) = [
    #set text(..font-special, size: 1.2em)
    #pad(bottom: 1.3em, subt)
  ]

  // == Page Content ==
  // title row
  align(left)[
    #text(..font-special, size: 1.8em, fach-long + " | " + fach)
    #v(1em, weak: true)
    #subtitle[Zusammenfassung]
  ]

  if (tableofcontents.enabled) {
    columns(tableofcontents.at("columns", default: 1),
      outline(depth: tableofcontents.at("depth", default: none)))
    pagebreak()
  }

  // Main body
  set par(justify: true)
  body
}

// Additional formatting templates
// "Zusätzlicher Hinweis"-Vorlage
#let hinweis(t) = {
  set text(style: "italic", size: 0.8em)
  show raw: set text(font: "JetBrains Mono", size: 1.05em)
  t
}

// "Definition"-Vorlage
#let definition(t) = {
  rect(stroke: 0.13em + colors.hellblau, inset: 0.73em, columns(1, t))
}

// Kommentar
#let comment(t) = {
  set text(style: "italic", weight: "bold", fill: colors.comment)
  t
}

// Text added by Jannis
#let jannis(t) = {
  set text(weight: "bold", fill: colors.orange)
  t
}

// Set a text color from the color dict for a math formula
#let fxcolor(subcolor, x) = {
  text(fill: colors.at(subcolor), $bold(#x)$)
}

#let tcolor(subcolor, x) = {
  text(fill: colors.at(subcolor), style: "italic", strong(x))
}
