-- Enhanced Raid Browser — MainFrame v5
-- Shows top 7 items per slot per phase with rank numbers
local ADDON_NAME, NS = ...
local ETB = NS.ETB
local ERB = ETB.ERB

-- v15.6.1: same namespace-wipe pattern as BlizzardData.lua. The three
-- submodules (Construction, Selection, Display) load before this file per
-- the TOC and attach themselves at ERB.MainFrame.X. A plain
-- `ERB.MainFrame = MF` would discard them.
--
-- Use TransmogUI.lua's established merge-prior-fields pattern: any submodule
-- registrations already on ERB.MainFrame get carried over into MF.
local MF = {}
local prior = ERB.MainFrame
if type(prior) == "table" then
    for k, v in pairs(prior) do
        if MF[k] == nil then
            MF[k] = v
        end
    end
end
ERB.MainFrame = MF

MF.frame = nil
MF.itemRows = {}
MF.slotButtons = {}
MF.phaseTabs = {}
MF.detailLines = {}

-- ============================================================================
-- CONSTANTS
-- ============================================================================

local FRAME_WIDTH = 1100
local FRAME_HEIGHT = 750
local SIDEBAR_WIDTH = 170
local DETAIL_WIDTH = 300
local FRAME_MARGIN = 15
local TITLE_HEIGHT = 45
local TOPBAR_HEIGHT = 50
local BOTTOMBAR_HEIGHT = 10
local CONTENT_Y = -(TITLE_HEIGHT + TOPBAR_HEIGHT + 5)
local BOTTOM_MARGIN = FRAME_MARGIN + BOTTOMBAR_HEIGHT + 5
local ITEM_ROW_HEIGHT = 36
local MAX_ITEMS = 7

local SLOT_ICONS = {
    Head="Interface\\PaperDoll\\UI-PaperDoll-Slot-Head",
    Neck="Interface\\PaperDoll\\UI-PaperDoll-Slot-Neck",
    Shoulder="Interface\\PaperDoll\\UI-PaperDoll-Slot-Shoulder",
    Back="Interface\\PaperDoll\\UI-PaperDoll-Slot-Chest",
    Chest="Interface\\PaperDoll\\UI-PaperDoll-Slot-Chest",
    Wrist="Interface\\PaperDoll\\UI-PaperDoll-Slot-Wrists",
    Hands="Interface\\PaperDoll\\UI-PaperDoll-Slot-Hands",
    Waist="Interface\\PaperDoll\\UI-PaperDoll-Slot-Waist",
    Legs="Interface\\PaperDoll\\UI-PaperDoll-Slot-Legs",
    Feet="Interface\\PaperDoll\\UI-PaperDoll-Slot-Feet",
    Finger="Interface\\PaperDoll\\UI-PaperDoll-Slot-Finger",
    Trinket="Interface\\PaperDoll\\UI-PaperDoll-Slot-Trinket",
    ["Main Hand"]="Interface\\PaperDoll\\UI-PaperDoll-Slot-MainHand",
    ["Off Hand"]="Interface\\PaperDoll\\UI-PaperDoll-Slot-SecondaryHand",
    ["Two Hand"]="Interface\\PaperDoll\\UI-PaperDoll-Slot-MainHand",
    Ranged="Interface\\PaperDoll\\UI-PaperDoll-Slot-Ranged",
    Weapon="Interface\\PaperDoll\\UI-PaperDoll-Slot-MainHand",
}

local RANK_COLORS = {
    [1] = "|cffff8000",  -- orange BiS
    [2] = "|cffa335ee",  -- purple
    [3] = "|cff0070dd",  -- blue
    [4] = "|cff1eff00",  -- green
    [5] = "|cff888888",  -- gray
    [6] = "|cff888888",  -- gray
    [7] = "|cff888888",  -- gray
}

-- v15.3.0: expose file-local constants on MF so submodule methods can read
-- them via owner.X after extraction (audit Batch 4 Part 3). The locals
-- stay in this file as the canonical declarations; these are accessor
-- aliases for the submodules. Pattern matches the v14.11+ TransmogUI
-- exposures and the v15.1+/v15.2+ BLT/BlizzardData exposures.
MF.FRAME_WIDTH      = FRAME_WIDTH
MF.FRAME_HEIGHT     = FRAME_HEIGHT
MF.SIDEBAR_WIDTH    = SIDEBAR_WIDTH
MF.DETAIL_WIDTH     = DETAIL_WIDTH
MF.FRAME_MARGIN     = FRAME_MARGIN
MF.TITLE_HEIGHT     = TITLE_HEIGHT
MF.TOPBAR_HEIGHT    = TOPBAR_HEIGHT
MF.BOTTOMBAR_HEIGHT = BOTTOMBAR_HEIGHT
MF.CONTENT_Y        = CONTENT_Y
MF.BOTTOM_MARGIN    = BOTTOM_MARGIN
MF.ITEM_ROW_HEIGHT  = ITEM_ROW_HEIGHT
MF.MAX_ITEMS        = MAX_ITEMS
MF.SLOT_ICONS       = SLOT_ICONS
MF.RANK_COLORS      = RANK_COLORS

-- ============================================================================
-- ITEM OWNERSHIP
-- ============================================================================

-- v15.7.4: Use MoP 5.4.8 global API form, not the C_Container namespace
-- (which is a Dragonflight 10.0.2+ addition). MoP Classic exposes
-- GetContainerNumSlots/GetContainerItemID as plain globals.
-- Defensive nil-checks match the polyfill pattern used by BagOverlay.lua,
-- in case a future build flips the namespace.
local _GetContainerNumSlots = (C_Container and C_Container.GetContainerNumSlots) or _G.GetContainerNumSlots
local _GetContainerItemID   = (C_Container and C_Container.GetContainerItemID)   or _G.GetContainerItemID

local function PlayerOwnsItem(itemID)
    for slot = 1, 18 do
        if GetInventoryItemID("player", slot) == itemID then return true end
    end
    if _GetContainerNumSlots and _GetContainerItemID then
        for bag = 0, NUM_BAG_SLOTS do
            local n = _GetContainerNumSlots(bag) or 0
            for slot = 1, n do
                if _GetContainerItemID(bag, slot) == itemID then return true end
            end
        end
    end
    return false
end

-- v15.3.0: expose for Display.lua submodule access (audit Batch 4 Part 3)
MF.PlayerOwnsItem = PlayerOwnsItem

-- ============================================================================
-- CREATE MAIN FRAME
-- ============================================================================

function MF:Create()
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Construction
    return NS.ETB.ERB.MainFrame.Construction:Create(self)
end

-- ============================================================================
-- TOP BAR — Spec, source toggle, phase tabs
-- ============================================================================

function MF:CreateTopBar(f)
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Construction
    return NS.ETB.ERB.MainFrame.Construction:CreateTopBar(self, f)
end

-- ============================================================================
-- LEFT SIDEBAR — Slot buttons
-- ============================================================================

function MF:CreateLeftSidebar(f)
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Construction
    return NS.ETB.ERB.MainFrame.Construction:CreateLeftSidebar(self, f)
end

-- ============================================================================
-- ITEM GRID — shows items for selected slot, ranked with #1 - #6
-- ============================================================================

function MF:CreateItemGrid(f)
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Construction
    return NS.ETB.ERB.MainFrame.Construction:CreateItemGrid(self, f)
end

-- ============================================================================
-- ITEM ROW — individual ranked item
-- ============================================================================

function MF:CreateItemRow(parent, index)
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Construction
    return NS.ETB.ERB.MainFrame.Construction:CreateItemRow(self, parent, index)
end

-- ============================================================================
-- DETAIL PANEL (right side)
-- ============================================================================

function MF:CreateDetailPanel(f)
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Construction
    return NS.ETB.ERB.MainFrame.Construction:CreateDetailPanel(self, f)
end

-- ============================================================================
-- SLOT SELECTION
-- ============================================================================

function MF:SelectSlot(slot)
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Selection
    return NS.ETB.ERB.MainFrame.Selection:SelectSlot(self, slot)
end

-- ============================================================================
-- SPEC SELECTOR BUTTONS
-- ============================================================================

MF.specButtons = {}

function MF:UpdateSpecButtons()
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Selection
    return NS.ETB.ERB.MainFrame.Selection:UpdateSpecButtons(self)
end

-- ============================================================================
-- UPDATE PHASE TABS
-- ============================================================================

function MF:UpdatePhaseTabs()
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Selection
    return NS.ETB.ERB.MainFrame.Selection:UpdatePhaseTabs(self)
end

-- ============================================================================
-- UPDATE SIDEBAR COUNTS
-- ============================================================================

function MF:UpdateSlotCounts()
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Selection
    return NS.ETB.ERB.MainFrame.Selection:UpdateSlotCounts(self)
end

-- ============================================================================
-- UPDATE DISPLAY — ranked items for current slot
-- ============================================================================

function MF:UpdateDisplay()
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Display
    return NS.ETB.ERB.MainFrame.Display:UpdateDisplay(self)
end

-- ============================================================================
-- ITEM DETAIL PANEL
-- ============================================================================

function MF:ShowItemDetail(itemID, itemData)
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Display
    return NS.ETB.ERB.MainFrame.Display:ShowItemDetail(self, itemID, itemData)
end

function MF:AddDetailLine(parent, yOff, text, font)
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Display
    return NS.ETB.ERB.MainFrame.Display:AddDetailLine(self, parent, yOff, text, font)
end

-- ============================================================================
-- REFRESH
-- ============================================================================

function MF:Refresh()
    -- v15.3.0: implementation lives in NS.ETB.ERB.MainFrame.Display
    return NS.ETB.ERB.MainFrame.Display:Refresh(self)
end

-- ============================================================================
-- TOGGLE
-- ============================================================================

function MF:Toggle()
    if not self.frame then self:Create() end
    if self.frame:IsShown() then
        self.frame:Hide()
    else
        self:Refresh()
        self:SelectSlot(ERB.DataBridge and ERB.DataBridge.currentSlot or "Head")
        self.frame:Show()
    end
end

function MF:IsShown()
    return self.frame and self.frame:IsShown()
end
