-- Paste into one new Script, compile, and enable it in the desktop client.
-- This demo uses nine invented members, not live party data.
local party = ui.window("party")
local members = {"Aster", "Birch", "Clover", "Dawn", "Ember", "Fern", "Gale", "Hazel", "Iris"}

function onLoad()
  party:create({
    title = "Demo party", width = 420, height = 360,
    minWidth = 320, minHeight = 260, x = 24, y = 24,
  })
  local cells = {}
  for index, name in ipairs(members) do
    cells[#cells + 1] = {
      type = "column", id = "member_" .. index,
      action = "select_member", actionValue = name,
      style = { gap = 4, padding = 8, background = "panel-2", borderRadius = 4 },
      children = {
        { type = "text", id = "name_" .. index, text = name, style = { fontWeight = "bold" } },
        { type = "gauge", id = "hp_" .. index, label = "HP", value = 100 - index * 6, maximum = 100, style = { color = "green" } },
        { type = "gauge", id = "sp_" .. index, label = "SP", value = 100 - index * 4, maximum = 100, style = { color = "cyan" } },
      },
    }
  end
  party:render({ type = "column", id = "party_root", style = { gap = 8 }, children = {
    { type = "text", id = "selected", text = "Sample data: choose a member", style = { color = "muted" } },
    { type = "grid", id = "members", columns = 3, rows = 3, style = { gap = 6 }, children = cells },
  } })
end

-- A Lua trigger or alias can call DemoPartyHP(1, 45, 100).
-- The captured reference continues to belong to this Script.
function DemoPartyHP(slot, hp, maximum)
  local index = tonumber(slot)
  if not index or index < 1 or index > 9 or index % 1 ~= 0 then return end
  party:update("hp_" .. index, { value = tonumber(hp) or 0, maximum = tonumber(maximum) or 100 })
end

function onUI(event)
  if event.window == "party" and event.action == "select_member" then
    state.demoSelectedMember = event.value
    party:update("selected", { text = "Selected: " .. tostring(event.value) })
  end
end
