-------------------------
-- AiB Crosshair Settings
-------------------------

-- Warte bis AiB geladen ist
local frame = CreateFrame("Frame")
frame:RegisterEvent("PLAYER_LOGIN")
frame:SetScript("OnEvent", function()
    C_Timer.After(2.5, function()
        
        if not _G.AiB then
            return
        end
        
        _G.AiB.CrosshairSettings = {}
        
        function _G.AiB.CrosshairSettings: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("Fadenkreuz")
            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("Fadenkreuz 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_CrosshairConfig.enabled)
            enableCB:SetScript("OnClick", function(self)
                AiB_CrosshairConfig.enabled = self:GetChecked()
                if AiB_Crosshair and AiB_Crosshair.UpdateVisibility then
                    AiB_Crosshair:UpdateVisibility()
                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_CrosshairConfig.showOnlyInCombat)
            combatCB:SetScript("OnClick", function(self)
                AiB_CrosshairConfig.showOnlyInCombat = self:GetChecked()
                if AiB_Crosshair and AiB_Crosshair.UpdateVisibility then
                    AiB_Crosshair:UpdateVisibility()
                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_CrosshairConfig.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, 3.0)
            scaleSlider:SetValue(AiB_CrosshairConfig.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_CrosshairConfig.scale = value
                scaleLabel:SetText("Größe: " .. string.format("%.1f", value))
                if AiB_Crosshair and AiB_Crosshair.ApplySettings then
                    AiB_Crosshair:ApplySettings()
                end
            end)
            yOffset = yOffset - 50
            
            -- Transparency Slider
            local alphaLabel = settingsFrame:CreateFontString(nil, "OVERLAY")
            alphaLabel:SetFont(FONT, 12)
            alphaLabel:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            alphaLabel:SetText(string.format("Transparenz: %.0f%%", (AiB_CrosshairConfig.alpha or 1.0) * 100))
            alphaLabel:SetTextColor(1, 1, 1, 1)
            yOffset = yOffset - 25
            
            local alphaSlider = CreateFrame("Slider", nil, settingsFrame, "OptionsSliderTemplate")
            alphaSlider:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            alphaSlider:SetMinMaxValues(0.1, 1.0)
            alphaSlider:SetValue(AiB_CrosshairConfig.alpha or 1.0)
            alphaSlider:SetValueStep(0.05)
            alphaSlider:SetObeyStepOnDrag(true)
            alphaSlider:SetWidth(250)
            alphaSlider:SetScript("OnValueChanged", function(self, value)
                AiB_CrosshairConfig.alpha = value
                alphaLabel:SetText(string.format("Transparenz: %.0f%%", value * 100))
                if AiB_Crosshair and AiB_Crosshair.ApplySettings then
                    AiB_Crosshair:ApplySettings()
                end
            end)
            yOffset = yOffset - 50
            
            -- === COLORS ===
            local colorHeader = settingsFrame:CreateFontString(nil, "OVERLAY")
            colorHeader:SetFont(FONT, 14, "OUTLINE")
            colorHeader:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            colorHeader:SetText("Farben")
            colorHeader:SetTextColor(1, 0.8, 0, 1)
            yOffset = yOffset - 35
            
            -- Helper function for color picker
            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")
                swatch:SetAllPoints()
                swatch:SetColorTexture(1, 1, 1)
                btn.swatch = swatch
                
                local border = btn:CreateTexture(nil, "BACKGROUND")
                border:SetPoint("TOPLEFT", -1, 1)
                border:SetPoint("BOTTOMRIGHT", 1, -1)
                border:SetColorTexture(0.5, 0.5, 0.5)
                
                local highlight = btn:CreateTexture(nil, "HIGHLIGHT")
                highlight:SetAllPoints()
                highlight:SetColorTexture(1, 1, 1, 0.3)
                
                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()
                            if applyFunc then 
                                applyFunc(newR, newG, newB)
                            end
                        end,
                        cancelFunc = function()
                            colorTable.r = r
                            colorTable.g = g
                            colorTable.b = b
                            UpdateButtonColor()
                            if applyFunc then 
                                applyFunc(r, g, b)
                            end
                        end,
                    }
                    
                    ColorPickerFrame:SetupColorPickerAndShow(info)
                end)
                
                UpdateButtonColor()
                return btn
            end
            
            -- Line Color
            MakeColorButton(20, yOffset, "Linien-Farbe:", AiB_CrosshairConfig.lineColor, function()
                if AiB_Crosshair and AiB_Crosshair.ApplySettings then
                    AiB_Crosshair:ApplySettings()
                end
            end)
            yOffset = yOffset - 30
            
            -- Border Color
            MakeColorButton(20, yOffset, "Rahmen-Farbe:", AiB_CrosshairConfig.borderColor, function()
                if AiB_Crosshair and AiB_Crosshair.ApplySettings then
                    AiB_Crosshair:ApplySettings()
                end
            end)
            yOffset = yOffset - 50
            
            return yOffset
        end
        
    end)
end)

