-------------------------
-- AiB Don't Release Settings
-------------------------

AiB = AiB or {}
AiB.DontReleaseSettings = {}

function AiB.DontReleaseSettings:AddToSettingsPanel(settingsFrame, startY)
    
    local FONT = "Fonts\\FRIZQT__.TTF"
    local yOffset = startY
    
    -- v2.1.0.0: Config kommt aus AceDB-Profil (Alias)
    -- Enable Checkbox
    local enableCB = AiB.Utils:CreateCheckbox(settingsFrame, "Don't Release aktivieren",
        AiB_DontReleaseConfig.enabled,
        function(checked)
            AiB_DontReleaseConfig.enabled = checked
        end
    )
    enableCB:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
    yOffset = yOffset - 50
    
    -- Modifier Key Dropdown
    local modKeyLabel = settingsFrame:CreateFontString(nil, "OVERLAY")
    modKeyLabel:SetFont(FONT, 12)
    modKeyLabel:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
    modKeyLabel:SetText("Modifier-Taste:")
    modKeyLabel:SetTextColor(1, 1, 1, 1)
    
    local modKeys = {"Shift", "Strg", "Alt"}
    local modKeyDD = CreateFrame("Frame", "AiBDRModKeyDropdown", settingsFrame, "UIDropDownMenuTemplate")
    modKeyDD:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 120, yOffset + 5)
    UIDropDownMenu_SetWidth(modKeyDD, 100)
    UIDropDownMenu_SetText(modKeyDD, modKeys[AiB_DontReleaseConfig.modKey])
    
    UIDropDownMenu_Initialize(modKeyDD, function(self, level)
        for i, key in ipairs(modKeys) do
            local info = UIDropDownMenu_CreateInfo()
            info.text = key
            info.checked = (AiB_DontReleaseConfig.modKey == i)
            info.func = function()
                AiB_DontReleaseConfig.modKey = i
                UIDropDownMenu_SetText(modKeyDD, key)
                CloseDropDownMenus()
            end
            UIDropDownMenu_AddButton(info, level)
        end
    end)
    yOffset = yOffset - 50
    
    -- Timeout Slider
    local timeoutLabel = settingsFrame:CreateFontString(nil, "OVERLAY")
    timeoutLabel:SetFont(FONT, 12)
    timeoutLabel:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
    timeoutLabel:SetText("Haltezeit (Sekunden):")
    timeoutLabel:SetTextColor(1, 1, 1, 1)
    
    local timeoutSlider = CreateFrame("Slider", "AiBDRTimeoutSlider", settingsFrame, "OptionsSliderTemplate")
    timeoutSlider:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset - 25)
    timeoutSlider:SetMinMaxValues(0, 10)
    timeoutSlider:SetValue(AiB_DontReleaseConfig.timeout)
    timeoutSlider:SetValueStep(0.1)
    timeoutSlider:SetObeyStepOnDrag(true)
    timeoutSlider:SetWidth(300)
    _G[timeoutSlider:GetName() .. "Low"]:SetText("0")
    _G[timeoutSlider:GetName() .. "High"]:SetText("10")
    _G[timeoutSlider:GetName() .. "Text"]:SetText(string.format("%.1f", AiB_DontReleaseConfig.timeout))
    timeoutSlider:SetScript("OnValueChanged", function(self, value)
        AiB_DontReleaseConfig.timeout = value
        _G[self:GetName() .. "Text"]:SetText(string.format("%.1f", value))
    end)
    yOffset = yOffset - 70
    
    -- Auto-Release Checkbox
    local autoReleaseCB = AiB.Utils:CreateCheckbox(settingsFrame, "Auto-Release wenn Button klickbar",
        AiB_DontReleaseConfig.autoRelease,
        function(checked)
            AiB_DontReleaseConfig.autoRelease = checked
        end
    )
    autoReleaseCB:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
    yOffset = yOffset - 50
    
    -- Difficulty Header
    local diffHeader = settingsFrame:CreateFontString(nil, "OVERLAY")
    diffHeader:SetFont(FONT, 12, "OUTLINE")
    diffHeader:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
    diffHeader:SetText("Aktiviert in folgenden Schwierigkeiten:")
    diffHeader:SetTextColor(1, 0.8, 0, 1)
    yOffset = yOffset - 30
    
    -- Initialisiere difficulties falls nicht vorhanden
    AiB_DontReleaseConfig.difficulties = AiB_DontReleaseConfig.difficulties or {
        [8] = true,   -- M+
        [14] = true,  -- Raid Normal
        [15] = true,  -- Raid Heroic
        [16] = true,  -- Raid Mythic
        [17] = true,  -- LFR
        [23] = true,  -- Mythic Dungeon
    }
    
    -- Difficulty Checkboxes
    local difficulties = {
        {id = 0, name = "Open World"},
        {id = 1, name = "Normal Dungeon"},
        {id = 2, name = "Heroic Dungeon"},
        {id = 8, name = "Mythic+ (M+)"},
        {id = 23, name = "Mythic Dungeon"},
        {id = 14, name = "Normal Raid"},
        {id = 15, name = "Heroic Raid"},
        {id = 16, name = "Mythic Raid"},
        {id = 17, name = "LFR"},
        {id = 208, name = "Delve"},
    }
    
    local xOffset = 20
    local colWidth = 200
    for i, diff in ipairs(difficulties) do
        local cb = AiB.Utils:CreateCheckbox(settingsFrame, diff.name,
            AiB_DontReleaseConfig.difficulties[diff.id] or false,
            function(checked)
                AiB_DontReleaseConfig.difficulties[diff.id] = checked
            end
        )
        
        -- 2 Spalten Layout
        local col = ((i - 1) % 2)
        local row = math.floor((i - 1) / 2)
        cb:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", xOffset + (col * colWidth), yOffset - (row * 30))
    end
    
    -- Berechne yOffset basierend auf Anzahl Rows
    local numRows = math.ceil(#difficulties / 2)
    yOffset = yOffset - (numRows * 30) - 20
    
    -- Info Text
    local infoText = settingsFrame:CreateFontString(nil, "OVERLAY")
    infoText:SetFont(FONT, 10)
    infoText:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
    infoText:SetPoint("TOPRIGHT", settingsFrame, "TOPRIGHT", -20, yOffset)
    infoText:SetJustifyH("LEFT")
    infoText:SetWordWrap(true)
    infoText:SetText("Verhindert versehentliches Release in den gewählten Schwierigkeiten. Du musst die Modifier-Taste halten um den Release-Button zu aktivieren.")
    infoText:SetTextColor(0.7, 0.7, 0.7, 1)
    
    return yOffset
end
