local _, ns = ...

-- Mirror Timers module (breath, fatigue, etc.)
local MirrorTimers = {}
ns.MirrorTimers = MirrorTimers

local STATUSBAR_TEXTURE = "Interface\\AddOns\\nCastbar\\Media\\statusbarTexture"
local BORDER_TEXTURE = "Interface\\Tooltips\\UI-Tooltip-Border"

-- WoW removed this constant, so we define it ourselves
local MIRRORTIMER_NUMTIMERS = MIRRORTIMER_NUMTIMERS or 3

-- Style mirror timer bars
function MirrorTimers:StyleBar(bar, index)
    local config = nCastbarDB.profile.mirrorTimers
    if not config or not config.enabled then return end
    
    -- Already styled?
    if bar.styled then return end
    
    ns.Debug("Styling mirror timer", index)
    
    -- Set size and scale
    bar:SetParent(UIParent)
    bar:SetSize(config.width, config.height)
    bar:SetScale(config.scale)
    
    -- Adjust position for multiple timers
    if index > 1 then
        local point, relativeTo, relativePoint, xOfs, yOfs = bar:GetPoint()
        bar:SetPoint(point, relativeTo, relativePoint, xOfs, yOfs - 15)
    end
    
    -- Get the statusbar
    local statusbar = _G["MirrorTimer"..index.."StatusBar"]
    if statusbar then
        statusbar:SetStatusBarTexture(STATUSBAR_TEXTURE)
        statusbar:SetAllPoints(bar)
    end
    
    -- Background
    local backdrop = select(1, bar:GetRegions())
    if backdrop and backdrop:GetObjectType() == "Texture" then
        backdrop:SetColorTexture(0, 0, 0, 0.5)
        backdrop:SetAllPoints(bar)
    end
    
    -- Border
    local border = _G["MirrorTimer"..index.."Border"]
    if border then
        border:Hide()
    end
    
    -- Create new border
    if not bar.customBorder then
        bar.customBorder = CreateFrame("Frame", nil, bar, "BackdropTemplate")
        bar.customBorder:SetFrameLevel(bar:GetFrameLevel() + 1)
        bar.customBorder:SetPoint("TOPLEFT", bar, "TOPLEFT", -1, 1)
        bar.customBorder:SetPoint("BOTTOMRIGHT", bar, "BOTTOMRIGHT", 1, -1)
        bar.customBorder:SetBackdrop({
            edgeFile = BORDER_TEXTURE,
            edgeSize = 16,
            insets = {left = 4, right = 4, top = 4, bottom = 4}
        })
        bar.customBorder:SetBackdropBorderColor(1, 1, 1, 1)
    end
    
    -- Text
    local text = _G["MirrorTimer"..index.."Text"]
    if text then
        text:ClearAllPoints()
        text:SetPoint("CENTER", bar)
        text:SetFont(STANDARD_TEXT_FONT, 13)
    end
    
    bar.styled = true
end

-- Initialize mirror timers
function MirrorTimers:Initialize()
    if not nCastbarDB.profile.mirrorTimers or not nCastbarDB.profile.mirrorTimers.enabled then
        ns.Debug("Mirror timers disabled")
        return
    end
    
    ns.Debug("Initializing mirror timers...")
    
    -- Style existing timers
    for i = 1, MIRRORTIMER_NUMTIMERS do
        local bar = _G["MirrorTimer"..i]
        if bar then
            self:StyleBar(bar, i)
        end
    end
    
    -- Watch for new timers
    local frame = CreateFrame("Frame")
    frame:RegisterEvent("MIRROR_TIMER_START")
    frame:SetScript("OnEvent", function(self, event)
        for i = 1, MIRRORTIMER_NUMTIMERS do
            local bar = _G["MirrorTimer"..i]
            if bar and not bar.styled then
                MirrorTimers:StyleBar(bar, i)
            end
        end
    end)
    
    ns.Debug("Mirror timers initialized")
end

ns.Debug("MirrorTimers.lua loaded")
