#import "@preview/touying:0.6.1": *

#let slide(title: auto, ..args) = touying-slide-wrapper(self => {
  let info = self.info + args.named()
  if title != auto {
    self.store.title = title
  }
  let header(self) = {
    set align(top)
    set text(fill: rgb("0065BD"), size: 25pt, weight: "bold")
    place(top + right,
      dx: -1em,
      dy: 1em,
      image("./resources/TUM_Logo_blau.svg", width: 7%)
    )
    v(1.5em)
    h(1em)
    utils.display-current-heading(level: 1)
    v(-1.6em)
    linebreak()
    h(1em)
    set text(fill: rgb("005293"), size: 20pt)
    if self.store.title != none {
      utils.call-or-display(self, self.store.title)
    } else {
      utils.display-current-heading(level: 2)
    }
  }
  let footer(self) = {
    set align(bottom)
    show: pad.with(.4em)
    set text(size: 14pt)
    h(1em)
    h(1fr)
    grid(
      columns: (1fr, 4fr, 1fr, 1fr), 
      align: center,
      grid.cell(align: left)[
        #h(1em)
        #info.footer-author],
      info.title,
      utils.display-info-date(self),
      grid.cell(align: right)[
        #context [#utils.slide-counter.display()/#utils.last-slide-number] #h(1em)]
    )
  }
  self = utils.merge-dicts(
    self,
    config-page(
      header: header,
      footer: footer,
    ),
  )
  touying-slide(self: self, ..args)
})

#let title-slide(..args) = touying-slide-wrapper(self => {
  let info = self.info + args.named()
  let header = {
    set text(fill: rgb("0065BD"), size: 25pt, weight: "bold")
    place(top + right,
      dx: -1em,
      dy: 1em,
      image("./resources/TUM_Logo_blau.svg", width: 7%)
    )
    place(top + left,
      dy: 3em,
      dx: 0.9em,
      grid(
          columns: (3fr, 1fr),
          [
            #set text(fill: rgb("0065BD"), size: 32pt, weight: "bold")
            #info.title
          ]
        )
    )
  }
  let body = {
    set text(weight: "bold")
    v(5em)
    info.author

    v(2em)
    set text(weight: "regular")
    utils.display-info-date(self)

    place(bottom + right,
      dx: 2em, 
      dy: 2em,
      image("./resources/TUM_Tower.png", width: 50%)
    )
  }
  self = utils.merge-dicts(
    self, 
    config-page(
      header: header,
    ),
  )
  touying-slide(self: self, body)
})

#let outline-slide(..args) = touying-slide-wrapper(self => {
  let header = {
    set text(fill: rgb("0065BD"), size: 25pt, weight: "bold")
    place(top + right,
      dx: -1em,
      dy: 1em,
      image("./resources/TUM_Logo_blau.svg", width: 7%)
    )
    place(top + left,
      dy: 3em,
      dx: 0.9em,
      grid(
          columns: (3fr, 1fr),
          [
            #set text(fill: rgb("0065BD"), size: 25pt, weight: "bold")
            Inhalt
          ]
        )
    )
  }
  let body = {
    
    show outline.entry: it => link(
      it.element.location(),
      [
        #it.body() #linebreak() #v(1em)
      ]
    )
    align(horizon)[
      #outline(title: none, depth: 1)
      //#utils.slide-counter.update(0)
    ]
  }
  self = utils.merge-dicts(
    self, 
    config-page(
      header: header,
    ),
  )
  touying-slide(self: self, body)
})

#let ending-slide(..args) = touying-slide-wrapper(self => {
  let info = self.info + args.named()
  let header = {
    set text(fill: rgb("0065BD"), size: 25pt, weight: "bold")
    place(top + right,
      dx: -1em,
      dy: 1em,
      image("./resources/TUM_Logo_blau.svg", width: 7%)
    )
  }
  let footer(self) = {
    set align(bottom)
    show: pad.with(.4em)
    set text(size: 14pt)
    h(1em)
    h(1fr)
    grid(
      columns: (1fr, 4fr, 1fr, 1fr), 
      align: center,
      grid.cell(align: left)[
        #h(1em)
        #info.footer-author],
      info.title,
      utils.display-info-date(self),
      grid.cell(align: right)[
        #context [#utils.slide-counter.display()/#utils.last-slide-number] #h(1em)]
    )
  }
  let body = {
    align(center)[
      #v(3em)
      Fragen, Anmerkungen? 
      #v(3em)
      *Danke für Ihre Aufmerksamkeit!*
    ]
  }
  self = utils.merge-dicts(
    self, 
    config-page(
      header: header,
      footer: footer,
    ),
  )
  touying-slide(self: self, body)
})



#let tum-slides(
  aspect-ratio: "16-9",
  footer: none,
  ..args,
  language: "en",
  body,
) = {
  set text(size: 20pt, font: "TUM Neue Helvetica", lang: language)
  set par(spacing: 0.6em)

  show: touying-slides.with(
    config-page(
      paper: "presentation-" + aspect-ratio,
      margin: (top: 5.5em, left: 1.2em),
    ),
    config-common(
      slide-fn: slide,
      datetime-format: "[day].[month].[year]"
    ),
    config-store(
      title: none,
      footer: footer,
    ),
    ..args,
  )

  

  title-slide()

  outline-slide()
  
  body

  ending-slide()
}

