-------------------------
-- AiB BreakTimer Settings
-------------------------

AiB = AiB or {}
AiB.BreakTimerSettings = {}

function AiB.BreakTimerSettings:AddToSettingsPanel(parent, startY)
    local FONT = "Fonts\\FRIZQT__.TTF"
    local yOffset = startY or -20
    
    -- === TIMER EINSTELLUNGEN ===
    local timerHeader = parent:CreateFontString(nil, "OVERLAY")
    timerHeader:SetFont(FONT, 14, "OUTLINE")
    timerHeader:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    timerHeader:SetText("Timer-Einstellungen")
    timerHeader:SetTextColor(1, 0.8, 0, 1)
    yOffset = yOffset - 30
    
    -- Breite Slider
    local widthLabel = parent:CreateFontString(nil, "OVERLAY")
    widthLabel:SetFont(FONT, 12)
    widthLabel:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    widthLabel:SetText("Breite:")
    widthLabel:SetTextColor(1, 1, 1, 1)
    
    local widthSlider = CreateFrame("Slider", nil, parent, "OptionsSliderTemplate")
    widthSlider:SetPoint("LEFT", widthLabel, "RIGHT", 10, 0)
    widthSlider:SetWidth(200)
    widthSlider:SetMinMaxValues(100, 600)
    widthSlider:SetValueStep(10)
    widthSlider:SetValue(AiB_BreakTimerConfig.width or 300)
    widthSlider:SetObeyStepOnDrag(true)
    
    local widthValue = parent:CreateFontString(nil, "OVERLAY")
    widthValue:SetFont(FONT, 12)
    widthValue:SetPoint("LEFT", widthSlider, "RIGHT", 10, 0)
    widthValue:SetText(tostring(widthSlider:GetValue()))
    widthValue:SetTextColor(1, 1, 1, 1)
    
    widthSlider:SetScript("OnValueChanged", function(self, value)
        AiB_BreakTimerConfig.width = value
        widthValue:SetText(tostring(math.floor(value)))
        if AiB_BreakTimer and AiB_BreakTimer.UpdateLayout then
            AiB_BreakTimer:UpdateLayout()
        end
    end)
    yOffset = yOffset - 40
    
    -- Höhe Slider
    local heightLabel = parent:CreateFontString(nil, "OVERLAY")
    heightLabel:SetFont(FONT, 12)
    heightLabel:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    heightLabel:SetText("Höhe:")
    heightLabel:SetTextColor(1, 1, 1, 1)
    
    local heightSlider = CreateFrame("Slider", nil, parent, "OptionsSliderTemplate")
    heightSlider:SetPoint("LEFT", heightLabel, "RIGHT", 10, 0)
    heightSlider:SetWidth(200)
    heightSlider:SetMinMaxValues(15, 60)
    heightSlider:SetValueStep(5)
    heightSlider:SetValue(AiB_BreakTimerConfig.height or 25)
    heightSlider:SetObeyStepOnDrag(true)
    
    local heightValue = parent:CreateFontString(nil, "OVERLAY")
    heightValue:SetFont(FONT, 12)
    heightValue:SetPoint("LEFT", heightSlider, "RIGHT", 10, 0)
    heightValue:SetText(tostring(heightSlider:GetValue()))
    heightValue:SetTextColor(1, 1, 1, 1)
    
    heightSlider:SetScript("OnValueChanged", function(self, value)
        AiB_BreakTimerConfig.height = value
        heightValue:SetText(tostring(math.floor(value)))
        if AiB_BreakTimer and AiB_BreakTimer.UpdateLayout then
            AiB_BreakTimer:UpdateLayout()
        end
    end)
    yOffset = yOffset - 40
    
    -- Bar Farbe
    local colorLabel = parent:CreateFontString(nil, "OVERLAY")
    colorLabel:SetFont(FONT, 12)
    colorLabel:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    colorLabel:SetText("Bar-Farbe:")
    colorLabel:SetTextColor(1, 1, 1, 1)
    
    -- Sichere Config-Prüfung
    if not AiB_BreakTimerConfig.barColor then
        AiB_BreakTimerConfig.barColor = {r = 1, g = 0.7, b = 0, a = 1}
    end
    
    -- Color swatch button (EXAKT wie Cursor!)
    local colorBtn = CreateFrame("Button", nil, parent)
    colorBtn:SetSize(24, 24)
    colorBtn:SetPoint("LEFT", colorLabel, "RIGHT", 8, 0)
    colorBtn:EnableMouse(true)
    
    -- Swatch texture (OVERLAY wie im Cursor!)
    local swatch = colorBtn:CreateTexture(nil, "OVERLAY")
    swatch:SetAllPoints()
    swatch:SetColorTexture(1, 1, 1)  -- Weiße Basis!
    
    -- Border (BACKGROUND layer)
    local border = colorBtn:CreateTexture(nil, "BACKGROUND")
    border:SetPoint("TOPLEFT", -1, 1)
    border:SetPoint("BOTTOMRIGHT", 1, -1)
    border:SetColorTexture(0.5, 0.5, 0.5)  -- Grauer Border
    
    -- Highlight
    local highlight = colorBtn:CreateTexture(nil, "HIGHLIGHT")
    highlight:SetAllPoints()
    highlight:SetColorTexture(1, 1, 1, 0.3)
    
    -- Funktion um Button-Farbe zu aktualisieren (EXAKT wie Cursor!)
    local function UpdateButtonColor()
        local c = AiB_BreakTimerConfig.barColor
        if c then
            swatch:SetVertexColor(c.r, c.g, c.b)  -- SetVertexColor statt SetColorTexture!
        else
            swatch:SetVertexColor(1, 1, 1)
        end
    end
    
    -- Initial color
    UpdateButtonColor()
    
    -- Click handler (MODERNE API wie in cursor_settings!)
    colorBtn:SetScript("OnClick", function()
        local r, g, b = AiB_BreakTimerConfig.barColor.r, AiB_BreakTimerConfig.barColor.g, AiB_BreakTimerConfig.barColor.b
        
        local info = {
            r = r,
            g = g,
            b = b,
            hasOpacity = false,
            
            swatchFunc = function()
                local newR, newG, newB = ColorPickerFrame:GetColorRGB()
                AiB_BreakTimerConfig.barColor.r = newR
                AiB_BreakTimerConfig.barColor.g = newG
                AiB_BreakTimerConfig.barColor.b = newB
                UpdateButtonColor() -- Button aktualisieren!
                if AiB_BreakTimer and AiB_BreakTimer.UpdateLayout then
                    AiB_BreakTimer:UpdateLayout()
                end
            end,
            
            cancelFunc = function()
                AiB_BreakTimerConfig.barColor.r = r
                AiB_BreakTimerConfig.barColor.g = g
                AiB_BreakTimerConfig.barColor.b = b
                UpdateButtonColor() -- Button zurücksetzen!
                if AiB_BreakTimer and AiB_BreakTimer.UpdateLayout then
                    AiB_BreakTimer:UpdateLayout()
                end
            end,
        }
        
        ColorPickerFrame:SetupColorPickerAndShow(info)
    end)
    
    -- Tooltip
    colorBtn:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:SetText("Klicken um Farbe zu ändern", 1, 1, 1)
        GameTooltip:Show()
    end)
    colorBtn:SetScript("OnLeave", function()
        GameTooltip:Hide()
    end)
    
    yOffset = yOffset - 40
    
    -- Entsperren/Verschieben
    local unlockCB = AiB.Utils:CreateCheckbox(parent, "Entsperren (zum Verschieben)",
        not (AiB_BreakTimerConfig.locked),
        function(checked)
            AiB_BreakTimerConfig.locked = not checked
            if AiB_BreakTimer and AiB_BreakTimer.UpdateLayout then
                AiB_BreakTimer:UpdateLayout()
            end
        end
    )
    unlockCB:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    yOffset = yOffset - 35
    
    -- Test Button
    local testBtn = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate")
    testBtn:SetSize(150, 25)
    testBtn:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    testBtn:SetText("Test (10 Sekunden)")
    testBtn:SetScript("OnClick", function()
        if AiB_BreakTimer then
            AiB_BreakTimer:StartTimer(10)
        end
    end)
    yOffset = yOffset - 40
    
    -- === SOUND EINSTELLUNGEN ===
    local soundHeader = parent:CreateFontString(nil, "OVERLAY")
    soundHeader:SetFont(FONT, 14, "OUTLINE")
    soundHeader:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    soundHeader:SetText("Sound-Einstellungen")
    soundHeader:SetTextColor(1, 0.8, 0, 1)
    yOffset = yOffset - 30
    
    -- Sound aktivieren
    local soundCB = AiB.Utils:CreateCheckbox(parent, "Sound aktivieren",
        AiB_BreakTimerConfig.soundEnabled,
        function(checked)
            AiB_BreakTimerConfig.soundEnabled = checked
        end
    )
    soundCB:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    yOffset = yOffset - 35
    
    -- Sound testen
    local soundTestBtn = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate")
    soundTestBtn:SetSize(150, 25)
    soundTestBtn:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    soundTestBtn:SetText("Sound testen")
    soundTestBtn:SetScript("OnClick", function()
        if AiB_BreakTimer and AiB_BreakTimer.PlaySound then
            AiB_BreakTimer:PlaySound()
        end
    end)
    yOffset = yOffset - 40
    
    -- === MEME EINSTELLUNGEN ===
    local memeHeader = parent:CreateFontString(nil, "OVERLAY")
    memeHeader:SetFont(FONT, 14, "OUTLINE")
    memeHeader:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    memeHeader:SetText("Meme-Einstellungen")
    memeHeader:SetTextColor(1, 0.8, 0, 1)
    yOffset = yOffset - 30
    
    -- Memes aktivieren
    local memeCB = AiB.Utils:CreateCheckbox(parent, "Memes aktivieren",
        AiB_BreakTimerConfig.memesEnabled,
        function(checked)
            AiB_BreakTimerConfig.memesEnabled = checked
        end
    )
    memeCB:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    yOffset = yOffset - 35
    
    -- Meme Wechsel-Intervall
    local intervalLabel = parent:CreateFontString(nil, "OVERLAY")
    intervalLabel:SetFont(FONT, 12)
    intervalLabel:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    intervalLabel:SetText("Wechsel-Intervall (Sekunden):")
    intervalLabel:SetTextColor(1, 1, 1, 1)
    
    local intervalSlider = CreateFrame("Slider", nil, parent, "OptionsSliderTemplate")
    intervalSlider:SetPoint("LEFT", intervalLabel, "RIGHT", 10, 0)
    intervalSlider:SetWidth(200)
    intervalSlider:SetMinMaxValues(5, 120)
    intervalSlider:SetValueStep(5)
    intervalSlider:SetValue(AiB_BreakTimerConfig.memeInterval or 30)
    intervalSlider:SetObeyStepOnDrag(true)
    
    local intervalValue = parent:CreateFontString(nil, "OVERLAY")
    intervalValue:SetFont(FONT, 12)
    intervalValue:SetPoint("LEFT", intervalSlider, "RIGHT", 10, 0)
    intervalValue:SetText(tostring(intervalSlider:GetValue()) .. "s")
    intervalValue:SetTextColor(1, 1, 1, 1)
    
    intervalSlider:SetScript("OnValueChanged", function(self, value)
        AiB_BreakTimerConfig.memeInterval = value
        intervalValue:SetText(tostring(math.floor(value)) .. "s")
    end)
    yOffset = yOffset - 40
    
    -- Meme Größe
    local memeWidthLabel = parent:CreateFontString(nil, "OVERLAY")
    memeWidthLabel:SetFont(FONT, 12)
    memeWidthLabel:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    memeWidthLabel:SetText("Meme-Breite:")
    memeWidthLabel:SetTextColor(1, 1, 1, 1)
    
    local memeWidthSlider = CreateFrame("Slider", nil, parent, "OptionsSliderTemplate")
    memeWidthSlider:SetPoint("LEFT", memeWidthLabel, "RIGHT", 10, 0)
    memeWidthSlider:SetWidth(200)
    memeWidthSlider:SetMinMaxValues(200, 1000)
    memeWidthSlider:SetValueStep(50)
    memeWidthSlider:SetValue(AiB_BreakTimerConfig.memeWidth or 600)
    memeWidthSlider:SetObeyStepOnDrag(true)
    
    local memeWidthValue = parent:CreateFontString(nil, "OVERLAY")
    memeWidthValue:SetFont(FONT, 12)
    memeWidthValue:SetPoint("LEFT", memeWidthSlider, "RIGHT", 10, 0)
    memeWidthValue:SetText(tostring(memeWidthSlider:GetValue()))
    memeWidthValue:SetTextColor(1, 1, 1, 1)
    
    memeWidthSlider:SetScript("OnValueChanged", function(self, value)
        AiB_BreakTimerConfig.memeWidth = value
        memeWidthValue:SetText(tostring(math.floor(value)))
        if AiB_BreakTimer and AiB_BreakTimer.UpdateLayout then
            AiB_BreakTimer:UpdateLayout()
        end
    end)
    yOffset = yOffset - 40
    
    -- Meme Höhe
    local memeHeightLabel = parent:CreateFontString(nil, "OVERLAY")
    memeHeightLabel:SetFont(FONT, 12)
    memeHeightLabel:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    memeHeightLabel:SetText("Meme-Höhe:")
    memeHeightLabel:SetTextColor(1, 1, 1, 1)
    
    local memeHeightSlider = CreateFrame("Slider", nil, parent, "OptionsSliderTemplate")
    memeHeightSlider:SetPoint("LEFT", memeHeightLabel, "RIGHT", 10, 0)
    memeHeightSlider:SetWidth(200)
    memeHeightSlider:SetMinMaxValues(200, 800)
    memeHeightSlider:SetValueStep(50)
    memeHeightSlider:SetValue(AiB_BreakTimerConfig.memeHeight or 400)
    memeHeightSlider:SetObeyStepOnDrag(true)
    
    local memeHeightValue = parent:CreateFontString(nil, "OVERLAY")
    memeHeightValue:SetFont(FONT, 12)
    memeHeightValue:SetPoint("LEFT", memeHeightSlider, "RIGHT", 10, 0)
    memeHeightValue:SetText(tostring(memeHeightSlider:GetValue()))
    memeHeightValue:SetTextColor(1, 1, 1, 1)
    
    memeHeightSlider:SetScript("OnValueChanged", function(self, value)
        AiB_BreakTimerConfig.memeHeight = value
        memeHeightValue:SetText(tostring(math.floor(value)))
        if AiB_BreakTimer and AiB_BreakTimer.UpdateLayout then
            AiB_BreakTimer:UpdateLayout()
        end
    end)
    yOffset = yOffset - 40
    
    -- Info
    local infoText = parent:CreateFontString(nil, "OVERLAY")
    infoText:SetFont(FONT, 11)
    infoText:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, yOffset)
    infoText:SetPoint("TOPRIGHT", parent, "TOPRIGHT", -20, yOffset)
    infoText:SetJustifyH("LEFT")
    infoText:SetWordWrap(true)
    infoText:SetText("|cFFFFAA00Verwendung:|r /break <minuten> - Startet einen Timer\n/break - Bricht den Timer ab")
    infoText:SetTextColor(0.7, 0.7, 0.7, 1)
    yOffset = yOffset - 60
    
    return yOffset
end
