--[[
============================================================================
ETB Set Builder - Built-in module for Enhanced Transmog Browser
============================================================================
Create custom transmog sets by clicking items. Export or send to ETB custom sets.

Usage:
  /setbuilder or /sb - Open the Set Builder UI
  /sb clear - Clear current set
  /sb export - Export current set to chat (copyable)
  /sb list - List saved sets
  /sb save <name> - Save current set with a name
  /sb load <name> - Load a saved set
  /sb delete <name> - Delete a saved set
============================================================================
]]--

local ADDON_NAME, NS = ...
local ETB = NS.ETB
-- Note: ETBDropdown is no longer captured at file scope as of v12.54.0
-- (was the source of the v12.52.1 load-order bug). The Frame submodule
-- looks it up at call time via NS.ETBDropdown when the UI is constructed.

-- Saved Variables (same name as standalone addon for compatibility)
ETB_SetBuilder_Data = ETB_SetBuilder_Data or {
    savedSets = {},
    version = 1,
}

ETB.SetBuilder = ETB.SetBuilder or {}
local SB = ETB.SetBuilder

SB.frame = nil
SB.currentSet = {
    name = "Unnamed Set",
    armorType = nil,
    source = "",
    expansion = 0,
    faction = nil,
    items = {},  -- [slot] = {itemID, name, icon}
}
SB.allItems = {}
SB.filteredItems = {}
SB.currentPage = 1
SB.itemsPerPage = 100
SB.searchText = ""
SB.filterSlot = "all"
SB.filterArmorType = "all"

-- =============================================================================
-- SLOT CONSTANTS (matching CustomSets.lua)
-- =============================================================================

local SLOTS = {
    HEAD = 1,
    SHOULDER = 2,
    CHEST = 3,
    WAIST = 4,
    LEGS = 5,
    FEET = 6,
    WRIST = 7,
    HANDS = 8,
    BACK = 9,
}

local SLOT_NAMES = {
    [1] = "Head",
    [2] = "Shoulders",
    [3] = "Chest",
    [4] = "Waist",
    [5] = "Legs",
    [6] = "Feet",
    [7] = "Wrists",
    [8] = "Hands",
    [9] = "Back",
}

-- Inventory type to slot mapping
local INVTYPE_TO_SLOT = {
    INVTYPE_HEAD = 1,
    INVTYPE_SHOULDER = 2,
    INVTYPE_CHEST = 3,
    INVTYPE_ROBE = 3,
    INVTYPE_WAIST = 4,
    INVTYPE_LEGS = 5,
    INVTYPE_FEET = 6,
    INVTYPE_WRIST = 7,
    INVTYPE_HAND = 8,
    INVTYPE_CLOAK = 9,
}

local EXPANSION_NAMES = {
    [0] = "Classic",
    [1] = "TBC",
    [2] = "Wrath",
    [3] = "Cata",
    [4] = "MoP",
}

-- Armor subclass to type mapping (used by Items.lua submodule)
local ARMOR_SUBCLASS_MAP = {
    [1] = "CLOTH",
    [2] = "LEATHER",
    [3] = "MAIL",
    [4] = "PLATE",
}

-- Category ID to Set Builder slot mapping (used by Items.lua submodule)
local CATEGORY_TO_SLOT = {
    [1] = 1,   -- Head
    [2] = 2,   -- Shoulder
    [3] = 9,   -- Back (Cloak)
    [4] = 3,   -- Chest
    [5] = nil, -- Shirt (skip)
    [6] = nil, -- Tabard (skip)
    [7] = 7,   -- Wrist
    [8] = 8,   -- Hands
    [9] = 4,   -- Waist
    [10] = 5,  -- Legs
    [11] = 6,  -- Feet
}

-- =============================================================================
-- EXPOSE FILE-LOCAL DEPENDENCIES ON SB (for submodule access)
-- =============================================================================
-- v12.52.0: Export.lua submodule reads these via `owner.X` rather than
-- file-local. Kept as fields on SB so the singleton has them. This pattern
-- matches the v12.47.0+ TransmogUI submodule extraction approach.

SB.SLOT_NAMES = SLOT_NAMES
SB.EXPANSION_NAMES = EXPANSION_NAMES
SB.INVTYPE_TO_SLOT = INVTYPE_TO_SLOT
SB.ARMOR_SUBCLASS_MAP = ARMOR_SUBCLASS_MAP   -- v12.53.0: for Items.lua
SB.CATEGORY_TO_SLOT = CATEGORY_TO_SLOT       -- v12.53.0: for Items.lua

-- =============================================================================
-- INITIALIZATION
-- =============================================================================

local initFrame = CreateFrame("Frame")
initFrame:RegisterEvent("PLAYER_LOGIN")
initFrame:SetScript("OnEvent", function(self, event)
    if event == "PLAYER_LOGIN" then
        local function initSetBuilder()
            SB:Initialize()
        end
        if ETB.RunWhenStartupIdle then
            ETB:RunWhenStartupIdle("setBuilder.init", initSetBuilder, { delay = 2, interval = 2, maxWait = 180 })
        else
            initSetBuilder()
        end
    end
end)

function SB:Initialize()
    return NS.ETB.SetBuilder.Frame:Initialize(self)
end


function SB:Show()
    return NS.ETB.SetBuilder.Frame:Show(self)
end


function SB:Hide()
    return NS.ETB.SetBuilder.Frame:Hide(self)
end


function SB:ShowEmbedded(parent)
    return NS.ETB.SetBuilder.Frame:ShowEmbedded(self, parent)
end


function SB:HideEmbedded()
    return NS.ETB.SetBuilder.Frame:HideEmbedded(self)
end


function SB:CreateEmbeddedUI(parent)
    return NS.ETB.SetBuilder.Frame:CreateEmbeddedUI(self, parent)
end


function SB:CreateUI()
    return NS.ETB.SetBuilder.Frame:CreateUI(self)
end


function SB:CreateMannequinPanel(parent)
    return NS.ETB.SetBuilder.Frame:CreateMannequinPanel(self, parent)
end


function SB:UpdateMannequin()
    return NS.ETB.SetBuilder.Frame:UpdateMannequin(self)
end


function SB:CreateSetPanel(parent)
    return NS.ETB.SetBuilder.Frame:CreateSetPanel(self, parent)
end


function SB:CreateItemBrowser(parent)
    return NS.ETB.SetBuilder.Frame:CreateItemBrowser(self, parent)
end


function SB:CreateExportPanel(parent)
    return NS.ETB.SetBuilder.Frame:CreateExportPanel(self, parent)
end


function SB:LoadAllItems()
    return NS.ETB.SetBuilder.Items:LoadAllItems(self)
end


function SB:ResolveItemNames()
    return NS.ETB.SetBuilder.Items:ResolveItemNames(self)
end


function SB:VerifyAndFinish()
    return NS.ETB.SetBuilder.Items:VerifyAndFinish(self)
end


function SB:FinishLoading()
    return NS.ETB.SetBuilder.Items:FinishLoading(self)
end

-- =============================================================================
-- FILTER AND DISPLAY ITEMS
-- =============================================================================

function SB:FilterItems()
    return NS.ETB.SetBuilder.Items:FilterItems(self)
end

function SB:DisplayItems()
    return NS.ETB.SetBuilder.Items:DisplayItems(self)
end

function SB:ScheduleNameUpdate(items)
    return NS.ETB.SetBuilder.Items:ScheduleNameUpdate(self, items)
end

-- =============================================================================
-- SET MANAGEMENT
-- =============================================================================

function SB:AddToSet(item)
    return NS.ETB.SetBuilder.Export:AddToSet(self, item)
end

function SB:RemoveFromSlot(slot)
    return NS.ETB.SetBuilder.Export:RemoveFromSlot(self, slot)
end

function SB:ClearCurrentSet()
    return NS.ETB.SetBuilder.Export:ClearCurrentSet(self)
end

function SB:UpdateSetDisplay()
    return NS.ETB.SetBuilder.Export:UpdateSetDisplay(self)
end

function SB:UpdateSlotHighlight()
    return NS.ETB.SetBuilder.Items:UpdateSlotHighlight(self)
end

-- =============================================================================
-- EXPORT
-- =============================================================================

function SB:UpdateExportText()
    return NS.ETB.SetBuilder.Export:UpdateExportText(self)
end

function SB:GenerateExportText()
    return NS.ETB.SetBuilder.Export:GenerateExportText(self)
end

function SB:ExportCurrentSet()
    return NS.ETB.SetBuilder.Export:ExportCurrentSet(self)
end

function SB:SendToETB()
    return NS.ETB.SetBuilder.Export:SendToETB(self)
end

-- =============================================================================
-- SAVE/LOAD SETS
-- =============================================================================

function SB:SaveSet(name)
    return NS.ETB.SetBuilder.Export:SaveSet(self, name)
end

function SB:LoadSet(name)
    return NS.ETB.SetBuilder.Export:LoadSet(self, name)
end

function SB:DeleteSet(name)
    return NS.ETB.SetBuilder.Export:DeleteSet(self, name)
end

function SB:ListSavedSets()
    return NS.ETB.SetBuilder.Export:ListSavedSets(self)
end

function SB:UpdateSavedSetsList()
    return NS.ETB.SetBuilder.Export:UpdateSavedSetsList(self)
end

-- =============================================================================
-- PANEL MODE (full panel in main addon content area, like Armor/Weapons/Sets)
-- =============================================================================

SB.panelFrame = nil
SB.panelContent = nil

function SB:ShowAsPanel(parent)
    return NS.ETB.SetBuilder.Frame:ShowAsPanel(self, parent)
end


function SB:HidePanel()
    return NS.ETB.SetBuilder.Frame:HidePanel(self)
end


function SB:CreatePanelUI(parent)
    return NS.ETB.SetBuilder.Frame:CreatePanelUI(self, parent)
end

-- Alias for SetsUI and slash: Open = Show
ETB.SetBuilder.Open = ETB.SetBuilder.Show
