-- Core/Pages/Blackout.lua --
-- BUILDS the main page with the editor button --
local AC = _G.ArenaCore
if not AC then return end

local V = AC.Vanity

local function CreateBlackoutPage(parent)
    if V and V.EnsureMottoStrip then V:EnsureMottoStrip(parent) end

    -- Create a single group box
    local group = CreateFrame("Frame", nil, parent)
    -- Position below motto strip if it exists
    if parent._mottoStrip then
        group:SetPoint("TOPLEFT", parent._mottoStrip, "BOTTOMLEFT", 10, -18)
        group:SetPoint("TOPRIGHT", parent._mottoStrip, "BOTTOMRIGHT", -10, -18)
    else
        group:SetPoint("TOPLEFT", 10, -60)
        group:SetPoint("TOPRIGHT", -10, -60)
    end
    group:SetHeight(140)
    AC:HairlineGroupBox(group)
    AC:CreateStyledText(group, "GENERAL", 13, AC.COLORS.PRIMARY, "OVERLAY", ""):SetPoint("TOPLEFT", 20, -18)

    -- Button to open the spell editor
    local editorBtn = AC:CreateTexturedButton(group, 200, 32, "Configure Blackout Auras", "UI\\tab-purple-matte")
    editorBtn:SetPoint("TOP", -50, -50)
    editorBtn:SetScript("OnClick", function()
        if AC.OpenBlackoutEditor then
            AC:OpenBlackoutEditor()
        end
    end)

    -- Enable checkbox
    local function OnBlackoutToggle(checked)
        -- Update database
        if AC.DB and AC.DB.profile and AC.DB.profile.blackout then
            AC.DB.profile.blackout.enabled = checked
        end
        
        if checked then
            print("|cff8B45FFArena Core:|r Blackout feature enabled")
        else
            print("|cff8B45FFArena Core:|r Blackout feature disabled")
        end
    end

    local db = (AC.DB and AC.DB.profile and AC.DB.profile.blackout) or {}
    local enableCheckbox = AC:CreateFlatCheckbox(group, 20, db.enabled ~= false, OnBlackoutToggle)
    enableCheckbox:SetPoint("TOPLEFT", 20, -90)
    
    local enableLabel = AC:CreateStyledText(group, "Enable Blackout Feature", 12, AC.COLORS.TEXT_2, "OVERLAY", "")
    enableLabel:SetPoint("LEFT", enableCheckbox, "RIGHT", 8, 0)

    -- How Blackout Works info block
    local infoGroup = CreateFrame("Frame", nil, parent)
    infoGroup:SetPoint("TOPLEFT", group, "BOTTOMLEFT", 0, -20)
    infoGroup:SetPoint("TOPRIGHT", group, "BOTTOMRIGHT", 0, -20)
    infoGroup:SetHeight(380)
    AC:HairlineGroupBox(infoGroup)
    
    local infoTitle = AC:CreateStyledText(infoGroup, "HOW BLACKOUT WORKS", 13, AC.COLORS.PRIMARY, "OVERLAY", "")
    infoTitle:SetPoint("TOPLEFT", 20, -18)

    -- Main description
    local mainDesc = AC:CreateStyledText(infoGroup, 
        "Blackout is an awareness system that transforms how you react to enemy cooldowns in PvP. Instead of cluttering your screen with 357 WeakAuras tracking individual abilities, Blackout provides instant visual feedback through a simple, elegant solution.",
        12, AC.COLORS.TEXT_2, "OVERLAY", "")
    mainDesc:SetPoint("TOPLEFT", 20, -50)
    mainDesc:SetPoint("TOPRIGHT", -20, -50)
    mainDesc:SetJustifyH("LEFT")
    mainDesc:SetWordWrap(true)

    -- How it works section
    local howTitle = AC:CreateStyledText(infoGroup, "The System:", 13, AC.COLORS.TEXT, "OVERLAY", "")
    howTitle:SetPoint("TOPLEFT", 20, -110)

    local howDesc = AC:CreateStyledText(infoGroup,
        "ArenaCore monitors enemy spell casts and automatically applies visual effects when specific abilities are detected. " ..
        "The system tracks high-priority spells that typically require immediate attention or defensive responses.",
        11, AC.COLORS.TEXT_2, "OVERLAY", "")
    howDesc:SetPoint("TOPLEFT", 20, -135)
    howDesc:SetPoint("TOPRIGHT", -20, -135)
    howDesc:SetWordWrap(true)

    -- Benefits section
    local benefitsTitle = AC:CreateStyledText(infoGroup, "The Benefits:", 13, AC.COLORS.TEXT, "OVERLAY", "")
    benefitsTitle:SetPoint("TOPLEFT", 20, -210)

    local benefitsDesc = AC:CreateStyledText(infoGroup,
        "• Instant Reaction Time: See threats immediately without scanning multiple WeakAuras\n" ..
        "• Clean UI: No screen clutter - just essential information when you need it\n" ..
        "• Macro Awareness: Focus on big picture gameplay instead of micro managing cooldown details\n" ..
        "• Better Teamplay: Quickly communicate threats (\"Warrior is black = Trade cooldowns!\") and coordinate responses\n" ..
        "• Improved Performance: React faster to wall, peel, or assist your healer when enemies go offensive",
        11, AC.COLORS.TEXT_2, "OVERLAY", "")
    benefitsDesc:SetPoint("TOPLEFT", 20, -230)
    benefitsDesc:SetPoint("TOPRIGHT", -20, -230)
    benefitsDesc:SetJustifyH("LEFT")
    benefitsDesc:SetWordWrap(true)
end

AC:RegisterPage("Blackout", CreateBlackoutPage)
