-- VDS_Rows_CanvasDecor
-- ============================================================================
-- Flat decor-row factory for the canvas picker (post-tree). Renders:
--   [icon]  [drops]  Name (truncates)              [count] [palette]
--
-- - icon:      14x14 decor icon from ed.icon; muted alpha when unowned
-- - drops:     1-3 dye-drop_32 droplets, stacked vertically, bottom-aligned
-- - name:      flex; ellipsis-truncates on overflow
-- - count:     gold number; visible only when loadoutCount > 0
-- - palette:   catalog-palette-icon at 12x12, right edge
--
-- Per-frame fields: _hl, _sel, _icon, _droplets[1..3], _name, _loadoutCount,
--   _palette. Built once via _laidOut flag; idempotent across pool re-acquires.
--
-- Replaces the canvas tree's `decor` row branch from VDS_Rows_CanvasTree.lua
-- (canvasRow factory). The old `loadout` row branch is no longer reachable --
-- loadouts move to the col 3 chip wall in Phase 4.

VDS = VDS or {}

local WHITE = "Interface\\Buttons\\WHITE8x8"

local function ensureFS(row, field, template)
    if not row[field] then
        row[field] = row:CreateFontString(nil, "OVERLAY", template)
    end
    row[field]:ClearAllPoints()
    return row[field]
end

local function ensureTex(row, field, layer, sublevel)
    if not row[field] then
        row[field] = row:CreateTexture(nil, layer, nil, sublevel)
    end
    row[field]:ClearAllPoints()
    return row[field]
end

local function buildChromeOnce(row)
    if row._laidOut then return end
    row._laidOut = true
    row:SetHeight(24)

    -- Selection highlight (full-row tint, behind everything).
    local sel = ensureTex(row, "_sel", "BACKGROUND", 0)
    sel:SetTexture(WHITE)
    sel:SetVertexColor(0.40, 0.55, 0.85, 0.18)
    sel:SetAllPoints()
    sel:Hide()

    -- Hover highlight (mouseover).
    local hl = ensureTex(row, "_hl", "BACKGROUND", 1)
    hl:SetTexture(WHITE)
    hl:SetVertexColor(1, 1, 1, 0.05)
    hl:SetAllPoints()
    hl:Hide()
    row:SetScript("OnEnter", function() row._hl:Show() end)
    row:SetScript("OnLeave", function() row._hl:Hide() end)

    -- Decor icon -- close to full row height (20x20 in a 24px row).
    local icon = ensureTex(row, "_icon", "ARTWORK", 0)
    icon:SetSize(20, 20)
    icon:SetPoint("LEFT", row, "LEFT", 4, 0)

    -- Channels droplet stack. Position is computed per row in paint() since
    -- the stack centers vertically based on count (1 drop centers different
    -- to 2 or 3). Here we just create/style; positioning lives in paint.
    row._droplets = row._droplets or {}
    for i = 1, 3 do
        local d = ensureTex(row, "_drop" .. i, "ARTWORK", 1)
        d:SetSize(5, 6)
        d:SetAtlas(VDS.Constants.ATLAS.DYE_DROP)
        d:SetVertexColor(0.50, 0.68, 0.88, 0.85)  -- muted blue (metadata, not interactive)
        d:Hide()
        row._droplets[i] = d
    end

    -- Name (mid-row, truncating). Width is dynamic via right-anchoring to
    -- the loadout-cluster's left edge.
    local name = ensureFS(row, "_name", "GameFontNormal")
    name:SetPoint("LEFT", row, "LEFT", 40, 0)
    name:SetJustifyH("LEFT")
    name:SetWordWrap(false)

    -- Loadout palette icon (rightmost edge).
    local palette = ensureTex(row, "_palette", "ARTWORK", 1)
    palette:SetSize(12, 12)
    palette:SetPoint("RIGHT", row, "RIGHT", -4, 0)
    palette:SetAtlas("catalog-palette-icon")
    palette:Hide()

    -- Loadout count (gold, left of palette icon).
    local count = ensureFS(row, "_loadoutCount", "GameFontHighlightSmall")
    count:SetPoint("RIGHT", palette, "LEFT", -2, 0)
    count:SetTextColor(0.82, 0.66, 0.31, 1)  -- gold
    count:Hide()

    -- Right-anchor name to the loadout cluster so name truncates first.
    name:SetPoint("RIGHT", count, "LEFT", -4, 0)

    -- Click handler. Reads row._ed (stamped in paint) -- ADR-041 row-ed pattern.
    row:RegisterForClicks("AnyUp")
    row:SetScript("OnClick", function(self)
        local ed = self._ed
        if not (ed and ed.decorID) then return end  -- exception(boundary): click can race pool reconfigure; Controller_Canvas is TOC-guaranteed
        VDS.Controller_Canvas:OnDecorListClick(ed)
    end)
end

local function paint(row, ed)
    buildChromeOnce(row)

    -- Icon: prefer ed.icon (atlas/fileID); fall back to question-mark when missing.
    if ed.icon then
        row._icon:SetTexture(ed.icon)
    else
        row._icon:SetTexture("Interface\\Icons\\INV_Misc_QuestionMark")
    end
    row._icon:SetVertexColor(1, 1, 1, ed.isOwned and 1 or 0.45)
    row._icon:Show()

    -- Channels: N droplets vertically centered in the row with mild overlap.
    -- Drop height = 6px; STEP = 4 → 2px overlap per pair. Stack stays compact
    -- + still legible as a count signal.
    -- Drop i's center offset from row center = (i - (N+1)/2) * STEP.
    local ch = ed.channels or 0  -- exception(nullable): channels unknown until variant info resolves
    local STEP = 4
    for i = 1, 3 do
        local d = row._droplets[i]
        if i <= ch then
            d:ClearAllPoints()
            d:SetPoint("LEFT", row, "LEFT", 28, (i - (ch + 1) * 0.5) * STEP)
            d:Show()
        else
            d:Hide()
        end
    end

    -- Name.
    row._name:SetText(ed.name or ("#" .. tostring(ed.decorID or "?")))
    row._name:SetTextColor(ed.isOwned and 1 or 0.62, ed.isOwned and 1 or 0.62, ed.isOwned and 0.95 or 0.55, 1)

    -- Loadout count + palette icon (only when count > 0).
    local lc = ed.loadoutCount or 0  -- exception(nullable): no saved loadouts = no count field
    if lc > 0 then
        row._loadoutCount:SetText(tostring(lc))
        row._loadoutCount:Show()
        row._palette:Show()
    else
        row._loadoutCount:Hide()
        row._palette:Hide()
    end

    -- Selection.
    if ed.selected then row._sel:Show() else row._sel:Hide() end

    -- Stamp ed for OnClick (ADR-041: handlers read row._ed, not Store:GetState).
    row._ed = ed
end

VDS.RowRegistry:Register("decorListRow", {
    init  = paint,
    reset = function(row)
        if row._sel  then row._sel:Hide()  end
        if row._hl   then row._hl:Hide()   end
        if row._icon then row._icon:Hide() end
        if row._droplets then
            for i = 1, 3 do if row._droplets[i] then row._droplets[i]:Hide() end end
        end
        if row._loadoutCount then row._loadoutCount:Hide() end
        if row._palette      then row._palette:Hide()      end
        row._ed = nil
    end,
})
