local _, _ns = ...
local AC = _G.ArenaCore

-- =============================================================================
-- Core/PvPQueueNotify.lua - PvP Queue Notify system
-- ported from Shade addon with ArenaCore conventions
-- because queue popping deserves some fanfare
-- =============================================================================

local PVP_QUEUE_NOTIFY_DEFAULTS = {
    randomBattlegrounds = false,
    randomEpicBattlegrounds = false,
    arenaSkirmish = false,
    brawl = false,
    rated2v2 = false,
    rated3v3 = false,
    soloShuffle = true,
    soloBlitz = false,
}

function AC:GetPvPQueueNotifyModeConfig()
    local db = AC.DB and AC.DB.profile and AC.DB.profile.pvpQueueNotify
    if not db then return {} end

    db.modes = db.modes or {}
    local config = db.modes

    for key, enabled in pairs(PVP_QUEUE_NOTIFY_DEFAULTS) do
        if config[key] == nil then
            config[key] = enabled
        end
    end

    return config
end

function AC:GetPvPQueueNotifyModeForIndex(index)
    if not index or type(GetBattlefieldStatus) ~= "function" then
        return nil
    end

    local status, mapName, teamSize, registeredMatch, suspendedQueue, queueType, gameType = GetBattlefieldStatus(index)
    if not status or status == "none" then
        return nil
    end

    if queueType == "ARENASKIRMISH" then
        return "arenaSkirmish"
    end

    if queueType == "ARENA" then
        if teamSize == 2 then
            return "rated2v2"
        elseif teamSize == 3 then
            return "rated3v3"
        elseif registeredMatch then
            return "rated3v3"
        end
        return nil
    end

    if queueType == "BRAWLSHUFFLE" or queueType == "RATEDSHUFFLE" then
        return "soloShuffle"
    end

    if queueType == "BRAWLSOLORBG" or queueType == "RATEDSOLORBG" then
        return "soloBlitz"
    end

    if queueType == "BATTLEGROUND" then
        local lowerMapName = type(mapName) == "string" and mapName:lower() or ""
        local lowerGameType = type(gameType) == "string" and gameType:lower() or ""
        local brawlInfo = C_PvP and C_PvP.GetAvailableBrawlInfo and C_PvP.GetAvailableBrawlInfo()
        local brawlName = brawlInfo and type(brawlInfo.name) == "string" and brawlInfo.name:lower() or nil

        if brawlName and (lowerMapName == brawlName or lowerGameType == brawlName) then
            return "brawl"
        end

        if lowerMapName:find("brawl", 1, true) or lowerGameType:find("brawl", 1, true) then
            return "brawl"
        end

        if not registeredMatch and teamSize and teamSize >= 20 then
            return "randomEpicBattlegrounds"
        end

        if not registeredMatch then
            return "randomBattlegrounds"
        end
    end

    return nil
end

function AC:ShouldTriggerPvPQueueNotify(index)
    if not AC.__pvpQueueNotifyEnabled then return false end
    if not index then return false end

    local mode = AC:GetPvPQueueNotifyModeForIndex(index)
    if not mode then
        return true
    end

    local config = AC:GetPvPQueueNotifyModeConfig()
    return config[mode] == true
end

function AC:CreatePvPQueueNotifyOverlay()
    local overlay = CreateFrame("Frame", "ArenaCorePvPQueueNotifyOverlay", UIParent)
    overlay:SetAllPoints(UIParent)
    overlay:SetFrameStrata("FULLSCREEN_DIALOG")
    overlay:SetFrameLevel(9999)
    overlay:EnableMouse(false)
    overlay:EnableKeyboard(false)

    local texture = overlay:CreateTexture(nil, "BACKGROUND")
    texture:SetAllPoints()
    texture:SetColorTexture(1, 0, 0, 0)
    overlay.texture = texture

    overlay:Hide()
    return overlay
end

function AC:TriggerPvPQueueNotify(index)
    if not AC.__pvpQueueNotifyEnabled then return end
    if index and not AC:ShouldTriggerPvPQueueNotify(index) then return end

    if not AC.__pvpQueueNotifyOverlay then
        AC.__pvpQueueNotifyOverlay = AC:CreatePvPQueueNotifyOverlay()
    end

    local overlay = AC.__pvpQueueNotifyOverlay
    if not overlay then return end

    if overlay.__acAnimating then return end

    overlay.__acAnimating = true
    overlay.__acElapsed = 0
    overlay.__acFadeDuration = 1.5
    overlay.__acSoundHandle = nil

    local soundPath = "Interface\\AddOns\\ArenaCore\\Media\\Sounds\\queuepopped.mp3"
    local willPlay, soundHandle = PlaySoundFile(soundPath, "Master")
    if willPlay and soundHandle then
        overlay.__acSoundHandle = soundHandle
    end

    overlay:Show()

    overlay:SetScript("OnUpdate", function(self, elapsed)
        local dialog = _G.PVPReadyDialog
        local dialogShown = dialog and dialog:IsShown()

        if not dialogShown then
            if self.__acSoundHandle then
                StopSound(self.__acSoundHandle)
                self.__acSoundHandle = nil
            end
            self:Hide()
            self.__acAnimating = nil
            self.__acElapsed = nil
            self.__acFadeDuration = nil
            self:SetScript("OnUpdate", nil)
            return
        end

        self.texture:SetColorTexture(0.18, 0.8, 0.44, 0.6)
    end)

    if not overlay.__acEscHandlerInstalled then
        overlay.__acEscHandlerInstalled = true
        overlay:SetPropagateKeyboardInput(true)
        overlay:EnableKeyboard(true)
        overlay:SetScript("OnKeyDown", function(self, key)
            if key == "ESCAPE" then
                if self.__acSoundHandle then
                    StopSound(self.__acSoundHandle)
                    self.__acSoundHandle = nil
                end
                self:Hide()
                self.__acAnimating = nil
                self.__acElapsed = nil
                self.__acFadeDuration = nil
                self:SetScript("OnUpdate", nil)
                self:SetPropagateKeyboardInput(false)
            else
                self:SetPropagateKeyboardInput(true)
            end
        end)
    end
end

function AC:SetPvPQueueNotifyEnabled(enabled)
    enabled = (enabled == true)
    AC.__pvpQueueNotifyEnabled = enabled

    if not AC.__pvpQueueNotifyEventFrame then
        local eventFrame = CreateFrame("Frame")
        eventFrame:RegisterEvent("UPDATE_BATTLEFIELD_STATUS")
        eventFrame:SetScript("OnEvent", function(_, event, ...)
            if event == "UPDATE_BATTLEFIELD_STATUS" then
                local battlefieldIndex = ...
                local status = GetBattlefieldStatus(battlefieldIndex)
                if status == "confirm" then
                    AC:TriggerPvPQueueNotify(battlefieldIndex)
                end
            end
        end)
        AC.__pvpQueueNotifyEventFrame = eventFrame
    elseif not enabled and AC.__pvpQueueNotifyEventFrame then
        AC.__pvpQueueNotifyEventFrame:UnregisterAllEvents()
        AC.__pvpQueueNotifyEventFrame = nil
    end
end
