local addonName, addon = ...

local UI = addon.UI
local LT = addon.LootTracker
local LootDB = LT.DB
local Query = LT.Query
local Data = LT.Data
local UpgradeTracks = LT.UpgradeTracks
local Upgrade = LT.Upgrade

function UI:RefreshLootTracker()
    self:CloseLootAnchoredMenu()


    if self.CloseLootAnchoredMenu then
        self:CloseLootAnchoredMenu()
    end
    if not self.LootTrackerFrame then
        return
    end

    self:EnsureLootTrackerSelection()
    self:RefreshLootTrackerSelectors()
    self:ClearFrame(self.LootContent)

    if self.LootViewMode ==
        "myloot"
    then
        self:BuildMyLoot()
    else
        self:BuildLootBrowse()
    end

    -- Loot Browser and My Loot share this frame, so re-sync the left
    -- navigation highlight after the effective loot view has been built.
    if self.UpdateNavigationSelection then
        self:UpdateNavigationSelection()
    end
end

function UI:ScheduleLootTrackerRefresh()
    if self.LootRefreshScheduled then
        return
    end

    self.LootRefreshScheduled = true

    C_Timer.After(
        0.20,
        function()
            self.LootRefreshScheduled = false

            if self.MainFrame
                and self.MainFrame:IsShown()
                and (self.ActiveSection == "loot" or self.ActiveSection == "myloot")
            then
                self:RefreshLootTracker()
            end
        end
    )
end

function UI:SetActiveSection(section)
    self:CloseLootAnchoredMenu()

    local valid = {
        tasks = true,
        loot = true,
        myloot = true,
        loottest = true,
        tank = true,
        about = true,
        settings = true,
    }

    if not valid[section] then
        section = "tasks"
    end

    self.ActiveSection = section

    if self.InputPanel then
        self.InputPanel:Hide()
    end
    if self.MainContent then
        self.MainContent:Hide()
    end
    if self.LootTrackerFrame then
        self.LootTrackerFrame:Hide()
    end
    if self.EquipmentLootTestFrame then
        self.EquipmentLootTestFrame:Hide()
    end
    if self.TankAssistantPage then
        self.TankAssistantPage:Hide()
    end
    if self.AboutPage then
        self.AboutPage:Hide()
    end
    if self.SettingsPage then
        self.SettingsPage:Hide()
    end

    if section == "loot" or section == "myloot" then
        self.LootViewMode = (section == "myloot") and "myloot" or "browse"
        if not self.LootTrackerFrame and self.InitializeLootTracker then
            self:InitializeLootTracker()
        end

        if self.LootTrackerFrame then
            self.LootTrackerFrame:Show()
            self:RefreshLootTracker()
        else
            self.ActiveSection = "tasks"
            if self.InputPanel then self.InputPanel:Show() end
            if self.MainContent then self.MainContent:Show() end
            self:Refresh()
        end

    elseif section == "loottest" then
        if not self.EquipmentLootTestFrame and self.InitializeEquipmentLootTest then
            self:InitializeEquipmentLootTest()
        end
        if self.EquipmentLootTestFrame then
            self.EquipmentLootTestFrame:Show()
            if self.RefreshEquipmentLootTest then self:RefreshEquipmentLootTest() end
        end

    elseif section == "tank" then
        if not self.TankAssistantPage and self.InitializeTankAssistant then
            self:InitializeTankAssistant()
        end
        if self.TankAssistantPage then
            self.TankAssistantPage:Show()
            if self.RefreshTankAssistant then self:RefreshTankAssistant() end
        end

    elseif section == "about" then
        if self.AboutPage then
            self.AboutPage:Show()
        end

    elseif section == "settings" then
        if self.SettingsPage then
            self.SettingsPage:Show()
        end

    else
        if self.InputPanel then
            self.InputPanel:Show()
        end
        if self.MainContent then
            self.MainContent:Show()
        end
        self:Refresh()
    end

    if self.UpdateNavigationSelection then
        self:UpdateNavigationSelection()
    end
end
