-- =============================================================================
-- ETB v12.58.0 — UI/MainFrame/Views.lua
-- =============================================================================
-- First MainFrame submodule. Owns view-switching: shuffling the main window
-- between the regular item grid, the SetsUI panel, and the SetBuilder panel
-- (both standalone and embedded modes).
--
-- This is the smallest, most contained MainFrame cluster (7 methods,
-- ~115 lines, low coupling). Picked first to validate the submodule
-- pattern works for MainFrame before tackling the larger clusters
-- (UI construction, item grid, live preview).
--
-- Public API (all take owner = MainFrame singleton as first arg):
--   Views:ShowSetsView(owner)
--   Views:HideSetsView(owner)
--   Views:ShowSetBuilderView(owner)
--   Views:HideSetBuilderView(owner)
--   Views:ShowSetBuilderEmbedded(owner)
--   Views:HideSetBuilderEmbedded(owner)
--   Views:IsSetBuilderEmbeddedShown(owner)
--
-- Owner contract — fields read/written:
--   owner.frame                       (main frame, with .gridFrame, .preview,
--                                      .sidebar, .setBuilderEmbedContainer)
--   owner.setsContainer               (created lazily on first ShowSetsView)
--   owner.setBuilderContainer         (created lazily on first ShowSetBuilderView)
--
-- Owner-exposed file-locals:
--   owner.FRAME_MARGIN
--   owner.BOTTOM_MARGIN
--
-- Owner methods called (delegators in main file):
--   owner:UpdateCurrentViewingLabel(text)
--   owner:HideSetBuilderView()
--   owner:HideSetBuilderEmbedded()
--
-- Globals/Blizzard:
--   CreateFrame
--   ETB.SetsUI (Show, Hide)
--   ETB.SetBuilder (ShowAsPanel, HidePanel, ShowEmbedded, HideEmbedded)
-- =============================================================================

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

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

-- =============================================================================
-- SETS VIEW
-- =============================================================================

function Views:ShowSetsView(owner)
    if not owner.frame then return end

    -- Hide Set Builder panel when showing Sets
    owner:HideSetBuilderView()

    -- Hide the regular items grid
    if owner.frame.gridFrame then
        owner.frame.gridFrame:Hide()
    end

    -- Hide live preview when in sets mode
    if owner.frame.preview then
        owner.frame.preview:Hide()
    end

    -- Hide Set Builder container
    if owner.setBuilderContainer then
        owner.setBuilderContainer:Hide()
    end

    -- Show the sets UI
    if ETB.SetsUI then
        if not owner.setsContainer then
            owner.setsContainer = CreateFrame("Frame", nil, owner.frame)
            owner.setsContainer:SetPoint("TOPLEFT", owner.frame.sidebar, "TOPRIGHT", 8, 0)
            owner.setsContainer:SetPoint("BOTTOMRIGHT", owner.frame, "BOTTOMRIGHT", -owner.FRAME_MARGIN, owner.BOTTOM_MARGIN)
        end
        owner.setsContainer:Show()
        ETB.SetsUI:Show(owner.setsContainer)
    else
        ETB:Print("|cffff0000SetsUI module not loaded!|r")
    end

    owner:UpdateCurrentViewingLabel("Transmog Sets")
end

function Views:HideSetsView(owner)
    if ETB.SetsUI then
        ETB.SetsUI:Hide()
    end

    if owner.setsContainer then
        owner.setsContainer:Hide()
    end

    owner:HideSetBuilderView()
    owner:HideSetBuilderEmbedded()

    if owner.frame and owner.frame.gridFrame then
        owner.frame.gridFrame:Show()
    end

    if owner.frame and owner.frame.preview then
        owner.frame.preview:Show()
    end
end

-- =============================================================================
-- SET BUILDER PANEL VIEW (standalone, swaps with item grid)
-- =============================================================================

function Views:ShowSetBuilderView(owner)
    if not owner.frame then return end

    -- Hide sets UI and item grid
    if ETB.SetsUI then ETB.SetsUI:Hide() end
    if owner.setsContainer then owner.setsContainer:Hide() end
    if owner.frame.gridFrame then owner.frame.gridFrame:Hide() end
    if owner.frame.preview then owner.frame.preview:Hide() end

    -- Create and show Set Builder container (same area as Sets)
    if not owner.setBuilderContainer then
        owner.setBuilderContainer = CreateFrame("Frame", nil, owner.frame)
        owner.setBuilderContainer:SetPoint("TOPLEFT", owner.frame.sidebar, "TOPRIGHT", 8, 0)
        owner.setBuilderContainer:SetPoint("BOTTOMRIGHT", owner.frame, "BOTTOMRIGHT", -owner.FRAME_MARGIN, owner.BOTTOM_MARGIN)
    end
    owner.setBuilderContainer:Show()

    if ETB.SetBuilder and ETB.SetBuilder.ShowAsPanel then
        ETB.SetBuilder:ShowAsPanel(owner.setBuilderContainer)
    end

    owner:UpdateCurrentViewingLabel("Set Builder")
end

function Views:HideSetBuilderView(owner)
    if owner.setBuilderContainer then
        owner.setBuilderContainer:Hide()
    end
    if ETB.SetBuilder and ETB.SetBuilder.HidePanel then
        ETB.SetBuilder:HidePanel()
    end
end

-- =============================================================================
-- SET BUILDER EMBEDDED VIEW (sidebar slide-out)
-- =============================================================================

function Views:ShowSetBuilderEmbedded(owner)
    local f = owner.frame
    if not f or not f.setBuilderEmbedContainer then return end
    f.setBuilderEmbedContainer:Show()
    if ETB.SetBuilder and ETB.SetBuilder.ShowEmbedded then
        ETB.SetBuilder:ShowEmbedded(f.setBuilderEmbedContainer)
    end
end

function Views:HideSetBuilderEmbedded(owner)
    local f = owner.frame
    if not f or not f.setBuilderEmbedContainer then return end
    f.setBuilderEmbedContainer:Hide()
    if ETB.SetBuilder and ETB.SetBuilder.HideEmbedded then
        ETB.SetBuilder:HideEmbedded()
    end
end

function Views:IsSetBuilderEmbeddedShown(owner)
    local f = owner.frame
    return f and f.setBuilderEmbedContainer and f.setBuilderEmbedContainer:IsShown()
end
