-------------------------
-- AiB Cursor Settings (MODERN ColorPicker API)
-------------------------

-- Initialisiere Config sofort mit Farben
AiB_CursorConfig = AiB_CursorConfig or {
    enabled = true,
    scale = 1.0,
    transparency = 1.0,
    showOnlyInCombat = false,  -- DEFAULT: ALWAYS VISIBLE (nicht nur im Kampf!)
    enableTrail = false,
    trailDuration = 0.4,
    trailDensity = 0.008,
    trailScale = 1.0,
    colors = {
        mainRing = {r = 1.0, g = 1.0, b = 1.0},
        gcdRing = {r = 1.0, g = 1.0, b = 0.0},
        castRing = {r = 1.0, g = 0.8, b = 0.0},
        trail = {r = 1.0, g = 1.0, b = 1.0},  -- Trail-Farbe (weiß)
    }
}

-- Ensure colors exist
AiB_CursorConfig.colors = AiB_CursorConfig.colors or {
    mainRing = {r = 1.0, g = 1.0, b = 1.0},
    gcdRing = {r = 1.0, g = 1.0, b = 0.0},
    castRing = {r = 1.0, g = 0.8, b = 0.0},
    trail = {r = 1.0, g = 1.0, b = 1.0},
}

-- Ensure trail color exists
if not AiB_CursorConfig.colors.trail then
    AiB_CursorConfig.colors.trail = {r = 1.0, g = 1.0, b = 1.0}
end

-- Ensure trail settings exist
AiB_CursorConfig.trailDuration = AiB_CursorConfig.trailDuration or 0.4
AiB_CursorConfig.trailDensity = AiB_CursorConfig.trailDensity or 0.008
AiB_CursorConfig.trailScale = AiB_CursorConfig.trailScale or 1.0

-- FORCE showOnlyInCombat to FALSE - rings should always be visible!
AiB_CursorConfig.showOnlyInCombat = false

-- CLEANUP: Remove old ring assignment values (we don't use them anymore)
AiB_CursorConfig.innerRing = nil
AiB_CursorConfig.mainRing = nil
AiB_CursorConfig.outerRing = nil
AiB_CursorConfig.reticle = nil
AiB_CursorConfig.reticleScale = nil

-- Warte bis AiB geladen ist
local frame = CreateFrame("Frame")
frame:RegisterEvent("PLAYER_LOGIN")
frame:SetScript("OnEvent", function()
    C_Timer.After(2, function()
        
        if not _G.AiB then
            return
        end
        
        _G.AiB.CursorSettings = {}
        
        function _G.AiB.CursorSettings:AddToSettingsPanel(settingsFrame, startY)
            
            local FONT = "Fonts\\FRIZQT__.TTF"
            local yOffset = startY
            
            -- Title
            local title = settingsFrame:CreateFontString(nil, "OVERLAY")
            title:SetFont(FONT, 16, "OUTLINE")
            title:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            title:SetText("AiB Cursor")
            title:SetTextColor(1, 0.8, 0, 1)
            yOffset = yOffset - 40
            
            -- === BASIC SETTINGS ===
            local basicHeader = settingsFrame:CreateFontString(nil, "OVERLAY")
            basicHeader:SetFont(FONT, 14, "OUTLINE")
            basicHeader:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            basicHeader:SetText("Basis-Einstellungen")
            basicHeader:SetTextColor(1, 0.8, 0, 1)
            yOffset = yOffset - 35
            
            -- Enable Checkbox
            local enableText = settingsFrame:CreateFontString(nil, "OVERLAY")
            enableText:SetFont(FONT, 12)
            enableText:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            enableText:SetText("Cursor Ringe aktivieren")
            enableText:SetTextColor(1, 1, 1, 1)
            
            local enableCB = CreateFrame("CheckButton", nil, settingsFrame, "UICheckButtonTemplate")
            enableCB:SetPoint("LEFT", enableText, "RIGHT", 5, 0)
            enableCB:SetSize(24, 24)
            enableCB:SetChecked(AiB_CursorConfig.enabled)
            enableCB:SetScript("OnClick", function(self)
                AiB_CursorConfig.enabled = self:GetChecked()
                -- Live-Update!
                if AiB_Cursor and AiB_Cursor.ApplySettings then
                    AiB_Cursor:ApplySettings()
                end
            end)
            yOffset = yOffset - 30
            
            -- Combat Only Checkbox
            local combatText = settingsFrame:CreateFontString(nil, "OVERLAY")
            combatText:SetFont(FONT, 12)
            combatText:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            combatText:SetText("Nur im Kampf anzeigen")
            combatText:SetTextColor(1, 1, 1, 1)
            
            local combatCB = CreateFrame("CheckButton", nil, settingsFrame, "UICheckButtonTemplate")
            combatCB:SetPoint("LEFT", combatText, "RIGHT", 5, 0)
            combatCB:SetSize(24, 24)
            combatCB:SetChecked(AiB_CursorConfig.showOnlyInCombat)
            combatCB:SetScript("OnClick", function(self)
                AiB_CursorConfig.showOnlyInCombat = self:GetChecked()
                -- Live-Update!
                if AiB_Cursor and AiB_Cursor.ApplySettings then
                    AiB_Cursor:ApplySettings()
                end
            end)
            yOffset = yOffset - 30
            
            -- Transparency Slider (moved from Visibility section)
            local transparencyLabel = settingsFrame:CreateFontString(nil, "OVERLAY")
            transparencyLabel:SetFont(FONT, 12)
            transparencyLabel:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            transparencyLabel:SetText(string.format("Transparenz: %.0f%%", (AiB_CursorConfig.transparency or 1.0) * 100))
            transparencyLabel:SetTextColor(1, 1, 1, 1)
            yOffset = yOffset - 25
            
            local transparencySlider = CreateFrame("Slider", nil, settingsFrame, "OptionsSliderTemplate")
            transparencySlider:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            transparencySlider:SetMinMaxValues(0.1, 1.0)
            transparencySlider:SetValue(AiB_CursorConfig.transparency or 1.0)
            transparencySlider:SetValueStep(0.05)
            transparencySlider:SetObeyStepOnDrag(true)
            transparencySlider:SetWidth(250)
            transparencySlider:SetScript("OnValueChanged", function(self, value)
                AiB_CursorConfig.transparency = value
                transparencyLabel:SetText(string.format("Transparenz: %.0f%%", value * 100))
                if AiB_Cursor_CursorFrame then
                    AiB_Cursor_CursorFrame:SetAlpha(value)
                end
            end)
            yOffset = yOffset - 40
            
            -- Scale Slider
            local scaleLabel = settingsFrame:CreateFontString(nil, "OVERLAY")
            scaleLabel:SetFont(FONT, 12)
            scaleLabel:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            scaleLabel:SetText("Größe: " .. string.format("%.1f", AiB_CursorConfig.scale))
            scaleLabel:SetTextColor(1, 1, 1, 1)
            yOffset = yOffset - 25
            
            local scaleSlider = CreateFrame("Slider", nil, settingsFrame, "OptionsSliderTemplate")
            scaleSlider:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            scaleSlider:SetMinMaxValues(0.5, 2.0)
            scaleSlider:SetValue(AiB_CursorConfig.scale)
            scaleSlider:SetValueStep(0.1)
            scaleSlider:SetObeyStepOnDrag(true)
            scaleSlider:SetWidth(250)
            scaleSlider:SetScript("OnValueChanged", function(self, value)
                value = math.floor(value * 10 + 0.5) / 10
                AiB_CursorConfig.scale = value
                scaleLabel:SetText("Größe: " .. string.format("%.1f", value))
                -- Live-Update!
                if AiB_Cursor and AiB_Cursor.ApplySettings then
                    AiB_Cursor:ApplySettings()
                end
            end)
            yOffset = yOffset - 50
            
            -- === COLOR SETTINGS (MODERN API) ===
            local colorHeader = settingsFrame:CreateFontString(nil, "OVERLAY")
            colorHeader:SetFont(FONT, 14, "OUTLINE")
            colorHeader:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            colorHeader:SetText("Ring-Farben")
            colorHeader:SetTextColor(1, 0.8, 0, 1)
            yOffset = yOffset - 35
            
            -- Helper function with MODERN ColorPicker API
            local function MakeColorButton(xPos, yPos, labelText, colorTable, applyFunc)
                local label = settingsFrame:CreateFontString(nil, "OVERLAY")
                label:SetFont(FONT, 11)
                label:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", xPos, yPos)
                label:SetText(labelText)
                label:SetTextColor(0.9, 0.9, 0.9, 1)
                
                local btn = CreateFrame("Button", nil, settingsFrame)
                btn:SetSize(24, 24)
                btn:SetPoint("LEFT", label, "RIGHT", 8, 0)
                btn:EnableMouse(true)
                
                local swatch = btn:CreateTexture(nil, "OVERLAY")  -- OVERLAY wie im Original!
                swatch:SetAllPoints()
                swatch:SetColorTexture(1, 1, 1)  -- Weiße Basis
                btn.swatch = swatch  -- Speichere Referenz
                
                local border = btn:CreateTexture(nil, "BACKGROUND")
                border:SetPoint("TOPLEFT", -1, 1)
                border:SetPoint("BOTTOMRIGHT", 1, -1)
                border:SetColorTexture(0.5, 0.5, 0.5)  -- Grauer Border
                
                local highlight = btn:CreateTexture(nil, "HIGHLIGHT")
                highlight:SetAllPoints()
                highlight:SetColorTexture(1, 1, 1, 0.3)
                
                -- Update button color function (like in original)
                local function UpdateButtonColor()
                    local c = colorTable
                    if c then
                        swatch:SetVertexColor(c.r, c.g, c.b)
                    else
                        swatch:SetVertexColor(1, 1, 1)
                    end
                end
                
                btn:SetScript("OnClick", function()
                    
                    local r, g, b = colorTable.r, colorTable.g, colorTable.b
                    
                    local info = {
                        r = r, g = g, b = b,
                        hasOpacity = false,
                        swatchFunc = function()
                            local newR, newG, newB = ColorPickerFrame:GetColorRGB()
                            colorTable.r = newR
                            colorTable.g = newG
                            colorTable.b = newB
                            UpdateButtonColor()  -- Update the button display!
                            if applyFunc then 
                                applyFunc(newR, newG, newB)
                            end
                        end,
                        cancelFunc = function()
                            colorTable.r = r
                            colorTable.g = g
                            colorTable.b = b
                            UpdateButtonColor()  -- Update the button display!
                            if applyFunc then 
                                applyFunc(r, g, b)
                            end
                        end,
                    }
                    
                    ColorPickerFrame:SetupColorPickerAndShow(info)
                end)
                
                btn:SetScript("OnEnter", function(self)
                    GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
                    GameTooltip:SetText("Klicken um Farbe zu ändern", 1, 1, 1)
                    GameTooltip:Show()
                end)
                btn:SetScript("OnLeave", function() GameTooltip:Hide() end)
                
                -- Initial color update
                UpdateButtonColor()
                
                -- Store for later access (like in original)
                btn.UpdateColor = UpdateButtonColor
            end
            
            local col1X = 20
            local col2X = 280
            
            -- Main Ring Color
            MakeColorButton(col1X, yOffset, "Main Ring:", AiB_CursorConfig.colors.mainRing, function(r, g, b)
                if AiB_Cursor_CursorFrame and AiB_Cursor_CursorFrame.MainRing then
                    AiB_Cursor_CursorFrame.MainRing:SetVertexColor(r, g, b, 1)
                end
            end)
            
            -- GCD Ring Color
            MakeColorButton(col2X, yOffset, "GCD Ring:", AiB_CursorConfig.colors.gcdRing, function(r, g, b)
                if AiB_Cursor and AiB_Cursor.GCDCooldownFrame then
                    AiB_Cursor.GCDCooldownFrame:SetSwipeColor(r, g, b, 1)
                end
            end)
            yOffset = yOffset - 30
            
            -- Cast Ring Color
            MakeColorButton(col1X, yOffset, "Cast Ring:", AiB_CursorConfig.colors.castRing, function(r, g, b)
                if AiB_Cursor and AiB_Cursor.CastFrame then
                    AiB_Cursor.CastFrame:SetSwipeColor(r, g, b, 1)
                end
            end)
            yOffset = yOffset - 45
            
            -- === TRAIL SETTINGS ===
            local trailHeader = settingsFrame:CreateFontString(nil, "OVERLAY")
            trailHeader:SetFont(FONT, 14, "OUTLINE")
            trailHeader:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            trailHeader:SetText("Maus-Trail")
            trailHeader:SetTextColor(1, 0.8, 0, 1)
            yOffset = yOffset - 35
            
            -- Enable Trail Checkbox
            local enableTrailText = settingsFrame:CreateFontString(nil, "OVERLAY")
            enableTrailText:SetFont(FONT, 12)
            enableTrailText:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            enableTrailText:SetText("Trail aktivieren")
            enableTrailText:SetTextColor(1, 1, 1, 1)
            
            local enableTrailCB = CreateFrame("CheckButton", nil, settingsFrame, "UICheckButtonTemplate")
            enableTrailCB:SetPoint("LEFT", enableTrailText, "RIGHT", 5, 0)
            enableTrailCB:SetSize(24, 24)
            enableTrailCB:SetChecked(AiB_CursorConfig.enableTrail or false)
            enableTrailCB:SetScript("OnClick", function(self)
                AiB_CursorConfig.enableTrail = self:GetChecked()
            end)
            yOffset = yOffset - 35
            
            -- Trail Color
            MakeColorButton(20, yOffset, "Trail-Farbe:", AiB_CursorConfig.colors.trail or {r = 1, g = 1, b = 1}, function(r, g, b)
                -- Set trail color mode to custom when color is changed
                if not AiB_CursorConfig.colors.trail then
                    AiB_CursorConfig.colors.trail = {}
                end
                AiB_CursorConfig.colors.trail.r = r
                AiB_CursorConfig.colors.trail.g = g
                AiB_CursorConfig.colors.trail.b = b
                AiB_CursorConfig.trailColorMode = "custom"
            end)
            yOffset = yOffset - 35
            
            -- Trail Duration Slider
            local trailDurationLabel = settingsFrame:CreateFontString(nil, "OVERLAY")
            trailDurationLabel:SetFont(FONT, 12)
            trailDurationLabel:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            trailDurationLabel:SetText("Trail-Dauer: 0.4s")
            trailDurationLabel:SetTextColor(1, 1, 1, 1)
            yOffset = yOffset - 25
            
            local trailDurationSlider = CreateFrame("Slider", nil, settingsFrame, "OptionsSliderTemplate")
            trailDurationSlider:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            trailDurationSlider:SetMinMaxValues(0.2, 1.0)
            trailDurationSlider:SetValue(AiB_CursorConfig.trailDuration or 0.4)
            trailDurationSlider:SetValueStep(0.05)
            trailDurationSlider:SetObeyStepOnDrag(true)
            trailDurationSlider:SetWidth(250)
            trailDurationSlider:SetScript("OnValueChanged", function(self, value)
                AiB_CursorConfig.trailDuration = value
                trailDurationLabel:SetText(string.format("Trail-Dauer: %.2fs", value))
            end)
            yOffset = yOffset - 40
            
            -- Trail Density Slider
            local trailDensityLabel = settingsFrame:CreateFontString(nil, "OVERLAY")
            trailDensityLabel:SetFont(FONT, 12)
            trailDensityLabel:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            trailDensityLabel:SetText("Trail-Dichte: 0.008s")
            trailDensityLabel:SetTextColor(1, 1, 1, 1)
            yOffset = yOffset - 25
            
            local trailDensitySlider = CreateFrame("Slider", "AiB_TrailDensitySlider", settingsFrame, "OptionsSliderTemplate")
            trailDensitySlider:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            trailDensitySlider:SetMinMaxValues(0.004, 0.02)
            trailDensitySlider:SetValue(AiB_CursorConfig.trailDensity or 0.008)
            trailDensitySlider:SetValueStep(0.001)
            trailDensitySlider:SetObeyStepOnDrag(true)
            trailDensitySlider:SetWidth(250)
            _G[trailDensitySlider:GetName() .. "Low"]:SetText("Dicht")  -- 0.004 = dichter Trail
            _G[trailDensitySlider:GetName() .. "High"]:SetText("Spärlich")  -- 0.02 = spärlicher Trail
            trailDensitySlider:SetScript("OnValueChanged", function(self, value)
                AiB_CursorConfig.trailDensity = value
                trailDensityLabel:SetText(string.format("Trail-Dichte: %.3fs", value))
            end)
            yOffset = yOffset - 40
            
            -- Trail Scale Slider
            local trailScaleLabel = settingsFrame:CreateFontString(nil, "OVERLAY")
            trailScaleLabel:SetFont(FONT, 12)
            trailScaleLabel:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            trailScaleLabel:SetText("Trail-Größe: 1.0x")
            trailScaleLabel:SetTextColor(1, 1, 1, 1)
            yOffset = yOffset - 25
            
            local trailScaleSlider = CreateFrame("Slider", nil, settingsFrame, "OptionsSliderTemplate")
            trailScaleSlider:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            trailScaleSlider:SetMinMaxValues(0.5, 2.0)
            trailScaleSlider:SetValue(AiB_CursorConfig.trailScale or 1.0)
            trailScaleSlider:SetValueStep(0.1)
            trailScaleSlider:SetObeyStepOnDrag(true)
            trailScaleSlider:SetWidth(250)
            trailScaleSlider:SetScript("OnValueChanged", function(self, value)
                value = math.floor(value * 10 + 0.5) / 10
                AiB_CursorConfig.trailScale = value
                trailScaleLabel:SetText(string.format("Trail-Größe: %.1fx", value))
            end)
            yOffset = yOffset - 50
            
            return yOffset
        end
        
    end)
end)

-- Apply saved colors on startup
function AiB_ApplySavedCursorColors()
    if not AiB_CursorConfig or not AiB_CursorConfig.colors then return end
    local c = AiB_CursorConfig.colors
    
    if c.mainRing and AiB_Cursor_CursorFrame and AiB_Cursor_CursorFrame.MainRing then
        AiB_Cursor_CursorFrame.MainRing:SetVertexColor(c.mainRing.r, c.mainRing.g, c.mainRing.b, 1)
    end
    
    if c.gcdRing and AiB_Cursor and AiB_Cursor.GCDCooldownFrame then
        AiB_Cursor.GCDCooldownFrame:SetSwipeColor(c.gcdRing.r, c.gcdRing.g, c.gcdRing.b, 1)
    end
    
    if c.castRing and AiB_Cursor and AiB_Cursor.CastFrame then
        AiB_Cursor.CastFrame:SetSwipeColor(c.castRing.r, c.castRing.g, c.castRing.b, 1)
    end
end

