-------------------------
-- AiB Crosshair System
-------------------------
-- Fadenkreuz in Bildschirmmitte (zeigt Charakter-Position)

AiB_Crosshair = AiB_Crosshair or {}
local Crosshair = AiB_Crosshair

-- v2.1.0.0: AiB_CrosshairConfig ist Alias auf AiB.db.profile.crosshair (AceDB)

-- Main Frame (Container)
local crosshairFrame = nil

-------------------------
-- Crosshair erstellen
-------------------------
function Crosshair:CreateCrosshair()
    if crosshairFrame then return end
    
    -- Outer Container (NICHT skaliert, fix positioniert)
    local outerFrame = CreateFrame("Frame", "AiB_CrosshairFrame_Outer", UIParent)
    outerFrame:SetSize(1, 1)  -- Minimale Größe
    outerFrame:SetPoint("CENTER", UIParent, "CENTER", 0, AiB_CrosshairConfig.yOffset or -46)
    outerFrame:SetFrameStrata("HIGH")
    Crosshair.OuterFrame = outerFrame  -- Speichern für späteren Zugriff
    
    -- Inner Container (WIRD skaliert, immer zentriert auf outer)
    crosshairFrame = CreateFrame("Frame", "AiB_CrosshairFrame", outerFrame)
    crosshairFrame:SetSize(40, 40)
    crosshairFrame:SetPoint("CENTER", outerFrame, "CENTER", 0, 0)  -- Immer CENTER!
    crosshairFrame:SetFrameLevel(100)
    
    -- *** Thickness HIER definieren (VOR dem ersten Gebrauch!) ***
    local thickness = AiB_CrosshairConfig.thickness or 5
    
    -- Vertical Border (schwarz, 7x27)
    local verticalBorder = crosshairFrame:CreateTexture(nil, "BACKGROUND")
    verticalBorder:SetTexture("Interface\\Buttons\\WHITE8x8")
    verticalBorder:SetSize(thickness + 2, 27)
    verticalBorder:SetPoint("CENTER", crosshairFrame, "CENTER", 0, 0)
    verticalBorder:SetVertexColor(0, 0, 0, 1)  -- Schwarz
    Crosshair.VerticalBorder = verticalBorder
    
    -- Horizontal Border (schwarz, 27x7)
    local horizontalBorder = crosshairFrame:CreateTexture(nil, "BACKGROUND")
    horizontalBorder:SetTexture("Interface\\Buttons\\WHITE8x8")
    horizontalBorder:SetSize(27, thickness + 2)
    horizontalBorder:SetPoint("CENTER", crosshairFrame, "CENTER", 0, 0)
    horizontalBorder:SetVertexColor(0, 0, 0, 1)  -- Schwarz
    Crosshair.HorizontalBorder = horizontalBorder
    
    -- Vertical Line (grün, 5x25)
    local verticalLine = crosshairFrame:CreateTexture(nil, "ARTWORK")
    verticalLine:SetTexture("Interface\\Buttons\\WHITE8x8")
    verticalLine:SetSize(thickness, 25)
    verticalLine:SetPoint("CENTER", crosshairFrame, "CENTER", 0, 0)
    verticalLine:SetVertexColor(0.129, 1.0, 0.0, 1)  -- Grün
    Crosshair.VerticalLine = verticalLine
    
    -- Horizontal Line (grün, 25x5)
    local horizontalLine = crosshairFrame:CreateTexture(nil, "ARTWORK")
    horizontalLine:SetTexture("Interface\\Buttons\\WHITE8x8")
    horizontalLine:SetSize(25, thickness)
    horizontalLine:SetPoint("CENTER", crosshairFrame, "CENTER", 0, 0)
    horizontalLine:SetVertexColor(0.129, 1.0, 0.0, 1)  -- Grün
    Crosshair.HorizontalLine = horizontalLine
    
    -- Event Frame für Combat Detection
    Crosshair.EventFrame = CreateFrame("Frame")
    Crosshair.EventFrame:RegisterEvent("PLAYER_REGEN_DISABLED")  -- Enter Combat
    Crosshair.EventFrame:RegisterEvent("PLAYER_REGEN_ENABLED")   -- Leave Combat
    Crosshair.EventFrame:SetScript("OnEvent", function(self, event)
        Crosshair:UpdateVisibility()
    end)
    
    Crosshair.CrosshairFrame = crosshairFrame
    
    -- Apply initial settings
    Crosshair:ApplySettings()
end

-------------------------
-- Settings anwenden
-------------------------
function Crosshair:ApplySettings()
    if not crosshairFrame then return end
    
    -- Scale - ohne ClearAllPoints!
    local scale = AiB_CrosshairConfig.scale or 1.0
    crosshairFrame:SetScale(scale)
    
    -- Alpha (Transparenz)
    local alpha = AiB_CrosshairConfig.alpha or 1.0
    
    -- *** Thickness Update ***
    local thickness = AiB_CrosshairConfig.thickness or 5
    if Crosshair.VerticalLine then
        Crosshair.VerticalLine:SetSize(thickness, 25)
    end
    if Crosshair.HorizontalLine then
        Crosshair.HorizontalLine:SetSize(25, thickness)
    end
    if Crosshair.VerticalBorder then
        Crosshair.VerticalBorder:SetSize(thickness + 2, 27)
    end
    if Crosshair.HorizontalBorder then
        Crosshair.HorizontalBorder:SetSize(27, thickness + 2)
    end
    
    -- *** Y-Offset Update ***
    local yOffset = AiB_CrosshairConfig.yOffset or -46
    if Crosshair.OuterFrame then
        Crosshair.OuterFrame:ClearAllPoints()
        Crosshair.OuterFrame:SetPoint("CENTER", UIParent, "CENTER", 0, yOffset)
    end
    
    -- Line Color
    local lineColor = AiB_CrosshairConfig.lineColor or {r = 0.129, g = 1.0, b = 0.0}
    if Crosshair.VerticalLine then
        Crosshair.VerticalLine:SetVertexColor(lineColor.r, lineColor.g, lineColor.b, alpha)
    end
    if Crosshair.HorizontalLine then
        Crosshair.HorizontalLine:SetVertexColor(lineColor.r, lineColor.g, lineColor.b, alpha)
    end
    
    -- Border Color
    local borderColor = AiB_CrosshairConfig.borderColor or {r = 0.0, g = 0.0, b = 0.0}
    if Crosshair.VerticalBorder then
        Crosshair.VerticalBorder:SetVertexColor(borderColor.r, borderColor.g, borderColor.b, alpha)
    end
    if Crosshair.HorizontalBorder then
        Crosshair.HorizontalBorder:SetVertexColor(borderColor.r, borderColor.g, borderColor.b, alpha)
    end
    
    -- Visibility
    Crosshair:UpdateVisibility()
end

-------------------------
-- Visibility Update
-------------------------
function Crosshair:UpdateVisibility()
    if not crosshairFrame then return end
    
    local enabled = AiB_CrosshairConfig.enabled
    local showOnlyInCombat = AiB_CrosshairConfig.showOnlyInCombat
    local inCombat = UnitAffectingCombat("player")
    
    if not enabled then
        crosshairFrame:Hide()
        return
    end
    
    if showOnlyInCombat and not inCombat then
        crosshairFrame:Hide()
    else
        crosshairFrame:Show()
    end
end

-------------------------
-- Initialize on Login
-------------------------
local initFrame = CreateFrame("Frame")
initFrame:RegisterEvent("PLAYER_LOGIN")
initFrame:SetScript("OnEvent", function()
    C_Timer.After(0.5, function()
        Crosshair:CreateCrosshair()
    end)
end)
