-- ============================================================================
-- TARGET HIGHLIGHT
-- ============================================================================
-- Shows a border on arena frames when you actually target someone.
-- ============================================================================

local AC = _G.ArenaCore
if not AC then return end

local function GetTargetBorderColor()
    local defaultR, defaultG, defaultB, defaultA = 1, 0.6, 0, 1

    if AC.DB and AC.DB.profile and AC.DB.profile.arenaFrames and AC.DB.profile.arenaFrames.sizing then
        local c = AC.DB.profile.arenaFrames.sizing.targetBorderColor
        if type(c) == "table" then
            local r = c.r or c[1] or defaultR
            local g = c.g or c[2] or defaultG
            local b = c.b or c[3] or defaultB
            local a = c.a or c[4] or defaultA
            return r, g, b, a
        end
    end

    return defaultR, defaultG, defaultB, defaultA
end

local TargetHighlight = {}
AC.TargetHighlight = TargetHighlight

-- ============================================================================
-- CONFIGURATION
-- ============================================================================

local FADE_DURATION = 0.15  -- Fade in/out duration in seconds
local UPDATE_INTERVAL = 0.05  -- Animation update interval

-- ============================================================================
-- LOCAL VARIABLES
-- ============================================================================

local eventFrame = nil
local activeTarget = nil  -- Currently targeted arena unit
local animationTimers = {}  -- [unit] = {startTime, startAlpha, targetAlpha, duration}

-- ============================================================================
-- HELPER FUNCTIONS
-- ============================================================================

local function GetFrameForUnit(unit)
    -- Extract arena index from unit ("arena1" -> 1)
    local index = unit and tonumber(unit:match("arena(%d+)"))
    
    -- MasterFrameManager first.
    if index and AC.MasterFrameManager and AC.MasterFrameManager.frames then
        local frame = AC.MasterFrameManager.frames[index]
        if frame then
            return frame
        end
    end
    
    -- Fallback: match by `unit`.
    if AC.MasterFrameManager and AC.MasterFrameManager.frames then
        for i = 1, 3 do
            local frame = AC.MasterFrameManager.frames[i]
            if frame and frame.unit == unit then
                return frame
            end
        end
    end
    
    -- Fallback: old `arenaFrames`.
    if AC.arenaFrames then
        if index then
            local frame = AC.arenaFrames[index]
            if frame then
                return frame
            end
        end
        for i = 1, 3 do
            local frame = AC.arenaFrames[i]
            if frame and frame.unit == unit then
                return frame
            end
        end
    end
    
    return nil
end

local function GetSettings()
    if AC.DB and AC.DB.profile and AC.DB.profile.targetHighlight then
        return AC.DB.profile.targetHighlight
    end
    -- Defaults if settings don't exist yet.
    return {
        enabled = true,
        color = {r = 1, g = 0.7, b = 0, a = 1},  -- Gold/orange default
        fadeEnabled = true,
        fadeDuration = 0.15
    }
end

-- ============================================================================
-- ANIMATION (smooth fade)
-- ============================================================================

local function StartFadeAnimation(unit, targetAlpha)
    local frame = GetFrameForUnit(unit)
    if not frame or not frame.targetOutline then return end
    
    local settings = GetSettings()
    local duration = settings.fadeEnabled and (settings.fadeDuration or FADE_DURATION) or 0
    
    -- Instant change if fade is disabled.
    if duration <= 0 then
        frame.targetOutline:SetAlpha(targetAlpha)
        animationTimers[unit] = nil
        return
    end
    
    -- Start animation.
    local currentAlpha = frame.targetOutline:GetAlpha()
    animationTimers[unit] = {
        startTime = GetTime(),
        startAlpha = currentAlpha,
        targetAlpha = targetAlpha,
        duration = duration
    }
end

local function UpdateAnimations()
    local currentTime = GetTime()
    
    for unit, anim in pairs(animationTimers) do
        local frame = GetFrameForUnit(unit)
        if frame and frame.targetOutline then
            local elapsed = currentTime - anim.startTime
            local progress = math.min(elapsed / anim.duration, 1.0)
            
            -- Smooth easing (ease-out).
            progress = 1 - math.pow(1 - progress, 3)
            
            local newAlpha = anim.startAlpha + (anim.targetAlpha - anim.startAlpha) * progress
            frame.targetOutline:SetAlpha(newAlpha)
            
            -- Animation complete.
            if progress >= 1.0 then
                animationTimers[unit] = nil
            end
        else
            -- Frame doesn't exist.
            animationTimers[unit] = nil
        end
    end
end

-- ============================================================================
-- TARGET TRACKING
-- ============================================================================

local function UpdateTargetHighlight()
    local settings = GetSettings()
    if not settings.enabled then 
        -- If disabled, hide everything.
        for i = 1, 3 do
            local frame = GetFrameForUnit("arena" .. i)
            if frame and frame.targetOutline then
                frame.targetOutline:SetAlpha(0)
            end
        end
        activeTarget = nil
        return 
    end
    
    -- No target, no highlight.
    if not UnitExists("target") then
        -- Clear any lingering highlight.
        if activeTarget then
            local frame = GetFrameForUnit(activeTarget)
            if frame and frame.targetOutline then
                frame.targetOutline:SetAlpha(0)
            end
            activeTarget = nil
        end
        return
    end
    
    local newTarget = nil
    
    -- Find which arena unit is targeted.
    for i = 1, 3 do
        local arenaUnit = "arena" .. i
        if UnitExists(arenaUnit) and UnitIsUnit(arenaUnit, "target") then
            newTarget = arenaUnit
            break
        end
    end
    
    -- Target changed.
    if newTarget ~= activeTarget then
        -- DEBUG: print("|cffFFA500[TargetHighlight]|r Target changed from", activeTarget or "nil", "to", newTarget or "nil")
        
        -- Fade out old target.
        if activeTarget then
            -- DEBUG: print("|cffFFA500[TargetHighlight]|r Fading out old target:", activeTarget)
            StartFadeAnimation(activeTarget, 0)
        end
        
        -- Fade in new target.
        if newTarget then
            local frame = GetFrameForUnit(newTarget)
            if frame and frame.targetOutline then
                local r, g, b, a = GetTargetBorderColor()
                if frame.targetOutline.top then frame.targetOutline.top:SetVertexColor(r, g, b, a) end
                if frame.targetOutline.bottom then frame.targetOutline.bottom:SetVertexColor(r, g, b, a) end
                if frame.targetOutline.left then frame.targetOutline.left:SetVertexColor(r, g, b, a) end
                if frame.targetOutline.right then frame.targetOutline.right:SetVertexColor(r, g, b, a) end
                
                -- Fade in.
                -- DEBUG: print("|cffFFA500[TargetHighlight]|r Fading in new target:", newTarget)
                StartFadeAnimation(newTarget, 1.0)
            else
                -- DEBUG: print("|cffFFA500[TargetHighlight]|r Frame or outline missing!")
            end
        end
        
        activeTarget = newTarget
    else
        -- DEBUG: Show why no change occurred (DISABLED)
        --[[
        if activeTarget then
            print("|cffFFA500[TargetHighlight]|r No change - still targeting:", activeTarget)
        else
            print("|cffFFA500[TargetHighlight]|r No change - no active target")
        end
        --]]
    end
end

-- ============================================================================
-- EVENT HANDLERS
-- ============================================================================

local function OnEvent(self, event, ...)
    if event == "PLAYER_TARGET_CHANGED" then
        UpdateTargetHighlight()
        
    elseif event == "PLAYER_REGEN_DISABLED" then
        -- Entering combat - continue tracking
        UpdateTargetHighlight()
        
    elseif event == "PLAYER_REGEN_ENABLED" then
        -- Leaving combat - continue tracking
        UpdateTargetHighlight()
        
    elseif event == "ARENA_PREP_OPPONENT_SPECIALIZATIONS" then
        -- Prep room: kill highlights immediately (no animation).
        activeTarget = nil
        animationTimers = {}  -- Clear all pending animations
        for i = 1, 3 do
            local frame = GetFrameForUnit("arena" .. i)
            if frame and frame.targetOutline then
                frame.targetOutline:SetAlpha(0)
            end
        end
        
    elseif event == "ARENA_OPPONENT_UPDATE" then
        -- Arena state changed: clear first, then check target.
        -- This is kind of important I guess: don't auto-show highlights unless you're targeting.
        C_Timer.After(0.1, function()
            -- Only update if we have a valid target.
            if UnitExists("target") then
                UpdateTargetHighlight()
            else
                -- No target.
                activeTarget = nil
                for i = 1, 3 do
                    local frame = GetFrameForUnit("arena" .. i)
                    if frame and frame.targetOutline then
                        frame.targetOutline:SetAlpha(0)
                    end
                end
            end
        end)
        
    elseif event == "PLAYER_ENTERING_WORLD" then
        -- Reload/zone change: clear everything.
        activeTarget = nil
        animationTimers = {}
        for i = 1, 3 do
            local frame = GetFrameForUnit("arena" .. i)
            if frame and frame.targetOutline then
                frame.targetOutline:SetAlpha(0)
            end
        end
    end
end

-- ============================================================================
-- FRAME CREATION
-- ============================================================================

function TargetHighlight:CreateOutline(frame)
    if frame.targetOutline then
        return frame.targetOutline
    end
    
    -- Container for border textures.
    local container = CreateFrame("Frame", nil, frame)
    container:SetFrameLevel(frame:GetFrameLevel() + 10)  -- Above everything
    
    -- Check for theme insets and extend the outline a bit if needed.
    local outlineOffset = 0
    if AC.ArenaFrameThemes then
        local currentTheme = AC.ArenaFrameThemes:GetCurrentTheme()
        local theme = AC.ArenaFrameThemes.themes and AC.ArenaFrameThemes.themes[currentTheme]
        if theme and theme.background and theme.background.enabled and theme.background.insets then
            -- The 1500 Special has insets = {-2, 2, 2, -2} (extends 2px left/right)
            -- Extend outline by 4px to make it thicker and more visible
            outlineOffset = 4
        end
    end
    
    -- Position container to cover entire frame.
    container:SetAllPoints(frame)
    
    -- Use 4 solid edges for a thick, obvious border.
    local borderThickness = 3  -- Default thickness
    if AC.DB and AC.DB.profile and AC.DB.profile.arenaFrames and AC.DB.profile.arenaFrames.sizing then
        borderThickness = AC.DB.profile.arenaFrames.sizing.targetBorderThickness or 3
    end
    local r, g, b, a = GetTargetBorderColor()
    
    -- Create 4 border edges (top, bottom, left, right).
    local top = container:CreateTexture(nil, "OVERLAY", nil, 7)
    top:SetColorTexture(r, g, b, a or 1)
    top:SetPoint("TOPLEFT", container, "TOPLEFT", 0, 0)
    top:SetPoint("TOPRIGHT", container, "TOPRIGHT", 0, 0)
    top:SetHeight(borderThickness)
    
    local bottom = container:CreateTexture(nil, "OVERLAY", nil, 7)
    bottom:SetColorTexture(r, g, b, a or 1)
    bottom:SetPoint("BOTTOMLEFT", container, "BOTTOMLEFT", 0, 0)
    bottom:SetPoint("BOTTOMRIGHT", container, "BOTTOMRIGHT", 0, 0)
    bottom:SetHeight(borderThickness)
    
    local left = container:CreateTexture(nil, "OVERLAY", nil, 7)
    left:SetColorTexture(r, g, b, a or 1)
    left:SetPoint("TOPLEFT", container, "TOPLEFT", 0, 0)
    left:SetPoint("BOTTOMLEFT", container, "BOTTOMLEFT", 0, 0)
    left:SetWidth(borderThickness)
    
    local right = container:CreateTexture(nil, "OVERLAY", nil, 7)
    right:SetColorTexture(r, g, b, a or 1)
    right:SetPoint("TOPRIGHT", container, "TOPRIGHT", 0, 0)
    right:SetPoint("BOTTOMRIGHT", container, "BOTTOMRIGHT", 0, 0)
    right:SetWidth(borderThickness)
    
    -- Store references.
    container.top = top
    container.bottom = bottom
    container.left = left
    container.right = right
    container:SetAlpha(0)  -- Hidden by default
    
    frame.targetOutline = container
    
    return container
end

function TargetHighlight:UpdateOutlineSize(frame)
    if not frame or not frame.targetOutline then return end
    
    -- Get current frame size.
    local width = frame:GetWidth()
    local height = frame:GetHeight()
    
    -- Cover the whole frame.
    frame.targetOutline:ClearAllPoints()
    frame.targetOutline:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)
    frame.targetOutline:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0)
end

function TargetHighlight:RefreshAll()
    -- Refresh all outlines (settings change).
    for i = 1, 3 do
        local frame = GetFrameForUnit("arena" .. i)
        if frame then
            self:UpdateOutlineSize(frame)
        end
    end
    
    -- Update current target.
    UpdateTargetHighlight()
end

-- ============================================================================
-- INITIALIZATION
-- ============================================================================

function TargetHighlight:Initialize()
    -- Create event frame.
    if not eventFrame then
        eventFrame = CreateFrame("Frame")
        eventFrame:SetScript("OnEvent", OnEvent)
        
        -- Register events.
        eventFrame:RegisterEvent("PLAYER_TARGET_CHANGED")
        eventFrame:RegisterEvent("PLAYER_REGEN_DISABLED")
        eventFrame:RegisterEvent("PLAYER_REGEN_ENABLED")
        eventFrame:RegisterEvent("ARENA_PREP_OPPONENT_SPECIALIZATIONS")
        eventFrame:RegisterEvent("ARENA_OPPONENT_UPDATE")
        eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
    end
    
    -- Start animation ticker.
    if not self.animationTicker then
        self.animationTicker = C_Timer.NewTicker(UPDATE_INTERVAL, UpdateAnimations)
    end
    
    -- Create outlines for existing frames.
    for i = 1, 3 do
        local frame = GetFrameForUnit("arena" .. i)
        if frame and not frame.targetOutline then
            self:CreateOutline(frame)
        end
    end
    
    -- This is kind of important I guess: don't auto-highlight on init.
    -- Clear any lingering highlights from previous session.
    activeTarget = nil
    for i = 1, 3 do
        local frame = GetFrameForUnit("arena" .. i)
        if frame and frame.targetOutline then
            frame.targetOutline:SetAlpha(0)
        end
    end
end

-- ============================================================================
-- PUBLIC API
-- ============================================================================

function TargetHighlight:Enable()
    local settings = GetSettings()
    settings.enabled = true
    self:RefreshAll()
end

function TargetHighlight:Disable()
    local settings = GetSettings()
    settings.enabled = false
    
    -- Hide all outlines
    for i = 1, 3 do
        StartFadeAnimation("arena" .. i, 0)
    end
    activeTarget = nil
end

function TargetHighlight:SetColor(r, g, b, a)
    local settings = GetSettings()
    settings.color = {r = r, g = g, b = b, a = a or 1}
    
    -- Update active target immediately
    if activeTarget then
        local frame = GetFrameForUnit(activeTarget)
        if frame and frame.targetOutline and frame.targetOutline.top then
            frame.targetOutline.top:SetVertexColor(r, g, b, a or 1)
            frame.targetOutline.bottom:SetVertexColor(r, g, b, a or 1)
            frame.targetOutline.left:SetVertexColor(r, g, b, a or 1)
            frame.targetOutline.right:SetVertexColor(r, g, b, a or 1)
        end
    end
end

function TargetHighlight:SetFadeDuration(duration)
    local settings = GetSettings()
    settings.fadeDuration = duration
end

function TargetHighlight:SetFadeEnabled(enabled)
    local settings = GetSettings()
    settings.fadeEnabled = enabled
end

function TargetHighlight:ClearAll()
    -- Clear all target highlights.
    activeTarget = nil
    for i = 1, 3 do
        local frame = GetFrameForUnit("arena" .. i)
        if frame and frame.targetOutline then
            frame.targetOutline:SetAlpha(0)
        end
        animationTimers["arena" .. i] = nil
    end
end

-- ============================================================================
-- AUTO-INITIALIZE
-- ============================================================================

-- Auto-initialize when ArenaCore is ready.
if AC.MasterFrameManager then
    C_Timer.After(0.5, function()
        TargetHighlight:Initialize()
    end)
else
    -- Wait for MasterFrameManager.
    local initFrame = CreateFrame("Frame")
    initFrame:RegisterEvent("ADDON_LOADED")
    initFrame:SetScript("OnEvent", function(self, event, addonName)
        if addonName == "ArenaCore" and AC.MasterFrameManager then
            C_Timer.After(0.5, function()
                TargetHighlight:Initialize()
            end)
            self:UnregisterAllEvents()
        end
    end)
end

-- Debug command removed for production
