#import "@preview/codly:1.2.0": *
#import "@preview/cetz:0.3.1"
#import "@preview/cetz-plot:0.1.0": chart
#import "constants.typ"

#let inwriting = false
#let draft = false
#let showNumberedLines = false

#assert(not(inwriting and not(draft)), message: "If inwriting is true, draft should be true as well.")

#let todo(it) = [
  #if inwriting {
    text([#emoji.pencil #it], fill: red, weight: 600)
  } else if not draft {
    panic("There is still a todo in the final version.")
  }
]

#let image-with-source(
  str,
  width: auto,
  height: auto,
  caption: str,
  source: constants.own-figure
) = {
  figure(
    figure(
      pad(y: 1em, image(str, width: width, height: height)), //"../images/" + 
      kind: "source",
      numbering: none,
      caption: [
        #constants.source: #source
      ]
    ),
    caption: figure.caption(
      position: top,
      caption
    )
  )
}

#let code(
  header: none,
  placement: none,
  caption: none,
  caption-pos: bottom,
  numbering: "1",
  outlined: true,
  breakable: false,
  range: none,
  ranges: none,
  content
) = {
  codly(
    zebra-fill: none, //luma(250),
    smart-indent: false,
    range: range,
    ranges: ranges,    
    breakable: breakable
  )
  if header != none {
    codly(
     header: if type(header) == str { [#header] } else { header },
     header-cell-args: (align: center),
    )
  }
  figure(
    placement: placement,
    caption: if caption == none {
      none
    } else {
      figure.caption(
        position: caption-pos,
        caption
      )
    },
    kind: "code",
    supplement: "Code",
    numbering: numbering,
    outlined: outlined,
    content
  )
}

#let code_file(
  path: "",
  header: none,
  placement: none,
  caption: none,
  caption-pos: bottom,
  numbering: "1",
  outlined: true,
  breakable: false,
  range: none,
  ranges: none
) = {
  code(
    header: header,
    placement: placement,
    caption: caption,
    caption-pos: caption-pos,
    numbering: numbering,
    range: range,
    ranges: ranges,
    outlined: outlined,
    breakable:breakable
  )[
    #raw(
      read(path), //"../code/" + 
      block: true,
      lang: path.split(".").at(-1)
    )
  ]
}

#let example(title: "", content) = {
  let lower_name = lower(constants.example)
  counter(lower_name).step()
  
  figure(
    kind: lower_name,
	  supplement: constants.example,
  	block(
      inset: 8pt,
      radius: 4pt,
      stroke: constants.accent_color,
      width: 100%,
      breakable: true,
      align(
        left,
        [
          #text(weight: "bold", fill: constants.accent_color, [Beispiel #context counter(lower_name).display()])
          #if repr(title).len() > 2 [
            (#text(weight: "bold", fill: constants.accent_color, title))
          ]

          #content
        ],
      )
    )
  )
}

#let colors = gradient.linear(red, blue, green)
#let draw_chart(data) = {
  align(center,
    cetz.canvas({
      chart.piechart(
        data,
        value-key: 1,
        label-key: 0,
        radius: 2.5,
        slice-style: colors,
        inner-radius: 0.75,
        inner-label: (content: (value, label) => [#text(white, str(value / data.map(x => x.at(1)).sum() * 100) + "%")], radius: 110%),
        //inner-label: (content: "%", radius: 110%),
        outer-label: (content: (value, label) => [#text(label)], radius: 140%),
        legend: (label: "LABEL" )
      )
    })
  )
}