-- Rack & Ruin: TableTracker.lua
-- Small companion window that lists everyone at the table (party members)
-- alongside their current chip balances. Stays open independent of which
-- game window is showing, and refreshes whenever chips change or the
-- party roster changes.

RackAndRuin = RackAndRuin or {}

local ORANGE = { 1, 0.55, 0.1 }

local frame = CreateFrame("Frame", "RackAndRuinTableTrackerFrame", UIParent, "BackdropTemplate")
frame:SetSize(220, 260)
frame:SetPoint("TOPLEFT", RackAndRuinMenuFrame or UIParent, "TOPRIGHT", 16, 0)
frame:SetMovable(true)
frame:EnableMouse(true)
frame:RegisterForDrag("LeftButton")
frame:SetScript("OnDragStart", frame.StartMoving)
frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
frame:SetBackdrop({
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Buttons\\WHITE8x8",
    edgeSize = 2,
})
frame:SetBackdropColor(0, 0, 0, 0.95)
frame:SetBackdropBorderColor(unpack(ORANGE))
frame:Hide()
RackAndRuin.RegisterManagedFrame(frame)

local title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
title:SetPoint("TOP", 0, -14)
title:SetText("At the Table")
title:SetTextColor(unpack(ORANGE))

local listText = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
listText:SetPoint("TOPLEFT", 16, -46)
listText:SetPoint("RIGHT", -16, 0)
listText:SetJustifyH("LEFT")
listText:SetJustifyV("TOP")
listText:SetTextColor(0.9, 0.9, 0.9)
listText:SetText("")

local closeButton = CreateFrame("Button", nil, frame, "BackdropTemplate")
closeButton:SetSize(24, 24)
closeButton:SetPoint("TOPRIGHT", -6, -6)
closeButton:SetBackdrop({
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Buttons\\WHITE8x8",
    edgeSize = 1,
})
closeButton:SetBackdropColor(0, 0, 0, 1)
closeButton:SetBackdropBorderColor(unpack(ORANGE))
local closeLabel = closeButton:CreateFontString(nil, "OVERLAY", "GameFontNormal")
closeLabel:SetPoint("CENTER")
closeLabel:SetText("x")
closeLabel:SetTextColor(1, 0.85, 0.6)
closeButton:SetScript("OnClick", function() frame:Hide() end)

local function TruncateName(name, maxLen)
    if #name > maxLen then
        return name:sub(1, maxLen - 1) .. "…"
    end
    return name
end

function RackAndRuin.TableTracker_Refresh()
    if not frame:IsShown() then return end

    local players = RackAndRuin.Net.GetPlayers()
    local myName = UnitName("player")
    local lines = {}

    for _, name in ipairs(players) do
        local chips = RackAndRuin.Chips.Get(name)
        local marker = (name == myName) and " (you)" or ""
        table.insert(lines, TruncateName(name, 16) .. marker .. ": " .. chips .. " chips")
    end

    listText:SetText(table.concat(lines, "\n"))
end

function RackAndRuin.ToggleTableTracker()
    if frame:IsShown() then
        frame:Hide()
    else
        frame:Show()
        RackAndRuin.TableTracker_Refresh()
    end
end

function RackAndRuin.ShowTableTracker()
    frame:Show()
    RackAndRuin.TableTracker_Refresh()
end

RackAndRuin.Net.RegisterStatusListener(function()
    RackAndRuin.TableTracker_Refresh()
end)

RackAndRuin.TableTrackerFrame = frame
