-- HDG_HT_W_DecorCurrency.lua  (C4b — split from CurrencyWallet)
-- Currencies used to buy decor from vendors. Source: HDG_VendorDB.items[].currCost
-- entries of type {"c", currID, amount} → C_CurrencyInfo.GetCurrencyInfo for
-- name/quantity/cap. Aggregator does the scan + cache. Renders as ChipStrip.

local _, privateDB = ...
privateDB.HDG_HT = privateDB.HDG_HT or {}
privateDB.HDG_HT.Widgets = privateDB.HDG_HT.Widgets or {}
local W = { id = "decorCurrency", title = "Decor Currencies" }
privateDB.HDG_HT.Widgets.DecorCurrency = W

local Patterns = privateDB.HDG_HT.Patterns

function W.Create(parent, dims)
    local self = {}
    self.pattern = Patterns.ChipStrip(parent, dims, {
        chipHeight = (dims and dims.chipHeight) or 18,
        chipGap    = (dims and dims.chipGap)    or 4,
    })

    function self:Refresh(snap)
        local housing = (snap and snap.wallet and snap.wallet.housing) or {}
        local ED = _G.HDG_ExpansionData
        local Theme = privateDB.HDG_HT.Theme
        local fmt   = BreakUpLargeNumbers or tostring
        -- Sufficient = "no more needed for any uncollected decor". Two cases:
        --   1. needed == 0  → player owns every decor that uses this currency
        --   2. have >= needed → player can afford remaining uncollected
        -- Both signal "saving for extras / done" → green count.
        local cSuccess = (Theme and Theme.C_Success and Theme.C_Success()) or "|cFF40C040"
        local chips = {}
        for _, c in ipairs(housing) do
            local sufficient = (c.needed or 0) == 0 or c.count >= c.needed
            local count
            if (c.needed or 0) > 0 then
                local marker = sufficient
                    and "  |TInterface\\RAIDFRAME\\ReadyCheck-Ready:0|t"
                    or ""
                count = string.format("%s / %s%s", fmt(c.count), fmt(c.needed), marker)
            else
                count = fmt(c.count)
            end
            if sufficient then
                count = cSuccess .. count .. "|r"
            end
            chips[#chips + 1] = {
                label = c.name,
                count = count,
                color = (c.expansion and ED and ED.GetColor and ED.GetColor(c.expansion)) or nil,
            }
        end
        if #chips == 0 then chips[1] = { label = "Currencies", count = "none" } end
        self.pattern:Update({ chips = chips })
    end

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

return W
