-- ============================================================
-- Decor Vendor
-- UI/MainHomePanel.lua
-- Main dashboard/home screen for the normal addon frame
-- ============================================================

local addonName, dv = ...

local CARD_BACKDROP = {
    bgFile = "Interface\\Buttons\\WHITE8X8",
    edgeFile = "Interface\\Buttons\\WHITE8X8",
    edgeSize = 1,
}

function dv:CreateMainHomePanel(parent)
    if parent.homePanel then
        return parent.homePanel
    end

    local panel = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    panel:SetPoint("TOPLEFT", parent, "TOPLEFT", 18, -46)
    panel:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", -18, 52)
    panel:SetFrameLevel(parent:GetFrameLevel() + 80)
    panel:SetBackdrop(CARD_BACKDROP)
    panel:SetBackdropColor(0.02, 0.018, 0.035, 1)
    panel:SetBackdropBorderColor(0.42, 0.26, 0.70, 1)

    parent.homePanel = panel

    -------------------------------------------------
    -- Header
    -------------------------------------------------

    local header = CreateFrame("Frame", nil, panel, "BackdropTemplate")
    header:SetHeight(58)
    header:SetPoint("TOPLEFT", panel, "TOPLEFT", 10, -10)
    header:SetPoint("TOPRIGHT", panel, "TOPRIGHT", -10, -10)
    header:SetBackdrop(CARD_BACKDROP)
    header:SetBackdropColor(0.09, 0.08, 0.14, 0.95)
    header:SetBackdropBorderColor(0.35, 0.25, 0.65, 1)

    local title = header:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    title:SetPoint("LEFT", header, "LEFT", 18, 0)
    title:SetText("|cffffd100Decor Vendor|r")

    local subtitle = header:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    subtitle:SetPoint("RIGHT", header, "RIGHT", -18, 0)
    subtitle:SetText("Choose a section to explore decor sources, tracking, and previews.")

    -------------------------------------------------
    -- Open section helper (FIXED FOR STATISTICS)
    -------------------------------------------------
local function OpenSection(sectionKey)
        -- 🚀 THE VISIBILITY OVERRIDE:
        if sectionKey == "statistics" then
            if dv.StatsPanel then
                panel:Hide() -- Hides the landing grid tiles completely!
                if dv.sidebar then dv.sidebar:Hide() end -- Ensure sidebar doesn't bleed through
                dv.StatsPanel:Show() -- Triggers our progress tracking engine to load
            else
                print("|cffff5555Decor Vendor:|r StatsPanel loader is missing. Ensure StatsPanel.lua is registered.")
            end
            return
        end

        panel:Hide()
        -- (Keep all your other gallery/vendors section keys right below here unchanged)

        if sectionKey == "gallery" then
            if dv.OpenGalleryAddon then
                dv.OpenGalleryAddon()
            else
                print("|cffff5555Decor Vendor:|r Gallery loader is missing.")
            end
            return
        end

        if dv.OpenMainSection then
            dv.OpenMainSection(sectionKey, true)
            return
        end

        if dv.tabButtons and dv.tabButtons[sectionKey] then
            dv.tabButtons[sectionKey]:Click()
            return
        end

        if dv.bottomTabs then
            for _, tab in ipairs(dv.bottomTabs) do
                if tab.id == sectionKey then
                    tab:Click()
                    return
                end
            end
        end

        print("|cffff5555Decor Vendor:|r No section handler found for " .. tostring(sectionKey))
    end

    -------------------------------------------------
    -- Cards (EXPANDED FOR DASHBOARD TRACKER)
    -------------------------------------------------
    local cards = {
        {
            key = "vendors",
            title = "Vendors",
            color = {0.35, 0.75, 1.00},
            desc = "Browse original decor vendors, waypoint support, and vendor locations.",
        },
        {
            key = "professions",
            title = "Professions",
            color = {0.55, 1.00, 0.70},
            desc = "View crafted decor, recipes, materials, and profession requirements.",
        },
        {
            key = "quests",
            title = "Quests",
            color = {1.00, 0.82, 0.10},
            desc = "Track decor earned from quests and quest-related unlocks.",
        },
        {
            key = "achievements",
            title = "Achievements",
            color = {0.75, 0.45, 1.00},
            desc = "Find decor tied to achievements and collection progress.",
        },
        {
            key = "bossdrops",
            title = "Boss Drops",
            color = {1.00, 0.45, 0.45},
            desc = "Browse decor from bosses, rares, treasures, events, and drops.",
        },
        {
            key = "gallery",
            title = "Gallery Browser",
            color = {1.00, 0.35, 0.85},
            desc = "Preview decor in 3D, compare sources, and browse the full collection.",
        },
        -- 🌟 7. BRAND NEW SYSTEM COMPLETION STATISTICS CARD CARD
        {
            key = "statistics",
            title = "Collection Statistics",
            color = {0.20, 1.00, 0.50}, -- Sleek interactive neon tracking green accent strip
            desc = "Review your progress milestones, account completion metrics, and graphs.",
        },
    }

    local cardWidth = 300
    local cardHeight = 112
    local gapX = 14
    local gapY = 18
    local startY = -110

    local cardFrames = {}

    for i, info in ipairs(cards) do
        local card = CreateFrame("Button", nil, panel, "BackdropTemplate")
        card:SetSize(cardWidth, cardHeight)
        table.insert(cardFrames, card)
        card:SetBackdrop(CARD_BACKDROP)
        card:SetBackdropColor(0.10, 0.10, 0.16, 0.94)
        card:SetBackdropBorderColor(0.30, 0.22, 0.55, 1)

        local strip = card:CreateTexture(nil, "ARTWORK")
        strip:SetHeight(3)
        strip:SetPoint("TOPLEFT", card, "TOPLEFT", 0, 0)
        strip:SetPoint("TOPRIGHT", card, "TOPRIGHT", 0, 0)
        strip:SetColorTexture(info.color[1], info.color[2], info.color[3], 1)

        local cardTitle = card:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        cardTitle:SetPoint("TOPLEFT", card, "TOPLEFT", 12, -14)
        cardTitle:SetText(info.title)
        cardTitle:SetTextColor(info.color[1], info.color[2], info.color[3], 1)

        local desc = card:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        desc:SetPoint("TOPLEFT", cardTitle, "BOTTOMLEFT", 0, -8)
        desc:SetPoint("RIGHT", card, "RIGHT", -12, 0)
        desc:SetJustifyH("LEFT")
        desc:SetJustifyV("TOP")
        desc:SetText(info.desc)

        card:SetScript("OnEnter", function(self)
            self:SetBackdropBorderColor(info.color[1], info.color[2], info.color[3], 1)
        end)

        card:SetScript("OnLeave", function(self)
            self:SetBackdropBorderColor(0.30, 0.22, 0.55, 1)
        end)

        card:SetScript("OnClick", function()
            OpenSection(info.key)
        end)
    end

    local function LayoutHomeCards()
        local columns = 3
        local panelWidth = panel:GetWidth()

        if not panelWidth or panelWidth <= 0 then
            return
        end

        local totalCardsWidth = (cardWidth * columns) + (gapX * (columns - 1))
        local startX = math.floor((panelWidth - totalCardsWidth) / 2)

        if startX < 10 then
            startX = 10
        end

        for i, card in ipairs(cardFrames) do
            local col = (i - 1) % columns
            local row = math.floor((i - 1) / columns)

            card:ClearAllPoints()
            card:SetPoint(
                "TOPLEFT",
                panel,
                "TOPLEFT",
                startX + (cardWidth + gapX) * col,
                startY - (cardHeight + gapY) * row
            )
        end
    end

    panel:HookScript("OnSizeChanged", LayoutHomeCards)
    C_Timer.After(0, LayoutHomeCards)

    -------------------------------------------------
    -- Footer
    -------------------------------------------------
local footerLine1 = panel:CreateFontString(nil, "OVERLAY")
    footerLine1:SetFont(STANDARD_TEXT_FONT, 14, "OUTLINE")
    footerLine1:SetPoint("BOTTOM", panel, "BOTTOM", 0, 46) -- Shifted up slightly to fit line 3 perfectly
    footerLine1:SetText("|cffffd100Tip:|r |cfff5ebd1Choose a section above. Use the Home button to return here anytime.|r")
    footerLine1:SetJustifyH("CENTER")

    local footerLine2 = panel:CreateFontString(nil, "OVERLAY")
    footerLine2:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE") 
    footerLine2:SetPoint("TOP", footerLine1, "BOTTOM", 0, -4)
    footerLine2:SetText("|cffde9b6bNote: The first load of the Gallery may take a moment while syncing with the game catalogue.|r")
    footerLine2:SetJustifyH("CENTER")

    -- 🚀 🌟 THE NEW PRE-PATCH MAINTENANCE WARNING LINE (BRIGHT RED)
    local footerLine3 = panel:CreateFontString(nil, "OVERLAY")
    footerLine3:SetFont(STANDARD_TEXT_FONT, 11, "OUTLINE") -- Slightly smaller font size so it sits neatly at the very bottom
    footerLine3:SetPoint("TOP", footerLine2, "BOTTOM", 0, -4)
    footerLine3:SetText("|cffff4040Patch has gone live currently going through some decor that are missing in game 3d Models.|r")
    footerLine3:SetJustifyH("CENTER")
end

function dv.ShowMainHomePanel()
    local frame = dv.frame

    if not frame then
        return
    end

    if not frame.homePanel and dv.CreateMainHomePanel then
        dv:CreateMainHomePanel(frame)
    end

    if dv.sidebar then
        dv.sidebar:Hide()
    end

    if frame.homeHint then
        frame.homeHint:Hide()
    end

    if dv.contentArea then
        dv.contentArea:Hide()
    end

    -- Ensure our custom stats frame layout drops back out of view when home pops up!
    if dv.StatsPanel then
        dv.StatsPanel:Hide()
    end

    if dv.tabBar then
        dv.tabBar:Hide()
    end

    if _G.DV_SearchBox then
        _G.DV_SearchBox:Hide()
    end

    if _G.DV_ResetProgressBtn then
        _G.DV_ResetProgressBtn:Hide()
    end

    if frame.homePanel then
        frame.homePanel:Show()
        frame.homePanel:SetFrameLevel(frame:GetFrameLevel() + 200)
    end
end