#import "@preview/cetz:0.5.1"

#let time(h,m) = datetime(hour:h, minute: m, second: 0)

#let parse_time(s) = {
  let (h, m) = s.match(regex("(\d{2}):(\d{2})")).captures.map(int)
  time(h,m)
}

#let gety(s) = {
  let dt = parse_time(s)
  11-(dt - time(8,0)).hours()
}

#let sans_font = "Fira Sans"


#let titre = "EDT 2026/2027 -- M1 S1"

#let data = json(bytes(sys.inputs.data))

#let cours = data.cours
/*
Un exemple :

#let cours = (
  (day: 0, start: "08:15", end: "10:15", name:"Algèbre (CM)", prof:"Jean-Benoît Bost", room: "OA1", color: "#ff80b3"),
  (day: 0, start: "10:30", end: "13:00", name:"Algèbre (TD)", prof:"x", room: "OA1", color: "#ff80b3"),
  (day: 3, start: "14:00", end: "16:00", name:"Algèbre (CM)", prof:"Jean-Benoît Bost", room: "OA1", color: "#ff80b3"),
  (day: 3, start: "16:15", end: "18:45", name:"Algèbre (TD)", prof:"x", room: "OA1", color: "#ff80b3"),
  
  (day: 0, start: "14:00", end: "16:00", name:"Analyse (CM)", prof:"Blanche Buet", room: "OA1", color: "#80ffcc"),
  (day: 0, start: "16:15", end: "18:45", name:"Analyse (TD)", prof:"x", room: "OA1", color: "#80ffcc"),
  (day: 2, start: "08:15", end: "10:15", name:"Analyse (CM)", prof:"Blanche Buet", room: "OA1", color: "#80ffcc"),
  (day: 2, start: "10:30", end: "13:00", name:"Analyse (TD)", prof:"x", room: "OA1", color: "#80ffcc"),

  (day: 1, start: "14:00", end: "16:00", name:"Cryptographie (CM)", prof:"Stéphane Fischler", room: "OA1", color: "#e6b3ff"),
  (day: 4, start: "10:30", end: "12:30", name:"Cryptographie (TP)", prof:"Chimène Fischler", room: "OA1", color: "#e6b3ff")
)
*/

#set page(
  paper: "a4",
  flipped: true, 
  footer: [
    #set align(right)
    #set text(font: sans_font, size: 9pt)
    $copyright$ EDT Valentin Moguérou 2026
  ],
  margin: (
    x: 1.5cm,
    top: 1cm,
    bottom: 1cm,
  )
)
#set par(justify: true)
#set text(
  font: "New Computer Modern",
  size: 11pt,
)

#set align(center)

#let jours = ("Lundi","Mardi","Mercredi","Jeudi","Vendredi")

#align(center, text(titre, size: 15pt, weight: "bold"))

#cetz.canvas(
  length: 1.5cm,
{
  import cetz.draw: *

  for heure in range(8,20) {
    content((-.5,18.85-heure),
        text([#heure:00], font: sans_font, size: 9pt))

    line((-0.5,19-heure),(0,19-heure), stroke:1pt)

    
    if calc.even(heure) { line((0,19-heure),(15,19-heure), stroke:0.5pt+gray) }

  }
  line((0,11),(15,11), stroke:1pt) // 8h
  line((0,0),(15,0), stroke:1pt) // 19h

  


  for c in cours {
    content(
      (3*c.day, gety(c.start)+.05),
      (3*(c.day+1), gety(c.end)-.05),
      frame: "rect",
      fill: rgb(c.color),
      stroke: 0.5pt,
      align(center+horizon)[
        *#c.name* \
        #c.prof
        
        #set text(size: 9pt, font: sans_font)
        #if c.room != "" [*#c.room*, ]
        #(c.start)--#c.end
      ]
    )
  }

  for jour in range(5) {
    content((3*jour+1.5,11.3), text([#jours.at(jour)], font: sans_font, size: 11pt))
    line((3*jour,11.3),(3*jour,0), stroke:1pt)
  }
  line((15,11.5),(15,0), stroke:1pt)
})