-- VDS_Rows_CustomPalette
-- ============================================================================
-- Row factory for the custom-panel sidebar palette list. Each row shows:
--   palette name (left)
--   small colored droplets (one per dye in the palette, capped at ~5)
--   dye count (right; "7")
-- Click selects the palette: SET_ACTIVE_SUBFILTER kind=theme + ENTER_EDIT_MODE.

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()
    row[field]:Show()
    return row[field]
end

local function lazyBuildPaletteRow(row)
    if row._cpBuilt then return end
    row._cpBuilt = true
    row:SetHeight(34)

    local bg = row:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetTexture(WHITE)
    row._bg = bg
    VDS.Theme:Register(bg, "RowBg")

    -- Selected highlight (Palette.accent.teal overlay, hidden by default).
    local sel = row:CreateTexture(nil, "BACKGROUND", nil, 1)
    sel:SetAllPoints()
    sel:SetTexture(WHITE)
    VDS.Theme:Register(sel, "RowCustomSelectedBg")
    sel:Hide()
    row._sel = sel

    -- Edit-mode accent bar on the left (Palette.accent.teal, 2px).
    local accent = row:CreateTexture(nil, "OVERLAY")
    accent:SetPoint("TOPLEFT",    0, 0)
    accent:SetPoint("BOTTOMLEFT", 0, 0)
    accent:SetWidth(2)
    accent:SetTexture(WHITE)
    VDS.Theme:Register(accent, "RowCustomAccent")
    accent:Hide()
    row._accent = accent

    local hl = row:CreateTexture(nil, "HIGHLIGHT")
    hl:SetAllPoints()
    hl:SetTexture(WHITE)
    VDS.Theme:Register(hl, "RowHoverHl")

    local name = ensureFS(row, "_name", "GameFontNormalSmall")
    name:SetPoint("TOPLEFT",  row, "TOPLEFT",  8, -4)
    name:SetPoint("RIGHT",    row, "RIGHT",   -22, 0)
    name:SetJustifyH("LEFT")
    VDS.Theme:Register(name, "TextRowName")

    local count = ensureFS(row, "_count", "GameFontNormalSmall")
    count:SetPoint("TOPRIGHT", row, "TOPRIGHT", -6, -4)
    count:SetJustifyH("RIGHT")
    VDS.Theme:Register(count, "TextRowMeta")

    -- 20 droplet slots (palette max). 7px @ 8px stride fits the 200px sidebar.
    row._droplets = row._droplets or {}
    for i = 1, 20 do
        local d = row:CreateTexture(nil, "ARTWORK")
        d:SetSize(7, 7)
        d:SetAtlas(VDS.Constants.ATLAS.DYE_DROP)
        d:SetPoint("BOTTOMLEFT", row, "BOTTOMLEFT", 8 + (i - 1) * 8, 4)
        d:Hide()
        row._droplets[i] = d
    end

    row:RegisterForClicks("AnyUp")
    row:SetScript("OnClick", function(self, button)
        VDS.Controller_Browse:OnPaletteRowClick(self, button)
    end)

    VDS.TooltipEngine:Attach(row, VDS.TooltipRecipes.CustomPaletteRow)
end

local function bindPaletteRow(row, data)
    lazyBuildPaletteRow(row)
    row._paletteName = data.name

    row._name:SetText(data.name or "?")
    row._count:SetText(tostring(data.dyeCount or 0))

    if data.selected then row._sel:Show()    else row._sel:Hide()    end
    if data.editing  then row._accent:Show() else row._accent:Hide() end

    -- Selector omits per-dye color (purity); resolve at bind time via C_DyeColor. -- boundary
    local dyes = data.dyes or {}
    for i = 1, 20 do
        local d = row._droplets[i]
        local dyeID = dyes[i]
        if dyeID then
            local info = VDS.DyeOwnershipObserver:ResolveInfo(dyeID)
            local c = info and info.swatchRGB  -- exception(nullable): catalog lookup can miss pre-CATALOG_READY
            if c then
                d:SetVertexColor(c.r, c.g, c.b, 1)
                d:Show()
            else
                d:Hide()
            end
        else
            d:Hide()
        end
    end
end

local function resetPaletteRow(row)
    row._paletteName = nil
    if row._name  then row._name:SetText("")  end
    if row._count then row._count:SetText("") end
    if row._droplets then
        for i = 1, 20 do if row._droplets[i] then row._droplets[i]:Hide() end end
    end
end

VDS.RowRegistry:Register("customPaletteRow", {
    init  = bindPaletteRow,
    reset = resetPaletteRow,
})
