local NS = rawget(_G, "MCDVoiceCooldownNS") or {}
_G.MCDVoiceCooldownNS = NS

NS.UI = NS.UI or {}
NS.UI.EditorActions = NS.UI.EditorActions or {}

local Actions = NS.UI.EditorActions
local EditorDrafts = NS.UI.EditorDrafts or {}

local function GetState()
    if NS.AceOptions and type(NS.AceOptions.GetState) == "function" then
        return NS.AceOptions:GetState()
    end
    return {}
end

local function SaveEditorDraft(state, entryType)
    if EditorDrafts and type(EditorDrafts.Save) == "function" then
        return EditorDrafts:Save(state, entryType)
    end
end

local function Pull(owner)
    if owner and type(owner.PullFromWidgets) == "function" then
        owner:PullFromWidgets()
    end
end

local function Refresh(owner)
    if owner and type(owner.Refresh) == "function" then
        owner:Refresh()
    end
end

function Actions:Install(owner, frame)
    if not owner or not frame or not frame.widgets then
        return
    end

    local widgets = frame.widgets
    local tabCooldown = widgets.tabCooldown
    local tabCast = widgets.tabCast
    local tabBloodlust = widgets.tabBloodlust
    local classDrop = widgets.classDrop
    local specDrop = widgets.specDrop
    local actionSave = widgets.actionSave
    local actionTest = widgets.actionTest
    local checkTalent = widgets.checkTalent
    local delayEnabled = widgets.delayEnabled

    if tabCooldown then
        tabCooldown:SetScript("OnClick", function()
            if type(owner.SwitchEntryType) == "function" then
                owner:SwitchEntryType("cooldown")
            end
        end)
    end

    if tabCast then
        tabCast:SetScript("OnClick", function()
            if type(owner.SwitchEntryType) == "function" then
                owner:SwitchEntryType("cast")
            end
        end)
    end

    if tabBloodlust then
        tabBloodlust:SetScript("OnClick", function()
            local state = GetState()
            if owner.mode == "new" then
                Pull(owner)
                local currentType = tostring(state.entryType or "")
                if currentType == "cooldown" or currentType == "cast" then
                    SaveEditorDraft(state, currentType)
                end
            end
            if NS.AceOptions and type(NS.AceOptions.LoadBloodlustConfig) == "function" then
                NS.AceOptions:LoadBloodlustConfig()
            end
            Refresh(owner)
        end)
    end

    if classDrop then
        classDrop.mcdOnValueChanged = function(value)
            Pull(owner)
            local state = GetState()
            state.classID = tonumber(value) or 0
            -- Do not set specID to nil.  GetState() treats nil as "sync to the
            -- player's current spec", which makes switching to another class or
            -- "all classes" jump back unexpectedly.  When class changes, default
            -- to "all specs".
            state.specID = 0
            if NS.AceOptions and type(NS.AceOptions.EnsureValidScope) == "function" then
                NS.AceOptions:EnsureValidScope()
            end
            Refresh(owner)
        end
    end

    if specDrop then
        specDrop.mcdOnValueChanged = function(value)
            Pull(owner)
            local state = GetState()
            if (tonumber(state.classID) or 0) == 0 then
                state.specID = 0
            else
                state.specID = tonumber(value) or 0
            end
            Refresh(owner)
        end
    end

    if actionSave then
        actionSave:SetScript("OnClick", function()
            Pull(owner)
            local state = GetState()
            if tostring(state.entryType or "") == "bloodlust" then
                if NS.AceOptions and type(NS.AceOptions.SaveBloodlustConfig) == "function" and NS.AceOptions:SaveBloodlustConfig() then
                    if type(owner.Close) == "function" then
                        owner:Close()
                    end
                end
                return
            end

            if NS.AceOptions and type(NS.AceOptions.SaveEntry) == "function" and NS.AceOptions:SaveEntry() then
                if type(owner.Close) == "function" then
                    owner:Close()
                end
            end
        end)
    end

    if actionTest then
        actionTest:SetScript("OnClick", function()
            Pull(owner)
            local state = GetState()
            if tostring(state.entryType or "") == "bloodlust" then
                if NS.AceOptions and type(NS.AceOptions.TestBloodlustConfig) == "function" then
                    NS.AceOptions:TestBloodlustConfig()
                end
                return
            end
            if NS.AceOptions and type(NS.AceOptions.TestCurrent) == "function" then
                NS.AceOptions:TestCurrent()
            end
        end)
    end

    if checkTalent then
        checkTalent:SetScript("OnClick", function(button)
            Pull(owner)
            local state = GetState()
            state.checkTalent = button:GetChecked() == true
            if not state.checkTalent then
                state.talentId = 0
                state.talentName = ""
                state.talentCD = 0
            end
            Refresh(owner)
        end)
    end

    if delayEnabled then
        delayEnabled:SetScript("OnClick", function(button)
            Pull(owner)
            local state = GetState()
            state.delayEnabled = button:GetChecked() == true
            Refresh(owner)
        end)
    end
end
