local PT = AhUI
local UI = {}
local L = PT.L
PT.Settings = UI

function UI:Create()
    if self.panel then
        return self.panel
    end

    local f = CreateFrame("Frame", "AhUISettingsMain", UIParent)
    f:SetSize(500, 440)

    local title = f:CreateFontString(nil, "OVERLAY", "GameFontHighlightLarge")
    title:SetPoint("TOPLEFT", 16, -16)
    title:SetText(L["Settings_Title"])

    AhUI_DB = AhUI_DB or {}
    AhUI_CharConfig = AhUI_CharConfig or {}

    local cb = CreateFrame("CheckButton", nil, f, "UICheckButtonTemplate")
    cb:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -12)
    cb.text:SetText(L["Settings_Concentration"])
    cb:SetChecked(PT.config.showConcentration)

    cb:SetScript("OnClick", function(self)
        PT.config.showConcentration = self:GetChecked()
        if PT.ConcentrationUI then
            if PT.config.showConcentration then
                PT.ConcentrationUI:Show()
            else
                PT.ConcentrationUI:Hide()
            end
        end
    end)

    if AhUI_CharConfig.skinningCheckEnabled == nil then
        AhUI_CharConfig.skinningCheckEnabled = false
    end

    local scb = CreateFrame("CheckButton", nil, f, "UICheckButtonTemplate")
    scb:SetPoint("LEFT", cb.text, "RIGHT", 20, 0)
    scb:SetChecked(AhUI_CharConfig.skinningCheckEnabled)

    scb.text = scb:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    scb.text:SetPoint("LEFT", scb, "RIGHT", 4, 0)
    scb.text:SetText(L["Settings_SkinningCheck"])

    scb:SetScript("OnClick", function(self)
        AhUI_CharConfig.skinningCheckEnabled = self:GetChecked()
        if PT.UI_SkinningCheck then
            PT.UI_SkinningCheck:Update()
        end
    end)

    local btn = CreateFrame("Button", nil, f, "UIPanelButtonTemplate")
    btn:SetSize(140, 22)
    btn:SetPoint("LEFT", scb, "RIGHT", 120, 0)
    btn:SetText(L["Settings_SkinningTargets"])

    btn:SetScript("OnClick", function()
        if PT.UI_SkinningTargets and PT.UI_SkinningTargets.Toggle then
            PT.UI_SkinningTargets:Toggle()
        end
    end)

    local sizeLabel = f:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    sizeLabel:SetPoint("TOPLEFT", scb, "BOTTOMLEFT", 4, -18)
    sizeLabel:SetText(L["Settings_SkinningIconSize"])

    local sizeInput = CreateFrame("EditBox", nil, f, "InputBoxTemplate")
    sizeInput:SetSize(30, 20)
    sizeInput:SetPoint("LEFT", sizeLabel, "RIGHT", 8, 0)
    sizeInput:SetAutoFocus(false)
    sizeInput:SetNumeric(true)
    sizeInput:SetMaxLetters(3)
    sizeInput:SetText(AhUI_CharConfig.skinningIconSize or 50)
	sizeInput:SetCursorPosition(0)
    sizeInput:SetEnabled(AhUI_CharConfig.skinningCheckEnabled)

    sizeInput:SetScript("OnEnterPressed", function(self)
        local v = tonumber(self:GetText())
        if not v then
            self:SetText(AhUI_CharConfig.skinningIconSize or 50)
            self:ClearFocus()
            return
        end

        v = math.max(20, math.min(300, math.floor(v)))
        AhUI_CharConfig.skinningIconSize = v
        self:SetText(v)
		self:SetCursorPosition(0)

        if PT.UI_SkinningCheck then
            PT.UI_SkinningCheck:Update()
        end

        self:ClearFocus()
    end)

    local cb2 = CreateFrame("CheckButton", nil, f, "UICheckButtonTemplate")
    cb2:SetPoint("TOPLEFT", cb, "BOTTOMLEFT", 0, -10)
    cb2.text:SetText(L["Settings_ProfWindow"])
    cb2:SetChecked(AhUI_CharConfig.showProfessionsWindow)

    cb2:SetScript("OnClick", function(self)
        AhUI_CharConfig.showProfessionsWindow = self:GetChecked()
        if PT.UI and PT.UI.frame then
            if AhUI_CharConfig.showProfessionsWindow then
                PT.UI.frame:Show()
            else
                PT.UI.frame:Hide()
            end
        end
    end)

    local cb3 = CreateFrame("CheckButton", nil, f, "UICheckButtonTemplate")
    cb3:SetPoint("TOPLEFT", cb2, "BOTTOMLEFT", 0, -10)
    cb3.text:SetText(L["Settings_LockWindows"])
    cb3:SetChecked(PT.config.lockWindows)

    cb3:SetScript("OnClick", function(self)
        PT.config.lockWindows = self:GetChecked()

        local windows = {
            PT.UI and PT.UI.frame,
            PT.ConcentrationUI and PT.ConcentrationUI.frame,
        }

        for _, w in ipairs(windows) do
            if w then
                w:SetMovable(not PT.config.lockWindows)
                w:EnableMouse(not PT.config.lockWindows)
            end
        end
    end)

    AhUI_CharConfig.confirmOK = (AhUI_CharConfig.confirmOK == true)
    AhUI_CharConfig.confirmReload = (AhUI_CharConfig.confirmReload == true)
    AhUI_CharConfig.confirmButtonStyle = AhUI_CharConfig.confirmButtonStyle or "default"

    local cbOK = CreateFrame("CheckButton", nil, f, "UICheckButtonTemplate")
    cbOK:SetPoint("TOPLEFT", cb3, "BOTTOMLEFT", 0, -10)
    cbOK.text:SetText(L["Settings_ConfirmOK"])
    cbOK:SetChecked(AhUI_CharConfig.confirmOK)

    cbOK:SetScript("OnClick", function(self)
        AhUI_CharConfig.confirmOK = (self:GetChecked() == true)
        if PT.ConfirmButtons and PT.ConfirmButtons.SetOKEnabled then
            PT.ConfirmButtons:SetOKEnabled(AhUI_CharConfig.confirmOK)
        end
    end)

    local cbR = CreateFrame("CheckButton", nil, f, "UICheckButtonTemplate")
    cbR:SetPoint("TOPLEFT", cbOK, "BOTTOMLEFT", 0, -10)
    cbR.text:SetText(L["Settings_ConfirmReload"])
    cbR:SetChecked(AhUI_CharConfig.confirmReload)

    cbR:SetScript("OnClick", function(self)
        AhUI_CharConfig.confirmReload = (self:GetChecked() == true)
        if PT.ConfirmButtons and PT.ConfirmButtons.SetReloadEnabled then
            PT.ConfirmButtons:SetReloadEnabled(AhUI_CharConfig.confirmReload)
        end
    end)
if PT.ConfirmButtons then
    PT.ConfirmButtons:SetOKEnabled(AhUI_CharConfig.confirmOK == true)
    PT.ConfirmButtons:SetReloadEnabled(AhUI_CharConfig.confirmReload == true)
end

    local styleLabel = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    styleLabel:SetPoint("TOPLEFT", cbR, "BOTTOMLEFT", 0, -8)
    styleLabel:SetText(L["Settings_ConfirmStyle"])

    local styles = {}
    if PT.ConfirmButtons and PT.ConfirmButtons.STYLES then
        for key, info in pairs(PT.ConfirmButtons.STYLES) do
            table.insert(styles, { key = key, name = info.name or key })
        end
    end
    table.sort(styles, function(a, b) return a.name < b.name end)

    local dd = CreateFrame("Frame", "AhUIConfirmStyleDropdown", f, "UIDropDownMenuTemplate")
    dd:SetPoint("TOPLEFT", styleLabel, "BOTTOMLEFT", -16, -4)
    UIDropDownMenu_SetWidth(dd, 200)

    UIDropDownMenu_Initialize(dd, function(_, level)
        for _, s in ipairs(styles) do
            local info = UIDropDownMenu_CreateInfo()
            info.text = s.name
            info.checked = (AhUI_CharConfig.confirmButtonStyle == s.key)
            info.func = function()
                AhUI_CharConfig.confirmButtonStyle = s.key
                UIDropDownMenu_SetText(dd, s.name)
                if PT.ConfirmButtons and PT.ConfirmButtons.ApplyStyle then
                    PT.ConfirmButtons:ApplyStyle()
                end
            end
            UIDropDownMenu_AddButton(info, level)
        end
    end)

    do
        local cur = AhUI_CharConfig.confirmButtonStyle
        local style = PT.ConfirmButtons and PT.ConfirmButtons.STYLES and PT.ConfirmButtons.STYLES[cur]
        UIDropDownMenu_SetText(dd, style and style and style.name or L["Default"])
    end

    local reset = CreateFrame("Button", nil, f, "UIPanelButtonTemplate")
    reset:SetSize(260, 22)
    reset:SetPoint("TOPLEFT", styleLabel, "BOTTOMLEFT", 0, -42)
    reset:SetText(L["Settings_ResetOK"])

    reset:SetScript("OnClick", function()
        if PT.ConfirmButtons and PT.ConfirmButtons.ResetOKExclusions then
            PT.ConfirmButtons:ResetOKExclusions()
        end
    end)

    self.panel = f
    return f
end

function UI:Register()
    if self.category then return end

    local ev = CreateFrame("Frame")
    ev:RegisterEvent("PLAYER_LOGIN")
    ev:SetScript("OnEvent", function()
        if self.category then return end

        local panel = self:Create()
        local category = Settings.RegisterCanvasLayoutCategory(panel, "AhUI")
        Settings.RegisterAddOnCategory(category)

        self.category = category
        if self.RegisterInfoPanelSubcategory then
            self:RegisterInfoPanelSubcategory()
        end
    end)
end

SLASH_AHUI1 = "/ahui"
SlashCmdList["AHUI"] = function()
    if PT.Settings and PT.Settings.category then
        Settings.OpenToCategory(PT.Settings.category:GetID())
    end
end
