local addonName, ns = ...
local L = ns.L

-- Combat logging for raids/M+
-- Auto mode:  enables silently on instance enter (default).
-- Popup mode: shows a styled BrightQoL dialog asking the user first.
-- Toggle module on/off with: /bqcombatlog

local isLogging = false

---------------------------------------------------------------------------
-- Custom BrightQoL-styled popup
---------------------------------------------------------------------------
local popup

local function GetAccent()
    if ns.Theme then return ns.Theme.GetAccent() end
    return 0, 207/255, 1.0
end

local function BuildPopup()
    if popup then return popup end

    local W = ns.Widgets
    if not W then return nil end

    local ar, ag, ab = GetAccent()

    local f = CreateFrame("Frame", "BrightQoL_CombatLogPopup", UIParent, "BackdropTemplate")
    f:SetSize(320, 140)
    f:SetPoint("CENTER", UIParent, "CENTER", 0, 80)
    f:SetFrameStrata("DIALOG")
    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop",  f.StopMovingOrSizing)

    f:SetBackdrop({
        bgFile   = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Buttons\\WHITE8X8",
        edgeSize = 1,
        insets   = { left = 1, right = 1, top = 1, bottom = 1 },
    })
    f:SetBackdropColor(0.02, 0.02, 0.02, 0.95)
    f:SetBackdropBorderColor(ar, ag, ab, 0.7)

    local topAccent = f:CreateTexture(nil, "OVERLAY")
    topAccent:SetHeight(2)
    topAccent:SetPoint("TOPLEFT",  1, -1)
    topAccent:SetPoint("TOPRIGHT", -1, -1)
    topAccent:SetColorTexture(ar, ag, ab, 0.9)

    local title = f:CreateFontString(nil, "OVERLAY")
    title:SetFont("Interface\\AddOns\\BrightQoL\\Assets\\Fonts\\Bright.ttf", 13, "")
    title:SetPoint("TOPLEFT", 12, -14)
    title:SetText("|cffa855f7Bright|r|cff00cfff QoL|r  |cffaaaaaa· Combat Logger|r")

    local divider = f:CreateTexture(nil, "ARTWORK")
    divider:SetHeight(1)
    divider:SetPoint("TOPLEFT",  10, -30)
    divider:SetPoint("TOPRIGHT", -10, -30)
    divider:SetColorTexture(ar, ag, ab, 0.25)

    local zoneLine = f:CreateFontString(nil, "OVERLAY")
    zoneLine:SetFont("Interface\\AddOns\\BrightQoL\\Assets\\Fonts\\Bright.ttf", 12, "")
    zoneLine:SetPoint("TOPLEFT", 12, -44)
    zoneLine:SetPoint("RIGHT", -12, 0)
    zoneLine:SetJustifyH("LEFT")
    f.zoneLine = zoneLine

    local diffLine = f:CreateFontString(nil, "OVERLAY")
    diffLine:SetFont("Interface\\AddOns\\BrightQoL\\Assets\\Fonts\\Bright.ttf", 10, "")
    diffLine:SetPoint("TOPLEFT", 12, -62)
    diffLine:SetTextColor(0.6, 0.6, 0.6, 1)
    f.diffLine = diffLine

    local btnEnable = W:CreateButton(f, {
        text    = L["COMBATLOGGER_ENABLE_BTN"] or "Enable Logging",
        width   = 130,
        height  = 26,
        onClick = function()
            f:Hide()
            LoggingCombat(true)
            isLogging = true
            ns:Log(L["COMBATLOGGER_ENABLED_MSG"], "03E826")
        end,
    })
    btnEnable:SetPoint("BOTTOMLEFT", 12, 12)

    local btnSkip = W:CreateButton(f, {
        text    = L["COMBATLOGGER_SKIP_BTN"] or "Skip",
        width   = 80,
        height  = 26,
        onClick = function() f:Hide() end,
    })
    btnSkip:SetPoint("LEFT", btnEnable, "RIGHT", 8, 0)

    tinsert(UISpecialFrames, f:GetName())

    f:Hide()
    popup = f
    return popup
end

local function ShowPopup(zoneName, difficultyName)
    local f = BuildPopup()
    if not f then return end
    f.zoneLine:SetText("|cffffffff" .. (zoneName or "") .. "|r")
    f.diffLine:SetText(difficultyName or "")
    f:Show()
    f:Raise()
end

local function HidePopup()
    if popup then popup:Hide() end
end

---------------------------------------------------------------------------
-- Zone change handler
---------------------------------------------------------------------------
local function OnZoneChanged(zoneData)
    local db = BrightQoL.combatLogger
    if not db or not db.enabled then
        if isLogging then LoggingCombat(false); isLogging = false end
        return
    end

    local shouldTrack = (zoneData.instanceType == "raid" or zoneData.instanceType == "party")

    if not shouldTrack then
        HidePopup()
        if isLogging then LoggingCombat(false); isLogging = false end
        return
    end

    if isLogging then return end  -- already logging this session

    if db.popupMode then
        ShowPopup(zoneData.zoneName, zoneData.difficultyName)
    else
        LoggingCombat(true)
        isLogging = true
        ns:Log(L["COMBATLOGGER_ENABLED_MSG"], "03E826")
    end
end

---------------------------------------------------------------------------
-- Called by config when popup mode is toggled, so it takes effect
-- immediately if the player is already inside a trackable instance.
---------------------------------------------------------------------------
function ns.CombatLoggerOnPopupModeChanged(enabled)
    if not enabled then HidePopup(); return end
    if not ns.ZoneUtil then return end
    local zone = ns.ZoneUtil.GetCurrentZone()
    if zone.instanceType == "raid" or zone.instanceType == "party" then
        ShowPopup(zone.zoneName, zone.difficultyName)
    end
end

---------------------------------------------------------------------------
-- Slash command
---------------------------------------------------------------------------
SLASH_BRIGHTQOL_COMBATLOG1 = "/bqcombatlog"
SlashCmdList["BRIGHTQOL_COMBATLOG"] = function()
    local db = BrightQoL.combatLogger
    if not db then return end
    db.enabled = not db.enabled
    if db.enabled then
        ns:Log(L["COMBATLOGGER_SLASH_ENABLED"], "03E826")
    else
        HidePopup()
        if isLogging then LoggingCombat(false); isLogging = false end
        ns:Log(L["COMBATLOGGER_SLASH_DISABLED"], "FF4444")
    end
end

---------------------------------------------------------------------------
-- Init
---------------------------------------------------------------------------
local loader = CreateFrame("Frame", "BrightQoL_CombatLogger")
loader:RegisterEvent("PLAYER_LOGIN")

loader:SetScript("OnEvent", function(self, event)
    if event == "PLAYER_LOGIN" then
        if not BrightQoL.combatLogger then
            BrightQoL.combatLogger = { enabled = true, popupMode = false }
        end
        local db = BrightQoL.combatLogger
        if db.enabled   == nil then db.enabled   = true  end
        if db.popupMode == nil then db.popupMode = false end

        if ns.ZoneUtil and ns.ZoneUtil.RegisterCallback then
            ns.ZoneUtil.RegisterCallback("CombatLogger", OnZoneChanged)
            C_Timer.After(0.5, function()
                OnZoneChanged(ns.ZoneUtil.GetCurrentZone())
            end)
        end

        self:UnregisterEvent("PLAYER_LOGIN")
    end
end)

ns.CombatLogger = loader
