--[[
============================================================================
ETB v11.0 - SetsUI.lua
============================================================================
UI for browsing Blizzard transmog sets - redesigned to match Blizzard style.
Shows full mannequin with item icons for each slot.
============================================================================
]]--

local ADDON_NAME, NS = ...
local ETB = NS.ETB
local ETBDropdown = NS.ETBDropdown

-- v12.56.1: Preserve existing ETB.SetsUI table. Sources.lua and List.lua
-- load BEFORE this file and register .Sources / .List on NS.ETB.SetsUI.
-- A bare `ETB.SetsUI = { ... }` table-literal assignment clobbers those
-- registrations, causing runtime errors when delegators run. This is the
-- same v12.45.1 namespace-clobber bug class.
ETB.SetsUI = ETB.SetsUI or {}
local SetsUI = ETB.SetsUI

SetsUI.frame = SetsUI.frame or nil
SetsUI.setListFrame = SetsUI.setListFrame or nil
SetsUI.setDetailFrame = SetsUI.setDetailFrame or nil
SetsUI.setButtons = SetsUI.setButtons or {}
SetsUI.selectedSetID = SetsUI.selectedSetID or nil
SetsUI.currentFilter = SetsUI.currentFilter or "blizzard"  -- "blizzard", "custom", "collected", "incomplete", "all", "community"
SetsUI.searchText = SetsUI.searchText or ""
SetsUI.armorFilter = SetsUI.armorFilter or "all"          -- "all", "CLOTH", "LEATHER", "MAIL", "PLATE"
SetsUI.expansionFilter = SetsUI.expansionFilter or "all"  -- "all", 0=Classic, 1=TBC, 2=Wrath, 3=Cata, 4=MoP
SetsUI.classFilter = SetsUI.classFilter or "all"          -- "all", or classMask number (1=Warrior, 2=Paladin, etc.)
SetsUI.colorFilter = SetsUI.colorFilter                   -- nil = all, or "RED", "BLUE", etc.
SetsUI.sortedSets = SetsUI.sortedSets or {}
SetsUI.pieceButtons = SetsUI.pieceButtons or {}  -- Buttons for each set piece

-- v12.56.0: CLASS_MASK_MAP, CLASS_COLORS, FACTION_INFO, GetClassFromMask,
-- and GetFactionDisplay were moved into UI/Windows/SetsUI/List.lua since
-- they're only referenced inside the List cluster (UpdateSetButton etc.).
--
-- v12.57.0: SLOT_ORDER, SLOT_NAMES, INVTYPE_TO_BUTTON, INVSLOT_TO_BUTTON,
-- BUTTON_TO_INVSLOT, and INVSLOT_NAMES were moved into UI/Windows/SetsUI/
-- Detail.lua for the same reason — they're only referenced by the Detail
-- cluster methods.

-- =============================================================================
-- CREATE SETS PANEL (Split View: List on Left, Detail on Right)
-- =============================================================================

function SetsUI:Create(parent)
    if self.frame then return self.frame end
    
    local f = CreateFrame("Frame", "ETBSetsUI", parent, "BackdropTemplate")
    f:SetAllPoints(parent)
    f:SetBackdrop({
        bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background-Dark",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        tile = true, tileSize = 16, edgeSize = 12,
        insets = { left = 4, right = 4, top = 4, bottom = 4 }
    })
    
    -- Create left panel (set list)
    self:CreateSetListPanel(f)
    
    -- Create right panel (set detail like Blizzard's UI)
    self:CreateSetDetailPanel(f)
    
    self.frame = f
    return self.frame
end

function SetsUI:CreateSetListPanel(parent)
    return NS.ETB.SetsUI.List:CreateSetListPanel(self, parent)
end


function SetsUI:CreateSetDetailPanel(parent)
    return NS.ETB.SetsUI.Detail:CreateSetDetailPanel(self, parent)
end


function SetsUI:CreatePieceIcons(parent)
    return NS.ETB.SetsUI.Detail:CreatePieceIcons(self, parent)
end

function SetsUI:GetInventorySlotID(slotName)
    return NS.ETB.SetsUI.Detail:GetInventorySlotID(self, slotName)
end

function SetsUI:CycleSlotAlternative(btn)
    return NS.ETB.SetsUI.Detail:CycleSlotAlternative(self, btn)
end


function SetsUI:UpdatePieceIcons(setID)
    return NS.ETB.SetsUI.Detail:UpdatePieceIcons(self, setID)
end


function SetsUI:CreateSmallFilterButton(parent, text, x, y, filter, width)
    return NS.ETB.SetsUI.List:CreateSmallFilterButton(self, parent, text, x, y, filter, width)
end


function SetsUI:UpdateFilterButtons()
    return NS.ETB.SetsUI.List:UpdateFilterButtons(self)
end


function SetsUI:RefreshSetList()
    return NS.ETB.SetsUI.List:RefreshSetList(self)
end


function SetsUI:PopulateSetList()
    return NS.ETB.SetsUI.List:PopulateSetList(self)
end


function SetsUI:UpdateVisibleButtons()
    return NS.ETB.SetsUI.List:UpdateVisibleButtons(self)
end


function SetsUI:UpdateSetButton(btn, setData)
    return NS.ETB.SetsUI.List:UpdateSetButton(self, btn, setData)
end


function SetsUI:CreateSetListButton(parent)
    return NS.ETB.SetsUI.List:CreateSetListButton(self, parent)
end


function SetsUI:SelectSet(setID)
    return NS.ETB.SetsUI.Detail:SelectSet(self, setID)
end


function SetsUI:DisplayCustomSet(setID, detail)
    return NS.ETB.SetsUI.Detail:DisplayCustomSet(self, setID, detail)
end


function SetsUI:DisplayCommunitySet(setID, detail)
    return NS.ETB.SetsUI.Detail:DisplayCommunitySet(self, setID, detail)
end


function SetsUI:UpdateCommunitySetPieceIcons(setID, communitySet)
    return NS.ETB.SetsUI.Detail:UpdateCommunitySetPieceIcons(self, setID, communitySet)
end


function SetsUI:ConfirmDeleteSet()
    return NS.ETB.SetsUI.Detail:ConfirmDeleteSet(self)
end


function SetsUI:DeleteCurrentSet()
    return NS.ETB.SetsUI.Detail:DeleteCurrentSet(self)
end


function SetsUI:UpdateCustomSetPieceIcons(setID, customSet)
    return NS.ETB.SetsUI.Detail:UpdateCustomSetPieceIcons(self, setID, customSet)
end


function SetsUI:PopulateVariantDropdown(setID, setInfo)
    return NS.ETB.SetsUI.Detail:PopulateVariantDropdown(self, setID, setInfo)
end


function SetsUI:SetFilter(filter)
    return NS.ETB.SetsUI.List:SetFilter(self, filter)
end


function SetsUI:ShowSetSources()
    return NS.ETB.SetsUI.Sources:ShowSetSources(self)
end

function SetsUI:CreateSourcesModal()
    return NS.ETB.SetsUI.Sources:CreateSourcesModal(self)
end

function SetsUI:PopulateSourcesModal(setID, setInfo)
    return NS.ETB.SetsUI.Sources:PopulateSourcesModal(self, setID, setInfo)
end

-- =============================================================================
-- SHOW / HIDE
-- =============================================================================

function SetsUI:Show(parent)
    if not self.frame then
        self:Create(parent)
    end
    
    self.frame:SetParent(parent)
    self.frame:SetAllPoints(parent)
    self.frame:Show()
    
    self:RefreshSetList()
end

function SetsUI:Hide()
    if self.frame then
        self.frame:Hide()
    end
end

function SetsUI:Toggle(parent)
    if self.frame and self.frame:IsShown() then
        self:Hide()
    else
        self:Show(parent)
    end
end

-- Static popup at load time to prevent taint
StaticPopupDialogs["ETB_DELETE_CUSTOM_SET"] = {
    text = "Delete this custom set?",
    button1 = "Delete", button2 = "Cancel",
    OnAccept = function() SetsUI:DeleteCurrentSet() end,
    timeout = 0, whileDead = true, hideOnEscape = true, preferredIndex = 3,
}

ETB:Debug("SetsUI", "SetsUI.lua loaded")
