-- HDG_HT_W_EventCard_Common.lua
-- Shared rendering for event-vendor decor cards (Ritual Sites, Abyss Anglers,
-- Decor Duels). Each event gets its own widget id (so picker can enable/
-- disable independently), but they all share this single Create implementation
-- via HDG_HT.EventCardHelper.Build({ id, title, vendorNpcID }).
--
-- Visual: IconShelf of every decor sold by the event vendor, full-alpha for
-- collected items and dimmed for uncollected. Progress chip "N/M (P%)" along
-- the bottom. Click row → tooltip via IconShelf's existing OnEnter.

local _, privateDB = ...
privateDB.HDG_HT = privateDB.HDG_HT or {}
local Helper = {}
privateDB.HDG_HT.EventCardHelper = Helper

local function GetItemIcon(itemID)
    return (C_Item and C_Item.GetItemIconByID and C_Item.GetItemIconByID(itemID))
        or "Interface\\Icons\\INV_Misc_QuestionMark"
end

local function GetItemName(itemID)
    if _G.HDG_Data and _G.HDG_Data.GetLocalizedName then
        local n = _G.HDG_Data.GetLocalizedName(itemID)
        if n then return n end
    end
    return C_Item and C_Item.GetItemNameByID and C_Item.GetItemNameByID(itemID)
        or ("Item " .. tostring(itemID))
end

-- Build a widget definition for a single event vendor.
-- cfg = { id = "ritualSites", title = "Ritual Sites", vendorNpcID = 255495 }
function Helper.Build(cfg)
    local W = { id = cfg.id, title = cfg.title }

    function W.Create(parent, dims)
        local self = {}
        local Patterns = privateDB.HDG_HT.Patterns

        -- Body region for IconShelf — leave 16px at the bottom for the
        -- progress text so the shelf doesn't overlap it.
        local body = CreateFrame("Frame", nil, parent)
        body:SetPoint("TOPLEFT",  parent, "TOPLEFT",  0, 0)
        body:SetPoint("TOPRIGHT", parent, "TOPRIGHT", 0, 0)
        body:SetPoint("BOTTOM",   parent, "BOTTOM",   0, 16)
        self.body = body

        self.shelf = Patterns.IconShelf(body, dims, {
            iconSize = (dims and dims.iconSize) or 24,
            iconGap  = (dims and dims.iconGap)  or 4,
        })

        -- Progress text along the bottom edge.
        local HDG = _G.HousingDecorGuide
        local sch = (HDG and HDG.Theme and HDG.Theme.GetCurrentScheme()) or {}
        self.progress = parent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        self.progress:SetPoint("BOTTOMLEFT",  parent, "BOTTOMLEFT",  4, 2)
        self.progress:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", -4, 2)
        self.progress:SetJustifyH("LEFT")
        if sch.text_dim then
            self.progress:SetTextColor(sch.text_dim.r, sch.text_dim.g, sch.text_dim.b)
        end
        local CockpitTheme = privateDB.HDG_HT and privateDB.HDG_HT.Theme
        if CockpitTheme and CockpitTheme.ApplyFont then
            CockpitTheme.ApplyFont(self.progress, sch, "small")
        elseif HDG and HDG.Theme and HDG.Theme.ApplyFont then
            HDG.Theme.ApplyFont(self.progress, sch, "small")
        end

        function self:Refresh(_)
            local vdb = _G.HDG_VendorDB
            local vendor = vdb and vdb[cfg.vendorNpcID]
            local itemMap = vendor and vendor.items
            if not itemMap then
                self.shelf:Update({ items = {} })
                self.progress:SetText("Vendor data not loaded")
                return
            end

            local list = {}
            local total, collected = 0, 0
            for itemID in pairs(itemMap) do
                total = total + 1
                local owned = _G.HDG_Data and _G.HDG_Data.IsItemCollected
                    and _G.HDG_Data.IsItemCollected(itemID) or false
                if owned then collected = collected + 1 end
                list[#list + 1] = {
                    itemID = itemID,
                    iconID = GetItemIcon(itemID),
                    name   = GetItemName(itemID),
                    owned  = owned,
                }
            end
            -- Stable sort: collected first, then by name. Helps the eye scan
            -- which ones are still missing without hunting through the grid.
            table.sort(list, function(a, b)
                if a.owned ~= b.owned then return a.owned end
                return (a.name or "") < (b.name or "")
            end)
            self.shelf:Update({ items = list })

            local pct = total > 0 and math.floor((collected / total) * 100 + 0.5) or 0
            self.progress:SetText(string.format("%d / %d  (%d%%)", collected, total, pct))
        end

        function self:Resize(w, h)
            self.shelf:Resize(w, math.max(0, (h or 0) - 16))
        end

        return self
    end

    return W
end
