#import "@preview/cetz:0.2.2": draw
#import "consts.typ": *
#import "participant.typ"
#import "note.typ"

#let get-arrow-marks(sym, color) = {
  if type(sym) == array {
    return sym.map(s => get-arrow-marks(s, color))
  }
  (
    "": none,
    ">": (symbol: ">", fill: color),
    ">>": (symbol: "straight"),
    "\\": (symbol: ">", fill: color, harpoon: true, flip: true),
    "\\\\": (symbol: "straight", harpoon: true, flip: true),
    "/": (symbol: ">", fill: color, harpoon: true),
    "//": (symbol: "straight", harpoon: true),
    "x": none,
    "o": (symbol: "o"),
  ).at(sym)
}

#let _seq(
  p1,
  p2,
  comment: none,
  dashed: false,
  start-tip: "",
  end-tip: ">",
  color: black,
  flip: false,
  enable-dst: false,
  create-dst: false,
  disable-dst: false,
  destroy-dst: false,
  disable-src: false,
  destroy-src: false,
  lifeline-style: auto
) = {
  return ((
    type: "seq",
    p1: p1,
    p2: p2,
    comment: comment,
    dashed: dashed,
    start-tip: start-tip,
    end-tip: end-tip,
    color: color,
    flip: flip,
    enable-dst: enable-dst,
    create-dst: create-dst,
    disable-dst: disable-dst,
    destroy-dst: destroy-dst,
    disable-src: disable-src,
    destroy-src: destroy-src,
    lifeline-style: lifeline-style,
  ),)
}

#let render(pars-i, x-pos, participants, elmt, y, lifelines) = {
  let shapes = ()

  y -= Y-SPACE

  let h = 0
  // Reserve space for comment
  if elmt.comment != none {
    h = calc.max(h, measure(box(elmt.comment)).height / 1pt + 6)
  }
  if "linked-note" in elmt {
    h = calc.max(h, note.get-size(elmt.linked-note).height / 2)
  }
  y -= h

  let i1 = pars-i.at(elmt.p1)
  let i2 = pars-i.at(elmt.p2)

  let start-info = (
    i: i1,
    x: x-pos.at(i1),
    y: y,
    ll-lvl: lifelines.at(i1).level * LIFELINE-W / 2
  )
  let end-info = (
    i: i2,
    x: x-pos.at(i2),
    y: y,
    ll-lvl: lifelines.at(i2).level * LIFELINE-W / 2
  )

  if elmt.disable-src {
    let src-line = lifelines.at(i1)
    src-line.level -= 1
    src-line.lines.push(("disable", y))
    lifelines.at(i1) = src-line
  }
  if elmt.destroy-src {
    let src-line = lifelines.at(i1)
    src-line.lines.push(("destroy", y))
    lifelines.at(i1) = src-line
  }
  if elmt.disable-dst {
    let dst-line = lifelines.at(i2)
    dst-line.level -= 1
    dst-line.lines.push(("disable", y))
    lifelines.at(i2) = dst-line
  }
  if elmt.destroy-dst {
    let dst-line = lifelines.at(i2)
    dst-line.lines.push(("destroy", y))
    lifelines.at(i2) = dst-line
  }
  if elmt.enable-dst {
    let dst-line = lifelines.at(i2)
    dst-line.level += 1
    lifelines.at(i2) = dst-line
  }
  if elmt.create-dst {
    let par = participants.at(i2)
    let m = measure(box(par.display-name))
    let f = if i1 > i2 {-1} else {1}
    end-info.x -= (m.width + PAR-PAD.last() * 2) / 2pt * f
    shapes += participant.render(x-pos, par, y: y - CREATE-OFFSET)
  }

  end-info.ll-lvl = lifelines.at(i2).level * LIFELINE-W / 2

  // Compute left/right position at start/end
  start-info.insert("lx", start-info.x)
  if start-info.ll-lvl != 0 { start-info.lx -= LIFELINE-W / 2 }
  end-info.insert("lx", end-info.x)
  if end-info.ll-lvl != 0 { end-info.lx -= LIFELINE-W / 2 }

  start-info.insert("rx", start-info.x + start-info.ll-lvl)
  end-info.insert("rx", end-info.x + end-info.ll-lvl)

  // Choose correct points to link
  let x1 = start-info.rx
  let x2 = end-info.lx

  if (start-info.i > end-info.i) {
    x1 = start-info.lx
    x2 = end-info.rx
  }

  let style = (
    mark: (
      start: get-arrow-marks(elmt.start-tip, elmt.color),
      end: get-arrow-marks(elmt.end-tip, elmt.color),
      scale: 1.2
    ),
    stroke: (
      dash: if elmt.dashed {(2pt,2pt)} else {"solid"},
      paint: elmt.color,
      thickness: .5pt
    )
  )

  let y0 = y
  if "linked-note" in elmt {
    let shps = note.render(pars-i, x-pos, elmt.linked-note, y, lifelines).last()
    shapes += shps
  }

  if elmt.p1 == elmt.p2 {
    if elmt.flip {
      x1 = start-info.lx
    } else {
      x2 = end-info.rx
    }

    let x-mid = if elmt.flip {
      calc.min(x1, x2) - 20
    } else {
      calc.max(x1, x2) + 20
    }

    if elmt.comment != none {
      shapes += draw.content(
        (x1, y),
        elmt.comment,
        anchor: if elmt.flip {"south-east"} else {"south-west"},
        padding: 3pt
      )
    }

    shapes += draw.line(
      (x1, y),
      (x-mid, y),
      (x-mid, y - 10),
      (x2, y - 10),
      ..style
    )
    y -= 10

  } else {
    if elmt.comment != none {
      let x = calc.min(x1, x2)
      if x2 < x1 {
        x += COMMENT-PAD
      }
      shapes += draw.content(
        (x, y),
        elmt.comment,
        anchor: "south-west",
        padding: 3pt
      )
    }

    shapes += draw.line(
      (x1, y),
      (x2, y),
      ..style
    )
  }
  if elmt.enable-dst {
    let dst-line = lifelines.at(i2)
    dst-line.lines.push(("enable", y, elmt.lifeline-style))
    lifelines.at(i2) = dst-line
  }
  if elmt.create-dst {
    y -= CREATE-OFFSET
    let dst-line = lifelines.at(i2)
    dst-line.lines.push(("create", y))
    lifelines.at(i2) = dst-line
  }

  if "linked-note" in elmt {
    let m = note.get-size(elmt.linked-note)
    y = calc.min(y, y0 - m.height / 2)
  }

  let r = (y, lifelines, shapes)
  return r
}