local addonName, L = ...

local AceGUI = LibStub("AceGUI-3.0")
local ui = L.tabSettingsUI

function ui.RefreshSimpleSoundWidgets(widgets, profileKey)
    local moduleEnabled = ui.IsProfileModuleEnabled(profileKey)
    local usingTTS = L:IsUsingTTS(profileKey)
    local selectedSound = ui.EnsureSelectedSound(profileKey)
    local selectedVoice = ui.EnsureSelectedVoice(profileKey)

    widgets.modeDropdown:SetValue(usingTTS and "tts" or "sound")
    widgets.modeDropdown:SetDisabled(not moduleEnabled)
    widgets.soundDropdown:SetValue(L:MakeUniqueID(ui.GetField(profileKey, "selectedSoundSource"), ui.GetField(profileKey, "selectedSoundKey")))
    widgets.soundDropdown:SetDisabled((not moduleEnabled) or usingTTS)
    widgets.soundChannelDropdown:SetValue(ui.GetField(profileKey, "soundChannel") or L.DEFAULT_SOUND_CHANNEL)
    widgets.soundChannelDropdown:SetDisabled((not moduleEnabled) or usingTTS)
    widgets.testSoundButton:SetDisabled((not moduleEnabled) or usingTTS)
    widgets.voiceDropdown:SetValue(tostring(ui.GetField(profileKey, "ttsVoiceID") or ""))
    widgets.voiceDropdown:SetDisabled((not moduleEnabled) or (not usingTTS))
    widgets.ttsTextBox:SetText(tostring(ui.GetField(profileKey, "ttsText") or ""))
    widgets.ttsTextBox:SetDisabled((not moduleEnabled) or (not usingTTS))
    widgets.testTTSButton:SetDisabled((not moduleEnabled) or (not usingTTS))
    if widgets.testCurrentButton then
        widgets.testCurrentButton:SetDisabled(not moduleEnabled)
    end
    if widgets.reloadVoicesButton then
        widgets.reloadVoicesButton:SetDisabled(not moduleEnabled)
    end
    widgets.statusLabel:SetText("Output: " .. (usingTTS and "Text-to-Speech" or "Sound") .. " | Selected sound: " .. (selectedSound and selectedSound.name or "None") .. " | Selected voice: " .. (selectedVoice and selectedVoice.name or "None") .. " | TTS status: " .. tostring(L.ttsPlaybackState))
end

function ui.BuildOutputGroup(root, profileKey, title, intro, includeCurrentButton, previewContext)
    local widgets = {}
    ui.AddHeading(root, title, intro)

    local outputGroup = AceGUI:Create("InlineGroup")
    outputGroup:SetTitle("Alert Output")
    outputGroup:SetFullWidth(true)
    outputGroup:SetLayout("List")
    root:AddChild(outputGroup)

    local modeDropdown = AceGUI:Create("Dropdown")
    modeDropdown:SetLabel("Output type")
    modeDropdown:SetList({ sound = "Sound", tts = "Text-to-Speech" })
    modeDropdown:SetFullWidth(true)
    modeDropdown:SetCallback("OnValueChanged", function(_, _, value)
        ui.SetField(profileKey, "useTTS", value == "tts")
        L:RefreshTabbedSettingsUIState()
    end)
    outputGroup:AddChild(modeDropdown)

    local soundDropdown = AceGUI:Create("Dropdown")
    soundDropdown:SetLabel("Sound")
    local soundList, soundOrder = L:BuildSoundListForAce()
    soundDropdown:SetList(soundList, soundOrder)
    soundDropdown:SetFullWidth(true)
    soundDropdown:SetCallback("OnValueChanged", function(_, _, value)
        local source, key = L:SplitUniqueID(value)
        if source and key and L.soundIndex[value] then
            -- Persist source and key separately so the DB remains stable even if two
            -- providers expose the same visible label.
            ui.SetField(profileKey, "selectedSoundSource", source)
            ui.SetField(profileKey, "selectedSoundKey", key)
            L:PlaySelectedSound(profileKey, { isPreview = true })
            L:RefreshTabbedSettingsUIState()
        end
    end)
    outputGroup:AddChild(soundDropdown)

    local soundChannelDropdown = AceGUI:Create("Dropdown")
    soundChannelDropdown:SetLabel("Sound channel")
    soundChannelDropdown:SetList(L:BuildSoundChannelListForAce())
    soundChannelDropdown:SetFullWidth(true)
    soundChannelDropdown:SetCallback("OnValueChanged", function(_, _, value)
        ui.SetField(profileKey, "soundChannel", value or L.DEFAULT_SOUND_CHANNEL)
        L:RefreshTabbedSettingsUIState()
    end)
    outputGroup:AddChild(soundChannelDropdown)

    local buttonRow = AceGUI:Create("SimpleGroup")
    buttonRow:SetFullWidth(true)
    buttonRow:SetLayout("Flow")
    outputGroup:AddChild(buttonRow)

    local testSoundButton = AceGUI:Create("Button")
    testSoundButton:SetText("Play sound")
    testSoundButton:SetWidth(140)
    testSoundButton:SetCallback("OnClick", function()
        L:PlaySelectedSound(profileKey, { isPreview = true })
    end)
    buttonRow:AddChild(testSoundButton)

    local voiceDropdown = AceGUI:Create("Dropdown")
    voiceDropdown:SetLabel("TTS voice")
    voiceDropdown:SetList(L:BuildVoiceListForAce())
    voiceDropdown:SetFullWidth(true)
    voiceDropdown:SetCallback("OnValueChanged", function(_, _, value)
        local id = tonumber(value)
        if id and L.voiceIndex[id] then
            ui.SetField(profileKey, "ttsVoiceID", id)
            L:RefreshTabbedSettingsUIState()
        end
    end)
    outputGroup:AddChild(voiceDropdown)

    local ttsTextBox = AceGUI:Create("EditBox")
    ttsTextBox:SetLabel("Spoken text")
    ttsTextBox:SetFullWidth(true)
    ttsTextBox:DisableButton(true)
    ttsTextBox:SetCallback("OnTextChanged", function(_, _, value)
        ui.SetField(profileKey, "ttsText", tostring(value or ""))
    end)
    ttsTextBox:SetCallback("OnEnterPressed", function(_, _, value)
        ui.SetField(profileKey, "ttsText", L:Trim(value))
        L:RefreshTabbedSettingsUIState()
    end)
    outputGroup:AddChild(ttsTextBox)

    local testTTSButton = AceGUI:Create("Button")
    testTTSButton:SetText("Play TTS")
    testTTSButton:SetWidth(140)
    testTTSButton:SetCallback("OnClick", function()
        L:PlaySelectedTTS(profileKey, previewContext, { isPreview = true })
    end)
    buttonRow:AddChild(testTTSButton)

    if includeCurrentButton then
        local testCurrentButton = AceGUI:Create("Button")
        testCurrentButton:SetText("Test current alert")
        testCurrentButton:SetWidth(160)
        testCurrentButton:SetCallback("OnClick", function()
            L:PlayCurrentAlert(profileKey, previewContext, { isPreview = true })
        end)
        buttonRow:AddChild(testCurrentButton)
        widgets.testCurrentButton = testCurrentButton

        local reloadVoicesButton = AceGUI:Create("Button")
        reloadVoicesButton:SetText("Refresh voices")
        reloadVoicesButton:SetWidth(140)
        reloadVoicesButton:SetCallback("OnClick", function()
            -- Voice availability can change while the client is running, so keep a
            -- manual refresh nearby instead of requiring a UI reload.
            L:RebuildVoiceList()
            ui.EnsureSelectedVoice(profileKey)
            L:RefreshTabbedSettingsUIState()
        end)
        buttonRow:AddChild(reloadVoicesButton)
        widgets.reloadVoicesButton = reloadVoicesButton
    end

    local statusGroup = AceGUI:Create("InlineGroup")
    statusGroup:SetTitle("Current Selection")
    statusGroup:SetFullWidth(true)
    statusGroup:SetLayout("List")
    root:AddChild(statusGroup)

    local statusLabel = AceGUI:Create("Label")
    statusLabel:SetFullWidth(true)
    statusGroup:AddChild(statusLabel)

    widgets.modeDropdown = modeDropdown
    widgets.soundDropdown = soundDropdown
    widgets.soundChannelDropdown = soundChannelDropdown
    widgets.testSoundButton = testSoundButton
    widgets.voiceDropdown = voiceDropdown
    widgets.ttsTextBox = ttsTextBox
    widgets.testTTSButton = testTTSButton
    widgets.statusLabel = statusLabel
    return widgets
end
