-- HDG_HT_W_GoblinTopLumber.lua
-- Top-5 craftable decor by gold-per-lumber. Source: HDG_GoblinTab.GetTopByLumberValue.
-- Renders [icon] item-name (lumber-expansion colored) with $/lum chip.
-- Click row → switches to Goblin tab.

local _, privateDB = ...
privateDB.HDG_HT = privateDB.HDG_HT or {}
privateDB.HDG_HT.Widgets = privateDB.HDG_HT.Widgets or {}
local W = { id = "goblinTopLumber", title = "Top $/Lumber" }
privateDB.HDG_HT.Widgets.GoblinTopLumber = W

local Patterns = privateDB.HDG_HT.Patterns

local function FormatGoldShort(copper)
    if not copper or copper <= 0 then return "0g" end
    local g = math.floor(copper / 10000)
    if g >= 1 then return string.format("%dg", g) end
    local s = math.floor((copper % 10000) / 100)
    return string.format("%ds", s)
end

local function ColorHex(c)
    if not c then return nil end
    return string.format("|cFF%02x%02x%02x",
        math.floor(math.min(1, c[1] or 1) * 255 + 0.5),
        math.floor(math.min(1, c[2] or 1) * 255 + 0.5),
        math.floor(math.min(1, c[3] or 1) * 255 + 0.5))
end

function W.Create(parent, dims)
    local self = {}
    self.pattern = Patterns.IconRowList(parent, dims, {
        rowHeight = dims.rowHeight or 26,
        iconSize  = dims.iconSize  or 22,
    })

    function self:Refresh(snap)
        local items = (snap and snap.goblinTopLumber) or {}
        local rows = {}
        for i, it in ipairs(items) do
            local hex = ColorHex(it.color)
            local lineA = it.name or ("Item " .. tostring(it.itemID or "?"))
            if hex then lineA = hex .. lineA .. "|r" end
            rows[i] = {
                icon  = it.icon,
                lineA = lineA,
                lineB = it.lumberShortName or "",
                chip  = FormatGoldShort(it.lumberValue) .. "/lum",
                onClick = function()
                    if _G.HDG_UI and _G.HDG_UI.SwitchTab then
                        _G.HDG_UI.SwitchTab("goblin")
                    end
                end,
            }
        end
        self.pattern:Update({ rows = rows })
    end

    function self:Resize(w, h) self.pattern:Resize(w, h) end
    return self
end

return W
