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

NS.UI = NS.UI or {}
NS.UI.Widgets = NS.UI.Widgets or {}

local Widgets = NS.UI.Widgets
local Skin = NS.UI.Skin
local UIDropDownMenu_Initialize = rawget(_G, "UIDropDownMenu_Initialize")
local UIDropDownMenu_CreateInfo = rawget(_G, "UIDropDownMenu_CreateInfo")
local UIDropDownMenu_AddButton = rawget(_G, "UIDropDownMenu_AddButton")
local UIDropDownMenu_SetText = rawget(_G, "UIDropDownMenu_SetText")
local UIDropDownMenu_SetWidth = rawget(_G, "UIDropDownMenu_SetWidth")
local UIDropDownMenu_JustifyText = rawget(_G, "UIDropDownMenu_JustifyText")
local UIDropDownMenu_DisableDropDown = rawget(_G, "UIDropDownMenu_DisableDropDown")
local UIDropDownMenu_EnableDropDown = rawget(_G, "UIDropDownMenu_EnableDropDown")

function Widgets:ApplyPanelChrome(frame, opts)
    if Skin and Skin.ApplyWindowChrome then
        Skin:ApplyWindowChrome(frame, opts)
    end
end

function Widgets:CreateLabel(parent, text, template)
    local label = parent:CreateFontString(nil, "OVERLAY", template or "GameFontNormal")
    label:SetJustifyH("LEFT")
    label:SetText(tostring(text or ""))
    if Skin and Skin.StyleFont then
        local role = template == "GameFontNormalLarge" and "title" or "body"
        if template == "GameFontHighlightSmall" then
            role = "muted"
        end
        Skin:StyleFont(label, role)
    end
    return label
end

function Widgets:CreateSection(parent, title)
    if Skin and Skin.CreateSectionHeader then
        return Skin:CreateSectionHeader(parent, title)
    end
    local frame = CreateFrame("Frame", nil, parent)
    frame:SetHeight(20)
    local label = self:CreateLabel(frame, title, "GameFontNormal")
    label:SetPoint("LEFT", frame, "LEFT", 0, 0)
    frame.label = label
    return frame
end

function Widgets:CreateButton(parent, text, width, height)
    local button = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate")
    button:SetSize(width or 96, height or 26)
    local label = tostring(text or "")
    button:SetText(label)
    if Skin and Skin.SkinButton then
        local isPrimary = label == "\228\191\157\229\173\152" or label == "\230\150\176\229\162\158"
        local isDanger = label == "\229\136\160\233\153\164"
        Skin:SkinButton(button, isPrimary and "primary" or isDanger and "danger" or "secondary")
    end
    return button
end

function Widgets:CreateEditBox(parent, width, height, numeric)
    local box = CreateFrame("EditBox", nil, parent, "InputBoxTemplate,BackdropTemplate")
    box:SetAutoFocus(false)
    box:SetSize(width or 180, height or 20)
    box:SetTextInsets(8, 8, 0, 0)
    if numeric and box.SetNumeric then
        box:SetNumeric(true)
    end
    box:SetScript("OnEscapePressed", function(self)
        self:ClearFocus()
    end)
    box:SetScript("OnEnterPressed", function(self)
        self:ClearFocus()
    end)
    if Skin and Skin.SkinEditBox then
        Skin:SkinEditBox(box)
    end
    return box
end

function Widgets:CreateCheckButton(parent, text)
    local button = CreateFrame("CheckButton", nil, parent, "BackdropTemplate")
    button:SetSize(24, 24)

    local label = button:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    label:SetPoint("LEFT", button, "LEFT", 30, 0)
    button.Text = label
    button.text = label
    button.mcdLabel = label
    if label then
        label:SetText(tostring(text or ""))
    end
    if label and label.GetStringWidth then
        button:SetWidth(math.max(24, math.ceil(label:GetStringWidth()) + 34))
    end
    if Skin and Skin.SkinCheckButton then
        Skin:SkinCheckButton(button)
    end
    return button
end

function Widgets:CreateDropdown(parent, name, width)
    local dropdown = CreateFrame("Frame", name, parent, "UIDropDownMenuTemplate,BackdropTemplate")
    width = width or 180
    UIDropDownMenu_SetWidth(dropdown, width)
    -- UIDropDownMenu 默认模板会在视觉上额外撑宽。这里明确设置实际框体宽度，
    -- 让编辑页的统一网格列宽真正生效，避免右侧溢出或相邻控件重叠。
    dropdown:SetSize(width, 30)
    UIDropDownMenu_JustifyText(dropdown, "LEFT")
    dropdown.mcdItems = {}
    dropdown.mcdValue = nil
    dropdown.mcdText = ""

    UIDropDownMenu_Initialize(dropdown, function(self, level)
        if level ~= 1 then
            return
        end

        for _, item in ipairs(self.mcdItems or {}) do
            local info = UIDropDownMenu_CreateInfo()
            info.text = tostring(item.text or "")
            info.value = item.value
            info.func = function()
                self.mcdValue = item.value
                self.mcdText = tostring(item.text or "")
                UIDropDownMenu_SetText(self, self.mcdText)
                if type(self.mcdOnValueChanged) == "function" then
                    self.mcdOnValueChanged(self.mcdValue)
                end
            end
            info.checked = (self.mcdValue == item.value)
            UIDropDownMenu_AddButton(info, level)
        end
    end)

    if Skin and Skin.SkinDropDown then
        Skin:SkinDropDown(dropdown)
    end
    return dropdown
end

function Widgets:SetDropdownItems(dropdown, items)
    dropdown.mcdItems = items or {}
end

function Widgets:SetDropdownValue(dropdown, value, fallbackText)
    dropdown.mcdValue = value
    local text = fallbackText or ""
    for _, item in ipairs(dropdown.mcdItems or {}) do
        if item.value == value then
            text = tostring(item.text or "")
            break
        end
    end
    dropdown.mcdText = text
    UIDropDownMenu_SetText(dropdown, text)
end

function Widgets:GetDropdownValue(dropdown)
    return dropdown.mcdValue
end

function Widgets:SetDropdownEnabled(dropdown, enabled)
    if enabled then
        UIDropDownMenu_EnableDropDown(dropdown)
        if dropdown.SetBackdropBorderColor then
            dropdown:SetBackdropBorderColor(0.85, 0.70, 0.18, 0.55)
        end
    else
        UIDropDownMenu_DisableDropDown(dropdown)
        if dropdown.SetBackdropBorderColor then
            dropdown:SetBackdropBorderColor(0.45, 0.42, 0.34, 0.35)
        end
    end
end
