-- ============================================================
-- 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
    -------------------------------------------------

local function OpenSection(sectionKey)
    panel:Hide()

    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
    -------------------------------------------------

    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.",
        },
    }

	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 col = (i - 1) % 3
        local row = math.floor((i - 1) / 3)

        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 footer = panel:CreateFontString(nil, "OVERLAY")
footer:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE")
footer:SetPoint("BOTTOM", panel, "BOTTOM", 0, 14)
footer:SetText("|cffffd100Tip:|r Choose a section above. Use the Home button to return here anytime.")
footer: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

    -- Hide normal section UI while Home is showing.
    if dv.sidebar then
        dv.sidebar:Hide()
    end

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

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

    -- Bottom tabs stay hidden.
    if dv.tabBar then
        dv.tabBar:Hide()
    end

    -- Optional: hide section-only header controls on Home.
    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