-- =============================================================================
-- ETB v12.60.0 — UI/MainFrame/Filters.lua
-- =============================================================================
-- Third MainFrame submodule. Owns the small cluster of "filter UI state"
-- methods that update the category tabs, the collection-status buttons
-- (All / Collected / Not Collected), and the weapon-vs-armor dropdown
-- visibility.
--
-- All four methods are visual-state updates — no UI construction here,
-- just toggling colors and visibility based on current filter state.
--
-- Public API (all take owner = MainFrame singleton as first arg):
--   Filters:UpdateCategoryTabs(owner)              -- color the active tab green
--   Filters:UpdateCollectionButtons(owner, filter) -- color All/Collected/Not buttons
--   Filters:UpdateWeaponTypeFilter(owner)          -- alias for UpdateWeaponCategoryUI
--   Filters:UpdateWeaponCategoryUI(owner)          -- show/hide weapon vs armor dropdowns
--
-- Owner contract — fields read/written:
--   owner.frame                   (with .armorTab, .weaponsTab, .setsTab,
--                                  .setBuilderTab; .collAllBtn, .collectedBtn,
--                                  .notCollectedBtn, .allItemsBtn;
--                                  .weaponCategoryLabel, .weaponCategoryDropdown,
--                                  .armorSlotLabel, .armorSlotDropdown)
--   owner.categoryMode            ("armor" / "weapons" / "sets" / "setbuilder")
--
-- Owner methods called (delegators in main file):
--   owner:UpdateWeaponCategoryUI()  -- self-call from UpdateWeaponTypeFilter alias
--
-- Globals/Blizzard:
--   ETBDropdown (NS-stored; resolved at call time)
--   ETB.Filtering, ETB.CATEGORY_INFO
-- =============================================================================

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

NS.ETB.MainFrame = NS.ETB.MainFrame or ETB.MainFrame or {}
local Filters = {}
NS.ETB.MainFrame.Filters = Filters

-- =============================================================================
-- CATEGORY TABS — top-row tab highlighting
-- =============================================================================

function Filters:UpdateCategoryTabs(owner)
    local f = owner.frame
    if not f then return end

    local tabs = { armor = f.armorTab, weapons = f.weaponsTab, sets = f.setsTab, setbuilder = f.setBuilderTab }
    for mode, tab in pairs(tabs) do
        if tab then
            if mode == owner.categoryMode then
                tab.bg:SetColorTexture(0.1, 0.4, 0.1, 0.9)
                tab.text:SetTextColor(0, 1, 0)
            else
                tab.bg:SetColorTexture(0.2, 0.2, 0.2, 0.8)
                tab.text:SetTextColor(1, 1, 1)
            end
        end
    end
end

-- =============================================================================
-- COLLECTION BUTTONS — All / Collected / Not Collected coloring
-- =============================================================================

function Filters:UpdateCollectionButtons(owner, filter)
    local f = owner.frame
    if not f then return end

    filter = filter or ETB.Filtering.filters.collected or "all"

    -- Reset all buttons to default
    if f.collAllBtn then
        f.collAllBtn:SetText(filter == "all" and "|cff00ffff» All «|r" or "» All «")
    end
    if f.collectedBtn then
        f.collectedBtn:SetText(filter == "collected" and "|cff00ff00Collected|r" or "Collected")
    end
    if f.notCollectedBtn then
        f.notCollectedBtn:SetText(filter == "uncollected" and "|cffff6666Not Collected|r" or "Not Collected")
    end

    -- Legacy support for old button names
    if f.allItemsBtn then
        f.allItemsBtn:SetText(filter == "all" and "|cffffffff» All «|r" or "All Items")
    end
end

-- =============================================================================
-- WEAPON / ARMOR CATEGORY UI VISIBILITY
-- =============================================================================

function Filters:UpdateWeaponTypeFilter(owner)
    -- Backward-compatible alias for UpdateWeaponCategoryUI
    owner:UpdateWeaponCategoryUI()
end

function Filters:UpdateWeaponCategoryUI(owner)
    local ETBDropdown = NS.ETBDropdown  -- Resolved at call time

    local f = owner.frame
    if not f then return end

    -- Show/hide appropriate category selector based on mode
    if owner.categoryMode == "weapons" then
        -- Weapon category selected via icon buttons at top - no dropdown needed
        if f.weaponCategoryLabel then f.weaponCategoryLabel:Hide() end
        if f.weaponCategoryDropdown then f.weaponCategoryDropdown:Hide() end
        if f.armorSlotLabel then f.armorSlotLabel:Hide() end
        if f.armorSlotDropdown then f.armorSlotDropdown:Hide() end

        -- Update dropdown text to show current category
        local categoryID = ETB.Filtering.filters.category
        local catInfo = ETB.CATEGORY_INFO and ETB.CATEGORY_INFO[categoryID]
        if catInfo and f.weaponCategoryDropdown then
            ETBDropdown:SetText(f.weaponCategoryDropdown, catInfo.name)
        end
    else
        -- Show armor slot dropdown, hide weapon category dropdown
        if f.armorSlotLabel then f.armorSlotLabel:Show() end
        if f.armorSlotDropdown then f.armorSlotDropdown:Show() end
        if f.weaponCategoryLabel then f.weaponCategoryLabel:Hide() end
        if f.weaponCategoryDropdown then f.weaponCategoryDropdown:Hide() end

        -- Update dropdown text to show current slot
        local categoryID = ETB.Filtering.filters.category
        local catInfo = ETB.CATEGORY_INFO and ETB.CATEGORY_INFO[categoryID]
        if catInfo and f.armorSlotDropdown then
            ETBDropdown:SetText(f.armorSlotDropdown, catInfo.name)
        end
    end
end
