--[[
============================================================
Decor Vendor Addon
© 2026 MidniteDestiny. All Rights Reserved.
============================================================

This file is part of the Decor Vendor addon.

All code, structure, and design are the intellectual
property of MidniteDestiny unless otherwise stated.

You may NOT:
• Copy, reproduce, or redistribute this code
• Modify and redistribute this code
• Use this code in other addons or projects

without explicit permission from the author.

This addon is distributed for personal use only.

============================================================
]]

local addonName, dv = ...

local C = dv.CONSTANTS
local frame = dv.frame

if not frame then
    print("|cffff4040DecorVendor BottomTabs:|r dv.frame is missing. Make sure UI\\MainFrame.lua loads before UI\\BottomTabs.lua.")
    return
end

local CORE_TABS = {
    { id = "vendors",      text = "Vendors",      icon = "Interface\\Icons\\inv_misc_5potionbag_special", width = 128 },
    { id = "professions",  text = "Professions",  icon = "Interface\\Icons\\Trade_Tailoring",             width = 136 },
    { id = "quests",       text = "Quests",       icon = "Interface\\Icons\\Inv_misc_note_01",             width = 128 },
    { id = "achievements", text = "Achievements", icon = "Interface\\Icons\\achievement_level_100",        width = 158 },
    { id = "bossdrops",    text = "Boss Drops",   icon = "Interface\\Icons\\achievement_boss_blackhand",   width = 136 },
}

local CatSizing = C.CatalogSizing or {}

local TAB_BAR_HEIGHT = CatSizing.BottomBarHeight or 44
local DEFAULT_TAB_WIDTH = 128
local TAB_HEIGHT = 28
local TAB_GAP = 6

local bottomTabs = {}
dv.bottomTabs = bottomTabs
dv.tabButtons = dv.tabButtons or {}

local TAB_BACKDROP = {
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
    tile = true,
    tileSize = 16,
    edgeSize = 8,
    insets = { left = 1, right = 1, top = 1, bottom = 1 }
}

-------------------------------------------------
-- Preview Sizing
-------------------------------------------------

function dv.UpdatePreviewSize()
    if not dv.modelContainer then
        return
    end

local heights = {
    vendors = 265,
    professions = 265,
    quests = 250,
    achievements = 250,
    bossdrops = 265,
}

    dv.modelContainer:SetHeight(heights[dv.currentTab] or 245)

    if dv.modelDivider then
        dv.modelDivider:ClearAllPoints()
        dv.modelDivider:SetPoint("TOPLEFT", dv.modelContainer, "BOTTOMLEFT", 12, -8)
        dv.modelDivider:SetPoint("TOPRIGHT", dv.modelContainer, "BOTTOMRIGHT", -12, -8)
    end

    if dv.itemContainer and dv.modelDivider and dv.previewPanel then
        dv.itemContainer:ClearAllPoints()
        dv.itemContainer:SetPoint("TOPLEFT", dv.modelDivider, "BOTTOMLEFT", -2, -10)
        dv.itemContainer:SetPoint("TOPRIGHT", dv.modelDivider, "BOTTOMRIGHT", 2, -10)
        dv.itemContainer:SetPoint("BOTTOMRIGHT", dv.previewPanel, "BOTTOMRIGHT", -10, 10)
    end
end

-------------------------------------------------
-- Tab Bar Shell
-------------------------------------------------

local tabBar = CreateFrame("Frame", "DV_BottomTabBar", frame, "BackdropTemplate")
dv.tabBar = tabBar

tabBar:SetHeight(TAB_BAR_HEIGHT)
tabBar:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 8, 8)
tabBar:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -8, 8)

tabBar:SetBackdrop({
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
    tile = true,
    tileSize = 16,
    edgeSize = 8,
    insets = { left = 1, right = 1, top = 1, bottom = 1 }
})

tabBar:SetBackdropColor(0.025, 0.018, 0.045, 0.90)
tabBar:SetBackdropBorderColor(0.38, 0.26, 0.58, 0.85)

local tabGlow = tabBar:CreateTexture(nil, "ARTWORK")
tabGlow:SetPoint("TOPLEFT", tabBar, "TOPLEFT", 10, -3)
tabGlow:SetPoint("TOPRIGHT", tabBar, "TOPRIGHT", -10, -3)
tabGlow:SetHeight(1)
tabGlow:SetColorTexture(1, 0.78, 0.28, 0.35)

-------------------------------------------------
-- Tab Visuals
-------------------------------------------------

local function SetTabVisual(tab, active)
    if active then
        tab:SetBackdropColor(0.11, 0.075, 0.18, 0.98)
        tab:SetBackdropBorderColor(1, 0.78, 0.28, 0.95)

        tab.bg:SetColorTexture(0.12, 0.07, 0.20, 0.95)
        tab.accent:SetColorTexture(1, 0.78, 0.28, 0.85)

        tab.text:SetTextColor(1, 0.86, 0.34, 1)
        tab.icon:SetVertexColor(1, 0.86, 0.34, 1)
    else
        tab:SetBackdropColor(0.045, 0.032, 0.075, 0.90)
        tab:SetBackdropBorderColor(0.35, 0.25, 0.52, 0.75)

        tab.bg:SetColorTexture(0.035, 0.025, 0.060, 0.88)
        tab.accent:SetColorTexture(0.55, 0.28, 0.95, 0.35)

        tab.text:SetTextColor(0.88, 0.84, 0.95, 1)
        tab.icon:SetVertexColor(0.78, 0.68, 0.95, 1)
    end
end

local function RefreshTabVisuals()
    for _, tab in ipairs(bottomTabs) do
        SetTabVisual(tab, tab.id == dv.currentTab)
    end
end

-------------------------------------------------
-- Clear Preview / Sidebar When Switching Tabs
-------------------------------------------------

function UpdateSidebarForTab()
    if dv.reagentsPopup then dv.reagentsPopup:Hide() end
    if dv.vendorPopup then dv.vendorPopup:Hide() end

    if dv.achievementWowheadWrapper then dv.achievementWowheadWrapper:Hide() end
    if dv.questWowheadWrapper then dv.questWowheadWrapper:Hide() end

    if dv.bossNotes then dv.bossNotes:Hide() end
    if dv.profNotes then dv.profNotes:Hide() end
    if dv.questNotes then dv.questNotes:Hide() end
    if dv.vendorNotes then dv.vendorNotes:Hide() end

    if dv.achievementPanel then dv.achievementPanel:Hide() end
    if dv.vendorWaypointBtn then dv.vendorWaypointBtn:Hide() end

    if dv.vendorPrevBtn then dv.vendorPrevBtn:Hide() end
    if dv.vendorNextBtn then dv.vendorNextBtn:Hide() end
    if dv.vendorPageText then dv.vendorPageText:Hide() end
    if dv.pagingFrame then dv.pagingFrame:Hide() end

    -------------------------------------------------
    -- Clear model scene so old previews do not ghost
    -------------------------------------------------
    if dv.modelScene then
        dv.modelScene:Hide()

        local actor = dv.previewActor

        if actor and actor.ClearModel then
            actor:ClearModel()
        end

        if dv.modelScene.sceneID then
            dv.modelScene:TransitionToModelSceneID(
                dv.modelScene.sceneID,
                Enum.ModelSceneCameraTransitionType.Immediate,
                Enum.ModelSceneCameraModificationType.Discard,
                true
            )
        end
    end

    if dv.texture then
        dv.texture:SetTexture(nil)
        dv.texture:Hide()
    end

-- Show the empty preview icon again when switching tabs
if dv.ShowPreviewEmptyState then
    dv.ShowPreviewEmptyState(true)
else
    if dv.SetPreviewWatermarkVisible then
        dv.SetPreviewWatermarkVisible(true)
    end

    if dv.modelScene then
        dv.modelScene:Show()
    end
end

    if dv.modelTitle and dv.itemContainer then
        dv.modelTitle:ClearAllPoints()
        dv.modelTitle:SetPoint("TOPLEFT", dv.itemContainer, "TOPLEFT", 10, -10)
        dv.modelTitle:SetPoint("TOPRIGHT", dv.itemContainer, "TOPRIGHT", -10, -10)
        dv.modelTitle:SetText("Select an item")
        dv.modelTitle:SetTextColor(1, 0.82, 0.2)
        dv.modelTitle:Show()
    end

    if dv.modelDivider then
        dv.modelDivider:Show()
    end

    -------------------------------------------------
    -- Sidebar filters
    -------------------------------------------------
    if dv.sidebar then
        dv.sidebar:Show()
    end

    if dv.ResetSidebarFilters then
        dv.ResetSidebarFilters()
    end

    if dv.sidebarFilters then
        dv.sidebarFilters:Hide()
        dv.sidebarFilters:Show()
    end

    if dv.currentTab == "vendors" and dv.BuildVendorFilters then
        dv.BuildVendorFilters()

    elseif dv.currentTab == "quests" and dv.BuildQuestFilters then
        dv.BuildQuestFilters()

    elseif dv.currentTab == "achievements" and dv.BuildAchievementFilters then
        dv.BuildAchievementFilters()

    elseif dv.currentTab == "professions" and dv.BuildProfessionFilters then
        dv.BuildProfessionFilters()

    elseif dv.currentTab == "bossdrops" and dv.BuildBossDropFilters then
        dv.BuildBossDropFilters()
    end
end

function dv.OpenMainSection(tabID, force)
    if not tabID then
        return false
    end

    -- Hide Home panel when entering a section.
    if dv.frame and dv.frame.homePanel then
        dv.frame.homePanel:Hide()
    end

	if dv.frame and dv.frame.homeHint then
		dv.frame.homeHint:Show()
	end
    -- Restore normal section UI.
    if dv.sidebar then
        dv.sidebar:Show()
    end

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

    -- Keep bottom tabs hidden visually.
    if dv.tabBar then
        dv.tabBar:Hide()
    end

    -- Restore section-only header controls.
    if _G.DV_SearchBox then
        _G.DV_SearchBox:Show()
    end

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

    if not force and dv.currentTab == tabID then
        return true
    end

    dv.currentTab = tabID

    RefreshTabVisuals()

    if dv.UpdatePreviewSize then
        dv.UpdatePreviewSize()
    end

    if UpdateSidebarForTab then
        UpdateSidebarForTab()
    end

    if BuildVendorUI then
        BuildVendorUI()
    elseif dv.BuildVendorUI then
        dv.BuildVendorUI()
    end

    return true
end
-------------------------------------------------
-- Create Bottom Tabs
-------------------------------------------------

local startX = 12

for i, tabData in ipairs(CORE_TABS) do
    local tab = CreateFrame("Button", "DV_MainTab" .. i, tabBar, "BackdropTemplate")
    tab:SetSize(tabData.width or DEFAULT_TAB_WIDTH, TAB_HEIGHT)
    tab:SetBackdrop(TAB_BACKDROP)

    if i == 1 then
        tab:SetPoint("LEFT", tabBar, "LEFT", startX, 0)
    else
        tab:SetPoint("LEFT", bottomTabs[i - 1], "RIGHT", TAB_GAP, 0)
    end

    tab.id = tabData.id

    local bg = tab:CreateTexture(nil, "BACKGROUND")
    bg:SetPoint("TOPLEFT", tab, "TOPLEFT", 2, -2)
    bg:SetPoint("BOTTOMRIGHT", tab, "BOTTOMRIGHT", -2, 2)
    bg:SetTexture("Interface\\Buttons\\WHITE8x8")
    tab.bg = bg

    local accent = tab:CreateTexture(nil, "ARTWORK")
    accent:SetPoint("TOPLEFT", tab, "TOPLEFT", 6, -3)
    accent:SetPoint("TOPRIGHT", tab, "TOPRIGHT", -6, -3)
    accent:SetHeight(1)
    tab.accent = accent

    local icon = tab:CreateTexture(nil, "ARTWORK")
    icon:SetSize(16, 16)
    icon:SetPoint("LEFT", tab, "LEFT", 9, 0)
    icon:SetTexture(tabData.icon)
    tab.icon = icon

    local text = tab:CreateFontString(nil, "OVERLAY")
    text:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE")
    text:SetPoint("LEFT", icon, "RIGHT", 6, 0)
    text:SetPoint("RIGHT", tab, "RIGHT", -8, 0)
    text:SetJustifyH("CENTER")
    text:SetText(tabData.text)
    tab.text = text

    local hover = tab:CreateTexture(nil, "HIGHLIGHT")
    hover:SetAllPoints(tab)
    hover:SetTexture("Interface\\Buttons\\WHITE8x8")
    hover:SetColorTexture(1, 0.82, 0.2, 0.16)

    tab:SetScript("OnEnter", function(self)
        if self.id ~= dv.currentTab then
            self:SetBackdropBorderColor(0.75, 0.55, 1, 0.95)
            self.text:SetTextColor(1, 1, 1, 1)
        end
    end)

    tab:SetScript("OnLeave", function()
        RefreshTabVisuals()
    end)

tab:SetScript("OnClick", function()
    if dv.currentTab == tabData.id then
        return
    end

    if dv.OpenMainSection then
        dv.OpenMainSection(tabData.id, false)
    end

    PlaySound(SOUNDKIT.IG_CHARACTER_INFO_TAB)
end)

    bottomTabs[i] = tab
dv.tabButtons[tabData.id] = tab
end

RefreshTabVisuals()

-- Home dashboard replaces the bottom tabs visually.
-- The tab buttons still exist for internal navigation, but stay hidden.
if dv.tabBar then
    dv.tabBar:Hide()
end