local AC = _G.ArenaCore

-- =============================================================================
-- Core/QueueStatusTracker.lua - Queue Status Tracker window
-- Ported from Shade addon with ArenaCore conventions
-- Shows draggable window with all active queues and wait times
-- =============================================================================

-- Internal state
AC.__queueStatusTracker = nil
AC.__queueStatusTrackerInitialized = false
AC.__queueStatusTrackerWatcher = nil

-- =============================================================================
-- Queue Status Tracker Window Creation
-- =============================================================================

function AC:CreateQueueStatusTracker()
    local frame = CreateFrame("Frame", "ArenaCoreQueueStatusTracker", UIParent)
    -- Don't set default position here - let UpdateQueueStatusTracker apply saved position
    frame:SetFrameStrata("MEDIUM")
    frame:SetFrameLevel(10)
    frame:EnableMouse(true)
    frame:SetMovable(true)
    frame:SetResizable(true)
    frame:RegisterForDrag("LeftButton")
    frame:SetClampedToScreen(true)
    
    -- Store queue sections for dynamic management
    frame.queueSections = {}
    frame.sectionHeight = 75
    frame.isUserResizing = false
    frame.userHasResized = false
    frame.userPreferredSize = nil
    frame.isInitializing = true

    frame:SetScript("OnDragStart", function(self)
        self:StartMoving()
    end)
    frame:SetScript("OnDragStop", function(self)
        self:StopMovingOrSizing()
        if AC and AC.QueueStatusTracker_SavePosition then
            AC:QueueStatusTracker_SavePosition(self)
        end
    end)

    frame:SetScript("OnSizeChanged", function(self, width, height)
        local minWidth, minHeight = 180, 120
        local maxWidth, maxHeight = 400, 500
        local needsResize = false
        
        if width < minWidth then
            width = minWidth
            needsResize = true
        elseif width > maxWidth then
            width = maxWidth
            needsResize = true
        end
        
        if height < minHeight then
            height = minHeight
            needsResize = true
        elseif height > maxHeight then
            height = maxHeight
            needsResize = true
        end
        
        if needsResize then
            self:SetSize(width, height)
            return
        end
        
        -- Store user's preferred size if they manually resized
        if self.isUserResizing then
            self.userHasResized = true
            self.userPreferredSize = {width, height}
        end
        
        -- Update all queue sections with new width
        local barWidth = width - 24
        for _, section in pairs(self.queueSections or {}) do
            if section.avgWaitBar then
                section.avgWaitBar:SetWidth(barWidth)
                if section.avgWaitBar.fill then
                    local currentPercent = section.avgWaitBar.__acCurrentPercent or 1
                    section.avgWaitBar.fill:SetWidth((barWidth - 2) * currentPercent)
                end
            end
            if section.currentTimeBar then
                section.currentTimeBar:SetWidth(barWidth)
                if section.currentTimeBar.fill then
                    local currentPercent = section.currentTimeBar.__acCurrentPercent or 0
                    section.currentTimeBar.fill:SetWidth((barWidth - 2) * currentPercent)
                end
            end
        end
    end)

    -- Background
    local bg = frame:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetColorTexture(0.05, 0.05, 0.05, 0.85)
    frame.bg = bg

    -- Border
    local border = frame:CreateTexture(nil, "BORDER")
    border:SetAllPoints()
    border:SetColorTexture(0.2, 0.2, 0.2, 0.9)
    border:SetDrawLayer("BORDER", 1)
    frame.border = border

    -- Inner background
    local innerBg = frame:CreateTexture(nil, "ARTWORK")
    innerBg:SetPoint("TOPLEFT", 2, -2)
    innerBg:SetPoint("BOTTOMRIGHT", -2, 2)
    innerBg:SetColorTexture(0.08, 0.08, 0.08, 0.9)
    frame.innerBg = innerBg

    -- Title
    local title = frame:CreateFontString(nil, "OVERLAY")
    title:SetPoint("TOP", frame, "TOP", 0, -8)
    title:SetJustifyH("CENTER")
    title:SetTextColor(1, 1, 1, 1)
    if title.SetFont then
        title:SetFont(AC.FONT_PATH or "Interface\\AddOns\\ArenaCore\\Media\\Fonts\\arenacore.ttf", 12, "OUTLINE")
    end
    title:SetText("Queue Status")
    frame.title = title

    -- Load saved size from DB, or use default
    local savedWidth, savedHeight
    if AC.DB and AC.DB.profile and AC.DB.profile.queueStatusTracker then
        if AC.DB.profile.queueStatusTracker.size then
            savedWidth, savedHeight = unpack(AC.DB.profile.queueStatusTracker.size)
        end
    end
    local minWidth, minHeight = 180, 120
    local maxWidth, maxHeight = 400, 500
    savedWidth = math.max(minWidth, math.min(maxWidth, savedWidth or 240))
    savedHeight = math.max(minHeight, math.min(maxHeight, savedHeight or 200))
    frame:SetSize(savedWidth, savedHeight)
    frame.userHasResized = true
    frame.isInitializing = false

    -- Helper function to create a queue section
    function frame:CreateQueueSection(queueId, queueName, yOffset)
        local section = CreateFrame("Frame", nil, self)
        section:SetSize(self:GetWidth() - 20, 50)
        section:SetPoint("TOPLEFT", self, "TOPLEFT", 10, yOffset)
        section.queueId = queueId
        section.queueName = queueName
        
        -- Queue name/title
        local nameText = section:CreateFontString(nil, "OVERLAY")
        nameText:SetPoint("TOPLEFT", section, "TOPLEFT", 0, 0)
        nameText:SetPoint("RIGHT", section, "RIGHT", -5, 0)
        nameText:SetJustifyH("LEFT")
        nameText:SetTextColor(0.9, 0.7, 0.3, 1)
        if nameText.SetFont then
            nameText:SetFont(AC.FONT_PATH or "Interface\\AddOns\\ArenaCore\\Media\\Fonts\\arenacore.ttf", 11, "OUTLINE")
        end
        
        -- Truncate extremely long names
        local displayName = queueName
        if string.len(displayName) > 40 then
            displayName = string.sub(displayName, 1, 37) .. "..."
        end
        nameText:SetText(displayName)
        section.nameText = nameText
        
        -- Avg Wait label
        local avgWaitLabel = section:CreateFontString(nil, "OVERLAY")
        avgWaitLabel:SetPoint("TOPLEFT", nameText, "BOTTOMLEFT", 0, -6)
        avgWaitLabel:SetJustifyH("LEFT")
        avgWaitLabel:SetTextColor(0.8, 0.8, 0.8, 1)
        if avgWaitLabel.SetFont then
            avgWaitLabel:SetFont(AC.FONT_PATH or "Interface\\AddOns\\ArenaCore\\Media\\Fonts\\arenacore.ttf", 9, "OUTLINE")
        end
        avgWaitLabel:SetText("Avg Wait:")
        section.avgWaitLabel = avgWaitLabel
        
        -- Avg Wait bar
        local avgWaitBar = CreateFrame("Frame", nil, section)
        avgWaitBar:SetSize(self:GetWidth() - 44, 12)
        avgWaitBar:SetPoint("TOPLEFT", avgWaitLabel, "BOTTOMLEFT", 0, -2)
        local avgWaitBorder = avgWaitBar:CreateTexture(nil, "BACKGROUND")
        avgWaitBorder:SetAllPoints()
        avgWaitBorder:SetColorTexture(1, 0.5, 0, 1)
        local avgWaitBg = avgWaitBar:CreateTexture(nil, "BORDER")
        avgWaitBg:SetPoint("TOPLEFT", avgWaitBar, "TOPLEFT", 1, -1)
        avgWaitBg:SetPoint("BOTTOMRIGHT", avgWaitBar, "BOTTOMRIGHT", -1, 1)
        avgWaitBg:SetColorTexture(0.1, 0.1, 0.1, 0.9)
        local avgWaitFill = avgWaitBar:CreateTexture(nil, "ARTWORK")
        avgWaitFill:SetPoint("TOPLEFT", avgWaitBar, "TOPLEFT", 1, -1)
        avgWaitFill:SetPoint("BOTTOMLEFT", avgWaitBar, "BOTTOMLEFT", 1, 1)
        avgWaitFill:SetWidth((self:GetWidth() - 46))
        avgWaitFill:SetColorTexture(0.3, 0.6, 0.9, 0.8)
        local avgWaitText = avgWaitBar:CreateFontString(nil, "OVERLAY")
        avgWaitText:SetAllPoints(avgWaitBar)
        avgWaitText:SetJustifyH("CENTER")
        avgWaitText:SetJustifyV("MIDDLE")
        avgWaitText:SetTextColor(1, 1, 1, 1)
        if avgWaitText.SetFont then
            avgWaitText:SetFont(AC.FONT_PATH or "Interface\\AddOns\\ArenaCore\\Media\\Fonts\\arenacore.ttf", 9, "OUTLINE")
        end
        avgWaitText:SetText("--")
        avgWaitBar.border = avgWaitBorder
        avgWaitBar.bg = avgWaitBg
        avgWaitBar.fill = avgWaitFill
        avgWaitBar.text = avgWaitText
        section.avgWaitBar = avgWaitBar
        
        -- Time In Queue label
        local currentTimeLabel = section:CreateFontString(nil, "OVERLAY")
        currentTimeLabel:SetPoint("TOPLEFT", avgWaitBar, "BOTTOMLEFT", 0, -4)
        currentTimeLabel:SetJustifyH("LEFT")
        currentTimeLabel:SetTextColor(0.8, 0.8, 0.8, 1)
        if currentTimeLabel.SetFont then
            currentTimeLabel:SetFont(AC.FONT_PATH or "Interface\\AddOns\\ArenaCore\\Media\\Fonts\\arenacore.ttf", 9, "OUTLINE")
        end
        currentTimeLabel:SetText("Time In Queue:")
        section.currentTimeLabel = currentTimeLabel
        
        -- Time In Queue bar
        local currentTimeBar = CreateFrame("Frame", nil, section)
        currentTimeBar:SetSize(self:GetWidth() - 44, 12)
        currentTimeBar:SetPoint("TOPLEFT", currentTimeLabel, "BOTTOMLEFT", 0, -2)
        local currentTimeBorder = currentTimeBar:CreateTexture(nil, "BACKGROUND")
        currentTimeBorder:SetAllPoints()
        currentTimeBorder:SetColorTexture(1, 0.5, 0, 1)
        local currentTimeBg = currentTimeBar:CreateTexture(nil, "BORDER")
        currentTimeBg:SetPoint("TOPLEFT", currentTimeBar, "TOPLEFT", 1, -1)
        currentTimeBg:SetPoint("BOTTOMRIGHT", currentTimeBar, "BOTTOMRIGHT", -1, 1)
        currentTimeBg:SetColorTexture(0.1, 0.1, 0.1, 0.9)
        local currentTimeFill = currentTimeBar:CreateTexture(nil, "ARTWORK")
        currentTimeFill:SetPoint("TOPLEFT", currentTimeBar, "TOPLEFT", 1, -1)
        currentTimeFill:SetPoint("BOTTOMLEFT", currentTimeBar, "BOTTOMLEFT", 1, 1)
        currentTimeFill:SetWidth((self:GetWidth() - 46))
        currentTimeFill:SetColorTexture(0.9, 0.6, 0.2, 0.8)
        local currentTimeText = currentTimeBar:CreateFontString(nil, "OVERLAY")
        currentTimeText:SetAllPoints(currentTimeBar)
        currentTimeText:SetJustifyH("CENTER")
        currentTimeText:SetJustifyV("MIDDLE")
        currentTimeText:SetTextColor(1, 1, 1, 1)
        if currentTimeText.SetFont then
            currentTimeText:SetFont(AC.FONT_PATH or "Interface\\AddOns\\ArenaCore\\Media\\Fonts\\arenacore.ttf", 9, "OUTLINE")
        end
        currentTimeText:SetText("--")
        currentTimeBar.border = currentTimeBorder
        currentTimeBar.bg = currentTimeBg
        currentTimeBar.fill = currentTimeFill
        currentTimeBar.text = currentTimeText
        section.currentTimeBar = currentTimeBar
        
        return section
    end
    
    -- Helper function to clear all queue sections
    function frame:ClearQueueSections()
        for _, section in pairs(self.queueSections or {}) do
            if section then
                section:Hide()
                section:SetParent(nil)
            end
        end
        self.queueSections = {}
    end
    
    -- Helper function to update frame size based on number of queues
    function frame:UpdateSizeForQueues(numQueues)
        -- User has full control over window size - no auto-resize
    end

    -- Vanity line
    local vanityLine = frame:CreateFontString(nil, "OVERLAY")
    vanityLine:SetPoint("BOTTOM", frame, "BOTTOM", 0, 8)
    vanityLine:SetJustifyH("CENTER")
    vanityLine:SetTextColor(1, 1, 1, 1)
    if vanityLine.SetFont then
        vanityLine:SetFont(AC.FONT_PATH or "Interface\\AddOns\\ArenaCore\\Media\\Fonts\\arenacore.ttf", 10, "OUTLINE")
    end
    vanityLine:SetText("hang in there buddy <3")
    frame.vanityLine = vanityLine

    -- Resize grip
    local resizeGrip = CreateFrame("Button", nil, frame)
    resizeGrip:SetSize(22, 22)
    resizeGrip:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0)
    resizeGrip:SetFrameLevel(frame:GetFrameLevel() + 10)

    local function makeLine(yOff, width)
        local t = resizeGrip:CreateTexture(nil, "OVERLAY")
        t:SetColorTexture(1, 0.5, 0, 0.75)
        t:SetSize(width, 1)
        t:SetPoint("BOTTOMRIGHT", resizeGrip, "BOTTOMRIGHT", -1, yOff)
        return t
    end

    local lines = { makeLine(1, 12), makeLine(4, 8), makeLine(7, 4) }

    resizeGrip:SetScript("OnEnter", function()
        for _, l in ipairs(lines) do
            l:SetVertexColor(1, 0.65, 0, 1)
        end
    end)
    resizeGrip:SetScript("OnLeave", function()
        for _, l in ipairs(lines) do
            l:SetVertexColor(1, 0.5, 0, 0.75)
        end
    end)
    resizeGrip:SetScript("OnMouseDown", function(self, button)
        if button == "LeftButton" then
            frame.isUserResizing = true
            frame:StartSizing("BOTTOMRIGHT")
            
            frame.resizeTimer = C_Timer.NewTimer(5, function()
                frame.isUserResizing = false
            end)
        end
    end)
    resizeGrip:SetScript("OnMouseUp", function(self, button)
        if button == "LeftButton" then
            frame:StopMovingOrSizing()
            frame.isUserResizing = false
            
            -- Save final size to DB
            if AC.DB and AC.DB.profile then
                AC.DB.profile.queueStatusTracker = AC.DB.profile.queueStatusTracker or {}
                local width, height = frame:GetSize()
                AC.DB.profile.queueStatusTracker.size = {width, height}
            end
            
            if frame.resizeTimer then
                frame.resizeTimer:Cancel()
                frame.resizeTimer = nil
            end
        elseif button == "RightButton" and IsShiftKeyDown() then
            frame.userHasResized = false
            frame.userPreferredSize = nil
            frame.lastQueueCount = nil
            print("|cff8B45FFArenaCore Queue Status:|r Reset to automatic sizing")
        end
    end)

    frame.resizeGrip = resizeGrip

    frame:Hide()
    return frame
end

-- =============================================================================
-- Queue Status Tracker Update
-- =============================================================================

function AC:UpdateQueueStatusTracker()
    if not AC.__queueStatusTracker then
        AC.__queueStatusTracker = AC:CreateQueueStatusTracker()
        -- Only apply saved position during initialization (like shade does)
        if AC.DB and AC.DB.profile and AC.DB.profile.queueStatusTracker then
            if AC.DB.profile.queueStatusTracker.position then
                local pos = AC.DB.profile.queueStatusTracker.position
                AC.__queueStatusTracker:ClearAllPoints()
                AC.__queueStatusTracker:SetPoint(pos[1], UIParent, pos[2], pos[3], pos[4])
            else
                -- Default position if no saved position
                AC.__queueStatusTracker:ClearAllPoints()
                AC.__queueStatusTracker:SetPoint("RIGHT", UIParent, "RIGHT", -20, 0)
            end
        else
            -- Default position if no database
            AC.__queueStatusTracker:ClearAllPoints()
            AC.__queueStatusTracker:SetPoint("RIGHT", UIParent, "RIGHT", -20, 0)
        end
    end

    local frame = AC.__queueStatusTracker
    if not frame then return end

    -- Collect all active queues
    local activeQueues = {}
    
    -- Collect PvP queues
    for i = 1, GetMaxBattlefieldID() do
        local status, mapName, teamSize, registeredMatch, suspendedQueue, queueType, gameType, role, asGroup, shortDescription, longDescription, isSoloQueue = GetBattlefieldStatus(i)
        if status == "queued" then
            local queueId = "pvp_" .. i
            local displayName = mapName or "Unknown PvP"
            
            if gameType and gameType ~= "" then
                displayName = displayName .. " (" .. gameType .. ")"
            end
            if teamSize and teamSize > 0 then
                displayName = displayName .. " " .. teamSize .. "v" .. teamSize
            end
            
            local estimatedWait = GetBattlefieldEstimatedWaitTime(i)
            local timeInQueue = GetBattlefieldTimeWaited(i)
            
            activeQueues[queueId] = {
                name = displayName,
                type = "pvp",
                avgWait = estimatedWait and estimatedWait > 0 and estimatedWait / 1000 or 0,
                currentTime = timeInQueue and timeInQueue > 0 and timeInQueue / 1000 or 0,
                rawData = {
                    battlefieldIndex = i,
                    status = status,
                    mapName = mapName,
                    gameType = gameType,
                    teamSize = teamSize
                }
            }
        end
    end
    
    -- Collect LFG queues
    if GetLFGQueueStats then
        for category = 1, NUM_LE_LFG_CATEGORYS or 2 do
            local mode = GetLFGMode(category)
            if mode == "queued" or mode == "proposal" or mode == "listed" then
                local hasData, leaderNeeds, tankNeeds, healerNeeds, dpsNeeds, totalTanks, totalHealers, totalDPS, instanceType, instanceSubType, instanceName, averageWait, tankWait, healerWait, damageWait, myWait, queuedTime = GetLFGQueueStats(category)
                
                if hasData and instanceName and instanceName ~= "" then
                    local queueId = "lfg_" .. category
                    local displayName = instanceName
                    
                    if instanceType then
                        if instanceType == "dungeon" then
                            displayName = "Dungeon: " .. displayName
                        elseif instanceType == "raid" then
                            displayName = "Raid: " .. displayName
                        elseif instanceType == "scenario" then
                            displayName = "Scenario: " .. displayName
                        elseif instanceType == "flexraid" then
                            displayName = "Flex: " .. displayName
                        elseif instanceType == "worfloater" then
                            displayName = "Time Walking: " .. displayName
                        end
                    end
                    
                    local avgWait = 0
                    if myWait and myWait > 0 then
                        avgWait = myWait
                    elseif averageWait and averageWait > 0 then
                        avgWait = averageWait
                    end
                    
                    local currentTime = 0
                    if queuedTime and queuedTime > 0 then
                        local now = GetTime and GetTime() or 0
                        local elapsed = now - queuedTime
                        if elapsed > 0 then
                            currentTime = elapsed
                        end
                    end
                    
                    activeQueues[queueId] = {
                        name = displayName,
                        type = "lfg",
                        avgWait = avgWait,
                        currentTime = currentTime,
                        rawData = {
                            category = category,
                            instanceType = instanceType,
                            instanceName = instanceName,
                            mode = mode
                        }
                    }
                end
            end
        end
    end
    
    -- Reset user resize preference if queue count changed
    local currentQueueCount = 0
    for _ in pairs(activeQueues) do
        currentQueueCount = currentQueueCount + 1
    end
    
    if frame.lastQueueCount and frame.lastQueueCount ~= currentQueueCount then
        frame.userHasResized = false
        frame.userPreferredSize = nil
    end
    frame.lastQueueCount = currentQueueCount
    
    -- Clear existing sections and create new ones
    frame:ClearQueueSections()
    
    local queueCount = 0
    local yOffset = -35
    
    -- Calculate optimal width based on longest queue name
    local currentWidth = frame:GetWidth()
    if not frame.userHasResized then
        local maxQueueNameLength = 0
        for queueId, queueData in pairs(activeQueues) do
            local nameLength = string.len(queueData.name)
            maxQueueNameLength = math.max(maxQueueNameLength, nameLength)
        end
        
        local baseWidth = 240
        local extraWidth = 0
        if maxQueueNameLength > 25 then
            extraWidth = math.min((maxQueueNameLength - 25) * 4, 80)
        end
        local optimalWidth = baseWidth + extraWidth
        
        if math.abs(currentWidth - optimalWidth) > 5 then
            local minWidth, maxWidth = 180, 400
            optimalWidth = math.max(minWidth, math.min(optimalWidth, maxWidth))
            frame:SetSize(optimalWidth, frame:GetHeight())
        end
    end
    
    -- Create sections for each active queue
    for queueId, queueData in pairs(activeQueues) do
        queueCount = queueCount + 1
        
        local section = frame:CreateQueueSection(queueId, queueData.name, yOffset)
        frame.queueSections[queueId] = section
        section:Show()
        
        local barWidth = frame:GetWidth() - 44
        local fillWidth = barWidth - 2
        
        -- Update average wait bar
        if queueData.avgWait > 0 then
            local avgPercent = 1
            if queueData.avgWait > 0 and queueData.currentTime > 0 then
                avgPercent = math.max(0, math.min(1, (queueData.avgWait - queueData.currentTime) / queueData.avgWait))
            end
            section.avgWaitBar.__acCurrentPercent = avgPercent
            section.avgWaitBar.fill:SetWidth(fillWidth * avgPercent)
            local avgMinutes = math.floor(queueData.avgWait / 60)
            local avgSeconds = math.floor(queueData.avgWait % 60)
            section.avgWaitBar.text:SetText(string.format("%d:%02d", avgMinutes, avgSeconds))
        else
            section.avgWaitBar.__acCurrentPercent = 1
            section.avgWaitBar.fill:SetWidth(fillWidth)
            section.avgWaitBar.text:SetText("Unknown")
        end
        
        -- Update current time bar
        if queueData.currentTime > 0 then
            local currentPercent = 1
            if queueData.avgWait > 0 then
                currentPercent = math.min(1, queueData.currentTime / queueData.avgWait)
            end
            section.currentTimeBar.__acCurrentPercent = currentPercent
            section.currentTimeBar.fill:SetWidth(fillWidth * currentPercent)
            local currentMinutes = math.floor(queueData.currentTime / 60)
            local currentSeconds = math.floor(queueData.currentTime % 60)
            section.currentTimeBar.text:SetText(string.format("%d:%02d", currentMinutes, currentSeconds))
        else
            section.currentTimeBar.__acCurrentPercent = 0
            section.currentTimeBar.fill:SetWidth(0)
            section.currentTimeBar.text:SetText("0:00")
        end
        
        yOffset = yOffset - 75
    end
    
    frame:UpdateSizeForQueues(queueCount)
    
    -- Show or hide frame based on whether we have active queues
    if queueCount > 0 then
        frame:Show()
    else
        frame:Hide()
    end
end

-- =============================================================================
-- Queue Status Tracker Initialization
-- =============================================================================

function AC:InitializeQueueStatusTracker()
    if AC.__queueStatusTrackerInitialized then return end
    AC.__queueStatusTrackerInitialized = true

    local f = CreateFrame("Frame")
    f:RegisterEvent("LFG_UPDATE")
    f:RegisterEvent("UPDATE_BATTLEFIELD_STATUS")
    f:RegisterEvent("LFG_QUEUE_STATUS_UPDATE")
    f:RegisterEvent("PLAYER_ENTERING_WORLD")
    f:SetScript("OnEvent", function()
        if AC and AC.UpdateQueueStatusTracker then
            AC:UpdateQueueStatusTracker()
        end
    end)

    AC.__queueStatusTrackerWatcher = f

    C_Timer.NewTicker(1, function()
        if AC and AC.UpdateQueueStatusTracker then
            AC:UpdateQueueStatusTracker()
        end
    end)
end

-- =============================================================================
-- Save Position Helper
-- =============================================================================

function AC:QueueStatusTracker_SavePosition(frame)
    if not AC.DB or not AC.DB.profile then return end
    AC.DB.profile.queueStatusTracker = AC.DB.profile.queueStatusTracker or {}

    local centerX, centerY = frame:GetCenter()
    if centerX and centerY then
        AC.DB.profile.queueStatusTracker.position = {"CENTER", "BOTTOMLEFT", centerX, centerY}
    end
end

-- =============================================================================
-- Public API to Enable/Disable
-- =============================================================================

function AC:SetQueueStatusTrackerEnabled(enabled)
    enabled = (enabled == true)
    
    if enabled then
        AC:InitializeQueueStatusTracker()
        AC:UpdateQueueStatusTracker()
    else
        if AC.__queueStatusTracker then
            AC.__queueStatusTracker:Hide()
        end
        if AC.__queueStatusTrackerWatcher then
            AC.__queueStatusTrackerWatcher:UnregisterAllEvents()
            AC.__queueStatusTrackerWatcher = nil
        end
        AC.__queueStatusTrackerInitialized = false
    end
end
