#import "/src/cetz.typ": draw

#import "/src/core/utils.typ": normalize-measure, normalize-units

#let name = "actor"

#let render(x, y, p, bottom) = {
  let m = normalize-measure(p.display-name)
  let style = p.resolved-style
  let spacing = normalize-units(style.spacing)
  let width = normalize-units(style.width)
  let height = width * 2
  let w2 = width / 2
  let head-r = width / 4
  let arms-y = height * 0.375

  let y0 = if bottom {
    y - m.height - spacing
  } else {
    y + m.height + height + spacing
  }

  // Head
  draw.circle(
    (x, y0 - head-r),
    radius: head-r,
    fill: style.fill,
    stroke: style.stroke
  )

  // Body
  draw.line(
    (x, y0 - head-r * 2),
    (x, y0 - height + w2),
    stroke: style.stroke
  )
  
  // Arms
  draw.line(
    (x - w2, y0 - arms-y),
    (x + w2, y0 - arms-y),
    stroke: style.stroke
  )

  // Legs
  draw.line(
    (x - w2, y0 - height),
    (x, y0 - height + w2),
    (x + w2, y0 - height),
    stroke: style.stroke
  )
  draw.content(
    (x, y),
    p.display-name,
    anchor: if bottom {"north"} else {"base"}
  )
}

#let get-size(par) = {
  let m = normalize-measure(par.display-name)

  let width = normalize-units(par.resolved-style.width)
  let height = width * 2
  let spacing = normalize-units(par.resolved-style.spacing)

  return (
    width: calc.max(m.width, width),
    height: height + spacing + m.height
  )
}

#let default-style = (
  width: 20pt,
  spacing: 5pt
)