local addonName, ns = ...

------------------------------------------------------------
-- Main window: themed shell with News / Shopping List tabs.
------------------------------------------------------------

ns.MAIN_WIDTH = 600
ns.MAIN_HEIGHT = 520

local mainFrame = CreateFrame("Frame", "HousingHeraldFrame", UIParent, "BackdropTemplate")
mainFrame:SetSize(ns.MAIN_WIDTH, ns.MAIN_HEIGHT)
mainFrame:SetPoint("CENTER")
mainFrame:SetFrameStrata("HIGH")
mainFrame:SetMovable(true)
mainFrame:EnableMouse(true)
mainFrame:RegisterForDrag("LeftButton")
mainFrame:SetScript("OnDragStart", mainFrame.StartMoving)
mainFrame:SetScript("OnDragStop", mainFrame.StopMovingOrSizing)
mainFrame:SetClampedToScreen(true)
-- The dialogs (dye picker, import, export/copy, help) belong to this
-- window; whatever closes it must take them along. OnHide catches every
-- close path -- the close button, the /hh toggle, future callers.
mainFrame:SetScript("OnHide", function() ns.HideAllPopups() end)
mainFrame:Hide()

mainFrame:SetBackdrop({
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
    edgeSize = 14,
    insets = { left = 3, right = 3, top = 3, bottom = 3 },
})
mainFrame:SetBackdropColor(ns.Theme.BG:GetRGBA())
mainFrame:SetBackdropBorderColor(ns.Theme.BORDER:GetRGBA())
ns.mainFrame = mainFrame

local title = mainFrame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
title:SetPoint("TOP", 0, -12)
title:SetText("Housing Herald")
title:SetTextColor(ns.Theme.HEADING:GetRGBA())

ns.CreateThemedCloseButton(mainFrame)

------------------------------------------------------------
-- Tabs
------------------------------------------------------------

local tabs = {}
local selectedTab

local function ApplyTabStyles()
    for name, tab in pairs(tabs) do
        if name == selectedTab then
            tab.panel:Show()
            tab.button:SetAlpha(1)
        else
            tab.panel:Hide()
            tab.button:SetAlpha(0.55)
        end
    end
end

function ns:SelectTab(name)
    if not tabs[name] then return end
    selectedTab = name
    ApplyTabStyles()
end

local function CreateTab(name, label, anchorButton)
    local button = ns.CreateThemedButton(mainFrame, label)
    button:SetSize(130, 24)
    if anchorButton then
        button:SetPoint("LEFT", anchorButton, "RIGHT", 8, 0)
    else
        button:SetPoint("TOPLEFT", 12, -40)
    end
    button:SetScript("OnClick", function()
        ns:SelectTab(name)
    end)

    local panel = CreateFrame("Frame", nil, mainFrame)
    panel:SetPoint("TOPLEFT", 12, -74)
    panel:SetPoint("BOTTOMRIGHT", -12, 12)
    panel:Hide()

    tabs[name] = { button = button, panel = panel }
    return button, panel
end

local newsButton, newsPanel = CreateTab("news", "Articles")
local shoppingButton, shoppingPanel = CreateTab("shopping", "Shopping List", newsButton)
local _, whatsnewPanel = CreateTab("whatsnew", "What's New", shoppingButton)
ns.newsPanel = newsPanel
ns.shoppingPanel = shoppingPanel
ns.whatsnewPanel = whatsnewPanel

------------------------------------------------------------
-- Show / hide
------------------------------------------------------------

function ns:ShowMainFrame(tabName)
    mainFrame:Show()
    ns:SelectTab(tabName or selectedTab or "news")
    if ns.RefreshNewsTab then ns:RefreshNewsTab() end
    if ns.RefreshShoppingTab then ns:RefreshShoppingTab() end
    if ns.RefreshWhatsNewTab then ns:RefreshWhatsNewTab() end
end

function ns:ToggleMainFrame()
    if mainFrame:IsShown() then
        mainFrame:Hide()
    else
        ns:ShowMainFrame()
    end
end
