-- =========================================================================
-- FILE: UI_Home.lua (TBC)
-- PURPOSE: Home view layout wrapper
-- =========================================================================
local addonName, NS = ...
local GR = NS.GR
local CreateBg = NS.CreateBg
local CreateBorder = NS.CreateBorder

-- =========================================================================
-- UI: HOME VIEW
-- =========================================================================

function GR:BuildHomeView()
    local parent = CreateFrame("Frame", "GRS_HomeView", self.ViewContainer)
    parent:SetPoint("TOPLEFT", 0, -30) 
    parent:SetPoint("BOTTOMRIGHT", 0, 0)
    self.views[1] = parent

    local outerScroll = CreateFrame("ScrollFrame", "GRS_OuterScroll", parent, "UIPanelScrollFrameTemplate")
    outerScroll:SetPoint("TOPLEFT", 0, 0)
    outerScroll:SetPoint("BOTTOMRIGHT", -28, 25) 
    
    if outerScroll.ScrollBar then outerScroll.ScrollBar:Hide(); outerScroll.ScrollBar:SetWidth(0) end
    
    local content = CreateFrame("Frame", "GRS_ScrollChild", outerScroll)
    outerScroll:SetScrollChild(content)
    
    local left = CreateFrame("Frame", nil, content)
    left:SetWidth(320); left:SetPoint("TOPLEFT"); left:SetPoint("BOTTOMLEFT")
    CreateBg(left, 0.1, 0.1, 0.1, 0.5); CreateBorder(left, 0.2, 0.2, 0.2, 1)
    self:BuildLeftPane(left)

    local rightPane = CreateFrame("Frame", "GRS_RightPane", content)
    rightPane:SetWidth(500)
    -- Anchor right pane to start AFTER the fixed-width list area (left pane 320 + list 760 + 45 = 1125px) (1:1 Retail Parity)
    rightPane:SetPoint("TOPLEFT", 1125, 0); rightPane:SetPoint("BOTTOMLEFT", 1125, 0)
    CreateBg(rightPane, 0.05, 0.05, 0.05, 0.8)
    CreateBorder(rightPane, 0.2, 0.2, 0.2, 1)
    self:BuildWhisperPane(rightPane)

    local center = CreateFrame("Frame", "GRS_CenterPane", content)
    center:SetPoint("TOPLEFT", left, "TOPRIGHT")
    center:SetPoint("BOTTOMLEFT", left, "BOTTOMRIGHT")
    center:SetWidth(760) -- Fixed width matching the list headers (1:1 Retail Parity)
    self:BuildCenterPane(center)
    
    if self.BuildFilterUI then self:BuildFilterUI() end
    
    parent:SetScript("OnSizeChanged", function(self, w, h)
        local scrollW = w - 52
        local scrollH = h - 45
        
        -- [[ STATIC LAYOUT LOGIC ]] (1:1 Retail Parity)
        -- To achieve the "sliding door" effect where elements do not move/compress but simply get covered,
        -- we must enforce a hard minimum width for the content frame that represents the "Full Open" state.
        local leftPaneW = 320
        local rightPaneW = 500 
        local centerPaneW = 610 -- Minimum width to prevent content compression
        
        local fullLayoutW = leftPaneW + centerPaneW + rightPaneW
        
        local finalContentW = math.max(scrollW, fullLayoutW)
        local finalH = math.max(scrollH, 1)
        
        content:SetWidth(finalContentW)
        content:SetHeight(finalH)
        
        local listAvailable = finalH - 115
        if listAvailable < 20 then listAvailable = 20 end

        local whisperAvailable = finalH - 50
        if whisperAvailable < 20 then whisperAvailable = 20 end

        if GR.UpdateResultsLayout then GR:UpdateResultsLayout(listAvailable) end
        if GR.UpdateWhisperLayout then GR:UpdateWhisperLayout(whisperAvailable) end
    end)
    
    GR.UpdateHomeLayout = function()
        local w, h = parent:GetSize()
        parent:GetScript("OnSizeChanged")(parent, w, h)
    end
end
