#import "@preview/drafting:0.2.2": inline-note, note-outline, margin-note

#let show-todos = state("show-todos", true)
#let show-notes = state("show-notes", true)

#let todo(color: red, content) = context {
  if show-todos.get() {
    inline-note(par-break: false, stroke: (paint: color, dash: "dashed"))[#text(fill: color)[TODO]: #content]
  }
}

#let todos() = context {
  if show-todos.get()  or show-notes.get() {
    note-outline()
  }
}

#let note(body, dy: auto, ..kwargs) = context {
  if show-notes.get() {
    margin-note(body, dy: dy, ..kwargs)
  } else {
    // If this is a line about smth, keep showing the inline part
    let phrase = none
    if kwargs.pos().len() > 0 {
      // margin note: kwargs.pos().at(0)
      // inline note: body
      phrase = body
    }
    if phrase != none {
      phrase
    }
  }
}

/*
#let todo-list = state("todo-list",())

#let todo(color: red, content) = context {
  if show-todos.get() {
    let todonum = todo-list.get().len() + 1

    box(
      text(font: "DejaVu Sans Mono", white, size: .5em)[
        TODO #todonum #label("todo-"+str(todonum))
      ],
      inset: 3pt, fill: color,
    )
    text(weight: "bold", fill: color)[
      #box(text($triangle.filled.r$, size: .8em), baseline: -.8mm)
      _ #content _
      #box(text($triangle.filled.l$, size: .8em), baseline: -.8mm)
    ]

    todo-list.update(x => x + (("todo-"+str(x.len()+1),x.len()+1, content),))
  }
}

#let todos() = context {
  if (todo-list.final().len() != 0) and (show-todos.get()) {
    //pagebreak(weak: true)
    text(size: 2em, weight: "bold", fill:red)[TO-DOs]
    for t in todo-list.final() {
      let l =  label(t.first())
      let locs = query(l)
      
      assert(
        locs.len() >= 1,
        message: "todo '" + t.first() + "' not found: " +
          repr(t.last()) + " (query(" + repr(l) + ") = " + repr(locs) + ")"
      )
      
      if locs.len() >= 1 {
        list.item(link(l)[TODO n°#t.at(1) p.#locs.first().location().page() : #t.last()])
      } else {
        list.item(link(l)[TODO n°#t.at(1) p.??? : #t.last()])
      }
    }
  }
}
*/
