
#let assets-folder = "../assets/"
#import "etablissements.typ": etablissements-data

#let cover(
  title-en: "",
  title-fr: "",
  author: "",
  affiliation: "",
  defense-place: "",
  defense-date: "",
  jury-content: [],
  university: "UR",
  speciality: "Informatique",
  heading-font: "TeX Gyre Heros",
  preliminary: false,
) = {
  set page(
    margin: (left: 20mm, right: 20mm, top: 30mm, bottom: 30mm),
    header: none,
    numbering: none,
    background: image(assets-folder + "cover-bg.svg")
  )
  set text(font: heading-font, fill: black)

  place(dx: 110mm, dy: -15mm, etablissements-data.at(university).logo)
  place(dx: 0mm, dy: -15mm, image(assets-folder + "logo.png", width: 7.5cm))

  // Add a blue background with the width of the page
  context {
    //let y-start = locate(<cover:title-en>).position().y - 1cm
    let y-start = 14.5cm // should match background, more or less
    let y-end = locate(<cover:defense-info>).position().y + measure(query(<cover:defense-info>).first()).height + .5cm
    let height = 5em

    place(
      top + left, float: false,
      dy: y-start - page.margin.top,
      dx: -page.margin.left,
      block(
        width: page.width,
        height: y-end - y-start,
        fill: blue
      )
    )
  }

  v(2.1cm)
  text(size: 2em, smallcaps[Thèse de doctorat de])
  v(2.25cm)

  set text(fill: white)
  text(size: 1.5em, smallcaps(etablissements-data.at(university).name))
  v(.01cm)
  text(size: 1.2em)[
    #smallcaps[École Doctorale N° 601] \
    _Mathématiques, Télécommunications, Informatique, \
    Signal, Systèmes, Électronique_ \
    Spécialité : _ #speciality _ \
    #v(.1cm) #h(.6cm) Par \
  ]


  // Author name
  v(0em)
  h(.6cm)
  text(size: 1.9em)[*#author* \ ]
  v(.1cm)

  // Title + defense info block
  text(size: 1.6em)[*#title-en* <cover:title-en>]
  parbreak()
  text(size: 1.4em, title-fr)
  parbreak()

  text(size: 1.1em)[
    *Thèse présentée et soutenue à #defense-place, le #defense-date* \
    *Unité de recherche : #affiliation*
    <cover:defense-info>
  ]

  v(1em)

  set text(fill: black)
  jury-content

  if preliminary {
    place(center + horizon, rotate(24deg, text(red.transparentize(0%), size: 60pt)[VERSION \ PRELIMINAIRE]))
  }
  pagebreak()
  set page(background: none)
}
