package de.qwertyuiop.copret
import syntax.*
import os.Path

trait Templates:
  def titleLine(title: String)(using theme: Theme) = Paragraph(
    "\n" + Format.figlet(title, theme.font("titleLine", "pagga")).block.blue + "\n",
  )

  def header(using theme: Theme) = Meta((p, pos) => {
    val left   = p.meta.getOrElse("author", "")
    val center = p.meta.getOrElse("title", "")
    val right  = s"${pos + 1} / ${p.slides.size}"
    theme.style("titleLine")(Format.distribute(left, center, right)).text
  })

  def slide(title: String)(slides: Slide*)(using Theme) = Group(
    Clear :: header :: titleLine(title) :: slides.toList,
  )
  def slide(slides: Slide*)(using Theme)                = Group(
    Clear :: header :: slides.toList,
  )

  def markdown(title: String, content: Path)(using Theme) = slide(title)(
    Paragraph(
      %%%(
        "/usr/bin/mdcat",
        "--columns",
        (columns * 0.8).toInt.toString,
        content.toString,
      )(using os.pwd).block,
    ),
  )

  def list(bullet: (Int, Int) => String, continued: (Int, Int) => String)(items: String*)(using Theme): Paragraph =
    items.zipWithIndex
      .map((str, index) => {
        def formatFirst(first: String) =
          s"    ${bullet(index, items.size)} $first"

        str.split("\n").toList match
          case first :: Nil  => formatFirst(first)
          case first :: cont =>
            formatFirst(first)
              + cont.map(line => s"    ${continued(index, items.size)}${line}").mkString("\n", "\n", "")
          case Nil           => ""
      })
      .mkString("\n")
      .par

  def ulist(items: String*)(using Theme) = list(bullet = (_, _) => "✦ ", continued = (_, _) => "   ")(items*)

  def olist(items: String*)(using Theme) =
    def digits(i: Int): Int = math.ceil(math.log10(i)).toInt
    list(
      bullet = (i, max) => (" " * (digits(max + 1) - digits(i + 1) - 1)) + s"${i + 1}.",
      continued = (i, max) => " " * digits(max + 1),
    )(items*)

  lazy val --- = Paragraph("\n" + ("-" * columns).yellow.toString + "\n")
  lazy val === = Paragraph("\n" + ("═" * columns).yellow.toString + "\n")
end Templates

/* vim:set tw=120: */
