-------------------------
-- AiB Call to Arms Settings
-------------------------

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.CallToArmsSettings = {}
        
        function _G.AiB.CallToArmsSettings: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("Call to Arms Tracker")
            title:SetTextColor(1, 0.8, 0, 1)
            yOffset = yOffset - 40
            
            -- Description
            local desc = settingsFrame:CreateFontString(nil, "OVERLAY")
            desc:SetFont(FONT, 11)
            desc:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            desc:SetWidth(500)
            desc:SetJustifyH("LEFT")
            desc:SetText("|cFF888888Zeigt aktive Call to Arms Belohnungen (Sack voll Waren) für Dungeons und Raids an.|r")
            yOffset = yOffset - 35
            
            -- === 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("Call to Arms Tracker 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_CallToArmsConfig.enabled)
            enableCB:SetScript("OnClick", function(self)
                AiB_CallToArmsConfig.enabled = self:GetChecked()
                
                -- Immediate update
                if AiB_CallToArms then
                    AiB_CallToArms:UpdateVisibility()
                end
            end)
            yOffset = yOffset - 30
            
            -- *** NEU: Sound Dropdown ***
            -- Initialisiere soundId falls nicht vorhanden
            AiB_CallToArmsConfig.soundId = AiB_CallToArmsConfig.soundId or 1
            
            local soundLabel = settingsFrame:CreateFontString(nil, "OVERLAY")
            soundLabel:SetFont(FONT, 12)
            soundLabel:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            soundLabel:SetText("Benachrichtigungs-Sound:")
            soundLabel:SetTextColor(1, 1, 1, 1)
            
            -- Sound Liste
            local sounds = {
                {id = 0, name = "Aus", soundkit = nil},
                {id = 1, name = "Auktionshaus", soundkit = "AUCTION_WINDOW_OPEN"},
                {id = 2, name = "Ready Check", soundkit = "READY_CHECK"},
                {id = 3, name = "Whisper", soundkit = "TELL_MESSAGE"},
                {id = 4, name = "Raid Warning", soundkit = "RAID_WARNING"},
                {id = 5, name = "Achievement", soundkit = "ACHIEVEMENT_MENU_OPEN"},
            }
            
            local soundDD = CreateFrame("Frame", "AiBCTASoundDropdown", settingsFrame, "UIDropDownMenuTemplate")
            soundDD:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 180, yOffset + 5)
            UIDropDownMenu_SetWidth(soundDD, 180)
            UIDropDownMenu_SetText(soundDD, sounds[AiB_CallToArmsConfig.soundId + 1].name)
            
            UIDropDownMenu_Initialize(soundDD, function(self, level)
                for i, sound in ipairs(sounds) do
                    local info = UIDropDownMenu_CreateInfo()
                    info.text = sound.name
                    info.checked = (AiB_CallToArmsConfig.soundId == sound.id)
                    info.func = function()
                        AiB_CallToArmsConfig.soundId = sound.id
                        UIDropDownMenu_SetText(soundDD, sound.name)
                        CloseDropDownMenus()
                    end
                    UIDropDownMenu_AddButton(info, level)
                end
            end)
            
            -- Sound Test Button (mehr Abstand!)
            local soundTestBtn = CreateFrame("Button", nil, settingsFrame, "UIPanelButtonTemplate")
            soundTestBtn:SetSize(100, 24)
            soundTestBtn:SetPoint("LEFT", soundDD, "RIGHT", 10, -2)
            soundTestBtn:SetText("Sound testen")
            soundTestBtn:SetScript("OnClick", function()
                local selectedSound = sounds[AiB_CallToArmsConfig.soundId + 1]
                if selectedSound.soundkit then
                    PlaySound(SOUNDKIT[selectedSound.soundkit], "Master")
                else
                    print("|cFFFF0000[AiB]|r Sound ist deaktiviert")
                end
            end)
            yOffset = yOffset - 50
            
            -- Test Mode Checkbox
            local testText = settingsFrame:CreateFontString(nil, "OVERLAY")
            testText:SetFont(FONT, 12)
            testText:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            testText:SetText("Test-Modus (zeigt Beispiel-Daten)")
            testText:SetTextColor(1, 0.5, 0, 1)
            
            local testCB = CreateFrame("CheckButton", nil, settingsFrame, "UICheckButtonTemplate")
            testCB:SetPoint("LEFT", testText, "RIGHT", 5, 0)
            testCB:SetSize(24, 24)
            testCB:SetChecked(AiB_CallToArmsConfig.testMode)
            testCB:SetScript("OnClick", function(self)
                AiB_CallToArmsConfig.testMode = self:GetChecked()
                
                -- Immediate update without throttle
                if AiB_CallToArms then
                    -- Force unlock to allow immediate update
                    AiB_CallToArms.lock = false
                    
                    -- Run check and update display
                    local hasRewards = AiB_CallToArms:CheckStatus()
                    AiB_CallToArms:UpdateVisibility()
                end
            end)
            yOffset = yOffset - 40
            
            -- === ROLE FILTERS ===
            local roleHeader = settingsFrame:CreateFontString(nil, "OVERLAY")
            roleHeader:SetFont(FONT, 14, "OUTLINE")
            roleHeader:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            roleHeader:SetText("Rollen-Filter")
            roleHeader:SetTextColor(1, 0.8, 0, 1)
            yOffset = yOffset - 30
            
            local roleDesc = settingsFrame:CreateFontString(nil, "OVERLAY")
            roleDesc:SetFont(FONT, 11)
            roleDesc:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            roleDesc:SetWidth(500)
            roleDesc:SetJustifyH("LEFT")
            roleDesc:SetText("|cFF888888Wähle für welche Rollen du Call to Arms sehen möchtest:|r")
            yOffset = yOffset - 30
            
            -- Tank Checkbox
            local tankIcon = settingsFrame:CreateTexture(nil, "ARTWORK")
            tankIcon:SetSize(20, 20)
            tankIcon:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            tankIcon:SetTexture("Interface\\LFGFrame\\UI-LFG-ICON-PORTRAITROLES")
            tankIcon:SetTexCoord(0, 19/64, 22/64, 41/64)
            
            local tankText = settingsFrame:CreateFontString(nil, "OVERLAY")
            tankText:SetFont(FONT, 12)
            tankText:SetPoint("LEFT", tankIcon, "RIGHT", 5, 0)
            tankText:SetText("Tank")
            tankText:SetTextColor(1, 1, 1, 1)
            
            local tankCB = CreateFrame("CheckButton", nil, settingsFrame, "UICheckButtonTemplate")
            tankCB:SetPoint("LEFT", tankText, "RIGHT", 5, 0)
            tankCB:SetSize(24, 24)
            tankCB:SetChecked(AiB_CallToArmsConfig.roles.tank)
            tankCB:SetScript("OnClick", function(self)
                AiB_CallToArmsConfig.roles.tank = self:GetChecked()
                
                -- Immediate update
                if AiB_CallToArms then
                    AiB_CallToArms.lock = false
                    local hasRewards = AiB_CallToArms:CheckStatus()
                    AiB_CallToArms:UpdateVisibility()
                end
            end)
            yOffset = yOffset - 30
            
            -- Healer Checkbox
            local healerIcon = settingsFrame:CreateTexture(nil, "ARTWORK")
            healerIcon:SetSize(20, 20)
            healerIcon:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            healerIcon:SetTexture("Interface\\LFGFrame\\UI-LFG-ICON-PORTRAITROLES")
            healerIcon:SetTexCoord(20/64, 39/64, 1/64, 20/64)
            
            local healerText = settingsFrame:CreateFontString(nil, "OVERLAY")
            healerText:SetFont(FONT, 12)
            healerText:SetPoint("LEFT", healerIcon, "RIGHT", 5, 0)
            healerText:SetText("Heiler")
            healerText:SetTextColor(1, 1, 1, 1)
            
            local healerCB = CreateFrame("CheckButton", nil, settingsFrame, "UICheckButtonTemplate")
            healerCB:SetPoint("LEFT", healerText, "RIGHT", 5, 0)
            healerCB:SetSize(24, 24)
            healerCB:SetChecked(AiB_CallToArmsConfig.roles.healer)
            healerCB:SetScript("OnClick", function(self)
                AiB_CallToArmsConfig.roles.healer = self:GetChecked()
                
                -- Immediate update
                if AiB_CallToArms then
                    AiB_CallToArms.lock = false
                    local hasRewards = AiB_CallToArms:CheckStatus()
                    AiB_CallToArms:UpdateVisibility()
                end
            end)
            yOffset = yOffset - 30
            
            -- Damage Checkbox
            local damageIcon = settingsFrame:CreateTexture(nil, "ARTWORK")
            damageIcon:SetSize(20, 20)
            damageIcon:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            damageIcon:SetTexture("Interface\\LFGFrame\\UI-LFG-ICON-PORTRAITROLES")
            damageIcon:SetTexCoord(20/64, 39/64, 22/64, 41/64)
            
            local damageText = settingsFrame:CreateFontString(nil, "OVERLAY")
            damageText:SetFont(FONT, 12)
            damageText:SetPoint("LEFT", damageIcon, "RIGHT", 5, 0)
            damageText:SetText("Damage")
            damageText:SetTextColor(1, 1, 1, 1)
            
            local damageCB = CreateFrame("CheckButton", nil, settingsFrame, "UICheckButtonTemplate")
            damageCB:SetPoint("LEFT", damageText, "RIGHT", 5, 0)
            damageCB:SetSize(24, 24)
            damageCB:SetChecked(AiB_CallToArmsConfig.roles.damage)
            damageCB:SetScript("OnClick", function(self)
                AiB_CallToArmsConfig.roles.damage = self:GetChecked()
                
                -- Immediate update
                if AiB_CallToArms then
                    AiB_CallToArms.lock = false
                    local hasRewards = AiB_CallToArms:CheckStatus()
                    AiB_CallToArms:UpdateVisibility()
                end
            end)
            yOffset = yOffset - 50
            
            -- Reset Position Button
            local resetBtn = CreateFrame("Button", nil, settingsFrame, "UIPanelButtonTemplate")
            resetBtn:SetSize(180, 30)
            resetBtn:SetPoint("TOPLEFT", settingsFrame, "TOPLEFT", 20, yOffset)
            resetBtn:SetText("Position zurücksetzen")
            resetBtn:SetScript("OnClick", function()
                -- Reset position to center
                AiB_CallToArmsConfig.position = {point = "CENTER", x = 0, y = 0}
                
                -- Update frame position immediately
                if AiB_CallToArms and AiB_CallToArms.frame then
                    AiB_CallToArms.frame:ClearAllPoints()
                    AiB_CallToArms.frame:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
                end
                
                print("|cFF00FF00[AiB]|r Call to Arms Position zurückgesetzt")
            end)
            yOffset = yOffset - 50
            
            return yOffset
        end
        
    end)
end)
