// Basic template settings. Must be shown at very begining
#let init(doc) = {
  set page(
    paper: "a4",
    numbering: none,
    margin: (left: 0.25cm, right: 0.25cm, top: 0.5cm, bottom: 0.5cm),
    background: [
      #align(top + left, rect(width: 5.55cm, height: 100%, fill: blue.lighten(90%)))
      #v(-100%)
      #v(-1.3em)
      #align(top, rect(width: 100%, height: 4.4cm, fill: blue))
      #v(-5cm)
      #align(top + left, box(inset: (x: 0.8cm, y: 1cm), image("./assets/moi.png", height: 4cm)))
    ],
  )

  set text(font: "CMU Sans Serif")
  set par(leading: 0.4em)

  doc
}

// Template for the left column. Must be shown before filling the left column content.
#let leftColumn(doc) = {
  set text(size: 12pt)

  show link: content => {
    underline(content)
  }

  show heading.where(level: 1): it => {
    v(0.75cm)
    text(size: 16pt, weight: "bold", it.body)
    v(-0.5cm)
    line(length: 5cm)
  }

  doc
}

// Template for the rest of the CV. Must be shown after the left column is done.
#let content(doc) = {

  show link: content => {
    set text(fill: blue)
    underline(content)
  }

  show heading.where(level: 1): it => {
    set text(fill: blue)
    grid(
      gutter: 1em,
      columns: (5cm, 7fr),
      [], // align(horizon, [#v(3pt) #rect(width: 1cm, height: 0.33em, fill: blue)]),
      [
        #it.body
        #v(-0.5cm)
        #line(stroke: blue, length: 100%)
      ],
    )
  }

  doc
}

// Print the header of the CV.
#let header(name: content, description: content) = {
  v(1cm)
  grid(
    columns: (6cm, 1fr),

    [],

    [
      #v(-0.25cm)
      #set text(size: 40pt, fill: white)
      Thomas Forgione \
      #set text(size: 18pt, style: "italic")
      Docteur en informatique et télécommunications
    ],
  )
}


// A simple entry in the CV.
#let entry(body, left: content) = {
  grid(
    gutter: 1em,
    columns: (5.5cm, 0.75fr, 7fr),
    [],
    align(right, left),
    body,
  )
  v(-4pt)
}

// An entry in the CV with "until" date, heading, description and note.
#let entry2(left: content, heading: content, description: content, note: content) = {

  let content = [
      #heading \
      #set text(size: 12pt, style: "italic")
      #description \
      #set text(size: 11pt, style: "normal")
      #note
    ]

    style(styles => {
      let size = measure(content, styles)
      let offset = measure(left, styles)
      grid(
        gutter: 1em,
        columns: (5.5cm, 0.75fr, 7fr),
        [],
        align(center, [
          #left
          #v(-0.5em)
          #line(angle: 90deg, length: size.height - offset.height - 0.5em)
        ]),
        content
      )
      v(-4pt)

  })
}

// A score box between 1 and 5.
#let score(level: int) = {

  let elems = ()

  for x in range(5) {
    if level > x {
      elems.push(rect(width: 0.4cm, height: 0.2cm, fill: blue))
    } else {
      elems.push(rect(width: 0.4cm, height: 0.2cm, fill: blue.lighten(60%)))
    }
  }

  v(0.1cm)
  stack(
    dir: ltr,
    elems.at(0), h(0.1cm),
    elems.at(1), h(0.1cm),
    elems.at(2), h(0.1cm),
    elems.at(3), h(0.1cm),
    elems.at(4),
  )
}
