local _, ns = ...

-- Debug frame for detailed logging
local debugFrame

-- Create debug frame
function ns.CreateDebugFrame()
    if debugFrame then return debugFrame end
    
    debugFrame = CreateFrame("Frame", "nCastbarDebugFrame", UIParent, "BackdropTemplate")
    debugFrame:SetSize(600, 400)
    debugFrame:SetPoint("CENTER")
    debugFrame:SetBackdrop({
        bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
        tile = true,
        tileSize = 32,
        edgeSize = 32,
        insets = {left = 11, right = 12, top = 12, bottom = 11}
    })
    debugFrame:SetBackdropColor(0, 0, 0, 0.9)
    debugFrame:EnableMouse(true)
    debugFrame:SetMovable(true)
    debugFrame:RegisterForDrag("LeftButton")
    debugFrame:SetScript("OnDragStart", function(self) self:StartMoving() end)
    debugFrame:SetScript("OnDragStop", function(self) self:StopMovingOrSizing() end)
    debugFrame:SetFrameStrata("DIALOG")
    debugFrame:Hide()
    
    -- Title
    local title = debugFrame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    title:SetPoint("TOP", 0, -15)
    title:SetText("nCastbar Debug Console")
    
    -- Scroll frame
    local scrollFrame = CreateFrame("ScrollFrame", "nCastbarDebugScrollFrame", debugFrame, "UIPanelScrollFrameTemplate")
    scrollFrame:SetPoint("TOPLEFT", 20, -40)
    scrollFrame:SetPoint("BOTTOMRIGHT", -40, 50)
    
    -- Edit box
    local editBox = CreateFrame("EditBox", "nCastbarDebugEditBox", scrollFrame)
    editBox:SetSize(scrollFrame:GetWidth(), scrollFrame:GetHeight())
    editBox:SetMultiLine(true)
    editBox:SetAutoFocus(false)
    editBox:SetFontObject(ChatFontNormal)
    editBox:SetScript("OnEscapePressed", function() debugFrame:Hide() end)
    scrollFrame:SetScrollChild(editBox)
    
    debugFrame.editBox = editBox
    
    -- Close button
    local closeBtn = CreateFrame("Button", nil, debugFrame, "UIPanelCloseButton")
    closeBtn:SetPoint("TOPRIGHT", -5, -5)
    
    -- Clear button
    local clearBtn = CreateFrame("Button", nil, debugFrame, "UIPanelButtonTemplate")
    clearBtn:SetSize(80, 22)
    clearBtn:SetPoint("BOTTOMLEFT", 20, 15)
    clearBtn:SetText("Clear")
    clearBtn:SetScript("OnClick", function()
        editBox:SetText("")
    end)
    
    return debugFrame
end

-- Log debug message to frame and/or chat
function ns.DebugLog(...)
    if not nCastbarDB.debug and not nCastbarDB.profile.debugChat then return end
    
    local timestamp = date("%H:%M:%S")
    local msg = strjoin(" ", tostringall(...))
    
    -- Log to debug frame if debug mode is enabled
    if nCastbarDB.debug then
        local debugFrame = ns.CreateDebugFrame()
        local editBox = debugFrame.editBox
        
        local text = editBox:GetText()
        
        if text == "" then
            editBox:SetText(string.format("[%s] %s", timestamp, msg))
        else
            editBox:SetText(string.format("%s\n[%s] %s", text, timestamp, msg))
        end
        
        -- Scroll to bottom
        editBox:SetCursorPosition(editBox:GetText():len())
    end
    
    -- Log to chat if chat debug is enabled
    if nCastbarDB.profile and nCastbarDB.profile.debugChat then
        print("|cff1784d1nCastbar:|r |cffff9900[" .. timestamp .. "]|r " .. msg)
    end
end

-- Show debug frame
function ns.ShowDebugFrame()
    local debugFrame = ns.CreateDebugFrame()
    if debugFrame:IsShown() then
        debugFrame:Hide()
    else
        debugFrame:Show()
    end
end

-- Toggle debug mode
function ns.ToggleDebug()
    nCastbarDB.debug = not nCastbarDB.debug
    
    if nCastbarDB.debug then
        ns.Print("Debug mode: " .. ns.L["ENABLED"])
        ns.DebugLog("Debug mode enabled")
    else
        ns.Print("Debug mode: " .. ns.L["DISABLED"])
    end
end

-- Event tracking for debugging
local eventFrame = CreateFrame("Frame")
local trackedEvents = {
    "PLAYER_ENTERING_WORLD",
    "UNIT_SPELLCAST_START",
    "UNIT_SPELLCAST_STOP",
    "UNIT_SPELLCAST_FAILED",
    "UNIT_SPELLCAST_INTERRUPTED",
    "UNIT_SPELLCAST_DELAYED",
    "UNIT_SPELLCAST_CHANNEL_START",
    "UNIT_SPELLCAST_CHANNEL_STOP",
    "UNIT_SPELLCAST_CHANNEL_UPDATE",
}

function ns.EnableEventTracking()
    for _, event in ipairs(trackedEvents) do
        eventFrame:RegisterEvent(event)
    end
    
    eventFrame:SetScript("OnEvent", function(self, event, ...)
        ns.DebugLog("EVENT:", event, ...)
    end)
end


