local _, ns = ...
local MythicTools = ns.MythicTools
local UI = ns.UI

local ApplyThinScrollBar = UI.ApplyThinScrollBar
local COLORS = UI.COLORS
local CONTROL_COLORS = UI.CONTROL_COLORS
local ClipChildren = UI.ClipChildren
local CreateAceDropdownWidget = UI.CreateAceDropdownWidget
local CreateAceFlowRow = UI.CreateAceFlowRow
local CreateAceSpacer = UI.CreateAceSpacer
local CreateActionButton = UI.CreateActionButton
local CreateCycleControl = UI.CreateCycleControl
local CreateFont = UI.CreateFont
local CreateLabeledEditBox = UI.CreateLabeledEditBox
local CreateLabeledSlider = UI.CreateLabeledSlider
local CreateSettingToggle = UI.CreateSettingToggle
local GetPopupModeLabel = UI.GetPopupModeLabel
local GetPopupModeNext = UI.GetPopupModeNext
local GetPopupTriggerLabel = UI.GetPopupTriggerLabel
local GetPopupTriggerNext = UI.GetPopupTriggerNext
local SetTextColor = UI.SetTextColor
local StyleAceDropdownWidget = UI.StyleAceDropdownWidget
local floor = UI.floor

function MythicTools:BuildSettingsPage(parent)
    local page = CreateFrame("Frame", nil, parent)
    page:SetAllPoints(parent)
    ClipChildren(page)

    local tabBar = CreateFrame("Frame", nil, page)
    tabBar:SetPoint("TOPLEFT", 12, -12)
    tabBar:SetPoint("TOPRIGHT", -12, -12)
    tabBar:SetHeight(32)

    local settingsContent = CreateFrame("Frame", nil, page)
    settingsContent:SetPoint("TOPLEFT", 0, -52)
    settingsContent:SetPoint("BOTTOMRIGHT", 0, 0)
    ClipChildren(settingsContent)

    self.ui.settingsSubtabButtons = {}
    self.ui.settingsSubtabPages = {}
    local settingsTabs = {
        {key = "General", label = self:L("SETTINGS_TAB_GENERAL")},
        {key = "Popup", label = self:L("SETTINGS_TAB_POPUP")},
        {key = "Tooltip", label = self:L("SETTINGS_TAB_TOOLTIP")},
        {key = "Notifications", label = self:L("SETTINGS_TAB_NOTIFICATIONS")},
    }
    for index, tabInfo in ipairs(settingsTabs) do
        local button = CreateActionButton(tabBar, 112, 28, tabInfo.label)
        button:SetPoint("LEFT", (index - 1) * 122, 0)
        button._restyle = function(self)
            if not self:IsEnabled() then
                MythicTools:ApplySurface(self, COLORS.rowBG, CONTROL_COLORS.controlBorder, COLORS.accent)
                SetTextColor(self.Text, COLORS.subdued)
                return
            end

            if self._selected then
                MythicTools:ApplySurface(self, CONTROL_COLORS.accentFillActive, COLORS.accent, COLORS.accent)
                SetTextColor(self.Text, COLORS.bright)
            else
                MythicTools:ApplySurface(self, CONTROL_COLORS.controlBG, CONTROL_COLORS.controlBorder, COLORS.accent)
                SetTextColor(self.Text, CONTROL_COLORS.controlText)
            end
        end
        if button._restyle then
            button:_restyle()
        end
        button:SetScript("OnClick", function()
            self.db.ui.settingsTab = tabInfo.key
            for key, subPage in pairs(self.ui.settingsSubtabPages or {}) do
                subPage:SetShown(key == self.db.ui.settingsTab)
            end
            for key, subButton in pairs(self.ui.settingsSubtabButtons or {}) do
                subButton._selected = key == self.db.ui.settingsTab
                if subButton._restyle then
                    subButton:_restyle()
                end
            end
        end)
        self.ui.settingsSubtabButtons[tabInfo.key] = button

        local subPage = CreateFrame("Frame", nil, settingsContent)
        subPage:SetAllPoints(settingsContent)
        ClipChildren(subPage)
        self.ui.settingsSubtabPages[tabInfo.key] = subPage
    end

    if self.db.ui.settingsTab == "Alerts" then
        self.db.ui.settingsTab = "Notifications"
    elseif self.db.ui.settingsTab == "Data" then
        self.db.ui.settingsTab = "General"
    end
    self.db.ui.settingsTab = self.db.ui.settingsTab or "General"

    local generalCard = CreateFrame("Frame", nil, self.ui.settingsSubtabPages.General)
    generalCard:SetPoint("TOPLEFT", 12, -12)
    generalCard:SetSize(350, 364)
    self:ApplySurface(generalCard, COLORS.sectionBG, COLORS.surface, COLORS.accentSoft)

    local generalTitle = CreateFont(generalCard, 16, COLORS.text)
    generalTitle:SetPoint("TOPLEFT", 16, -16)
    generalTitle:SetText(self:L("SETTINGS_GENERAL"))

    self.ui.settingsLocale = CreateAceDropdownWidget(self:L("SETTINGS_LANGUAGE"), 230, self:GetLocaleOptions(), self:GetCurrentLocalePreference(), function(value)
        local previousLocale = self:GetCurrentLocalePreference()
        self:SetLocalePreference(value)
        self:RefreshSettingsView()
        if value ~= previousLocale then
            self:ShowCustomModal({
                title = self:L("SETTINGS_LANGUAGE_RELOAD_TITLE"),
                text = self:L("SETTINGS_LANGUAGE_RELOAD_BODY"),
                inputMode = "none",
                confirmText = self:L("SETTINGS_LANGUAGE_RELOAD_NOW"),
                cancelText = self:L("SETTINGS_LANGUAGE_RELOAD_LATER"),
                onAccept = function()
                    if ReloadUI then
                        ReloadUI()
                    end
                end,
            })
        end
    end)
    self.ui.settingsLocale.frame:SetParent(generalCard)
    self.ui.settingsLocale.frame:SetPoint("TOPLEFT", 16, -44)

    self.ui.settingsMinimap = CreateSettingToggle(generalCard, self:L("SETTINGS_MINIMAP"), self:L("SETTINGS_MINIMAP_DESC"), function(checked)
        self:SetMinimapButtonEnabled(checked)
        self:RefreshSettingsView()
    end)
    self.ui.settingsMinimap:SetPoint("TOPLEFT", 16, -108)
    self.ui.settingsMinimap:SetPoint("TOPRIGHT", -16, -108)

    self.ui.settingsLootHistory = CreateSettingToggle(generalCard, self:L("SETTINGS_LOOT_HISTORY"), self:L("SETTINGS_LOOT_HISTORY_DESC"), function(checked)
        self.db.settings.showLootHistory = checked and true or false
        self:RefreshAllViews()
    end)
    self.ui.settingsLootHistory:SetPoint("TOPLEFT", 16, -156)
    self.ui.settingsLootHistory:SetPoint("TOPRIGHT", -16, -156)

    self.ui.settingsTooltipBadge = CreateSettingToggle(generalCard, self:L("UI_SHOW_LFG_BADGE"), self:L("UI_SHOW_LFG_BADGE_DESC"), function(checked)
        self.db.settings.showLFGApplicantHistoryBadge = checked and true or false
        if self.RefreshVisibleLFGApplicantHistoryBadges then
            self:RefreshVisibleLFGApplicantHistoryBadges()
        end
        self:RefreshSettingsView()
    end)
    self.ui.settingsTooltipBadge:SetPoint("TOPLEFT", 16, -204)
    self.ui.settingsTooltipBadge:SetPoint("TOPRIGHT", -16, -204)

    self.ui.settingsTooltipUnitMenu = CreateSettingToggle(generalCard, self:L("UI_SHOW_UNIT_MENU"), self:L("UI_SHOW_UNIT_MENU_DESC"), function(checked)
        self.db.settings.showUnitContextMenuEntry = checked and true or false
        self:RefreshSettingsView()
    end)
    self.ui.settingsTooltipUnitMenu:SetPoint("TOPLEFT", 16, -252)
    self.ui.settingsTooltipUnitMenu:SetPoint("TOPRIGHT", -16, -252)

    self.ui.settingsAutoSave = CreateSettingToggle(generalCard, self:L("SETTINGS_AUTO_SAVE"), self:L("SETTINGS_AUTO_SAVE_DESC"), function(checked)
        self.db.settings.autoSaveAfterRun = checked and true or false
        self:RefreshSettingsView()
    end)
    self.ui.settingsAutoSave:SetPoint("TOPLEFT", 16, -300)
    self.ui.settingsAutoSave:SetPoint("TOPRIGHT", -16, -300)

    local popupCard = CreateFrame("Frame", nil, self.ui.settingsSubtabPages.Popup)
    popupCard:SetPoint("TOPLEFT", 12, -12)
    popupCard:SetPoint("TOPRIGHT", self.ui.settingsSubtabPages.Popup, "TOPRIGHT", -12, -12)
    popupCard:SetHeight(416)
    self:ApplySurface(popupCard, COLORS.sectionBG, COLORS.surface, COLORS.accent)

    local popupTitle = CreateFont(popupCard, 16, COLORS.text)
    popupTitle:SetPoint("TOPLEFT", 16, -16)
    popupTitle:SetText(self:L("SETTINGS_COMPLETION_POPUP"))

    self.ui.settingsPopupEnabled = CreateSettingToggle(popupCard, self:L("SETTINGS_POPUP_ENABLED"), self:L("SETTINGS_POPUP_ENABLED_DESC"), function(checked)
        local mode = self:GetCompletionPopupMode()
        if checked then
            self.db.settings.completionPopupMode = mode == "off" and "full" or mode
        else
            self.db.settings.completionPopupMode = "off"
        end
        self.db.settings.showCompletionPopup = self.db.settings.completionPopupMode ~= "off"
        if not checked then
            self.runtime.pendingCompletionPopup = nil
            self:HideCompletionToast()
            self:HideCompletionPopup()
        end
        self:RefreshSettingsView()
    end)
    self.ui.settingsPopupEnabled:SetPoint("TOPLEFT", 16, -44)
    self.ui.settingsPopupEnabled:SetPoint("TOPRIGHT", -16, -44)

    self.ui.settingsPopupMode = CreateCycleControl(popupCard, self:L("SETTINGS_POPUP_MODE"), 260, GetPopupModeLabel, function()
        self.db.settings.completionPopupMode = GetPopupModeNext(self:GetCompletionPopupMode())
        self.db.settings.showCompletionPopup = self.db.settings.completionPopupMode ~= "off"
        if self.db.settings.completionPopupMode == "off" then
            self.runtime.pendingCompletionPopup = nil
            self:HideCompletionToast()
            self:HideCompletionPopup()
        end
        self.ui.settingsPopupMode:SetValue(self.db.settings.completionPopupMode)
        self:RefreshSettingsView()
    end)
    self.ui.settingsPopupMode:SetPoint("TOPLEFT", 16, -104)

    self.ui.settingsPopupTrigger = CreateCycleControl(popupCard, self:L("SETTINGS_OPEN_TIMING"), 190, GetPopupTriggerLabel, function()
        self.db.settings.completionPopupTrigger = GetPopupTriggerNext(self.db.settings.completionPopupTrigger or "LOOT_OPENED")
        self.ui.settingsPopupTrigger:SetValue(self.db.settings.completionPopupTrigger)
        self:RefreshSettingsView()
    end)
    self.ui.settingsPopupTrigger:SetPoint("TOPLEFT", 16, -166)
    self.ui.settingsPopupDelay = CreateLabeledEditBox(popupCard, self:L("SETTINGS_DELAY"), 110, nil)
    self.ui.settingsPopupDelay:SetPoint("TOPLEFT", 244, -166)

    local popupDelayApply = CreateActionButton(popupCard, 78, 28, self:L("SETTINGS_SAVE"))
    popupDelayApply:SetPoint("TOPLEFT", self.ui.settingsPopupDelay, "TOPRIGHT", 10, -18)
    popupDelayApply:SetScript("OnClick", function()
        self.db.settings.completionPopupDelay = self:Clamp(tonumber(self.ui.settingsPopupDelay.Input:GetText()) or 0, 0, 10)
        self:RefreshSettingsView()
    end)

    self.ui.settingsPopupScale = CreateLabeledEditBox(popupCard, self:L("SETTINGS_SCALE"), 110, nil)
    self.ui.settingsPopupScale:SetPoint("TOPLEFT", 16, -228)

    local popupScaleApply = CreateActionButton(popupCard, 78, 28, self:L("SETTINGS_APPLY"))
    popupScaleApply:SetPoint("TOPLEFT", self.ui.settingsPopupScale, "TOPRIGHT", 10, -18)
    popupScaleApply:SetScript("OnClick", function()
        self.db.settings.completionPopupScale = self:Clamp(tonumber(self.ui.settingsPopupScale.Input:GetText()) or 1, 0.7, 1.4)
        self:ApplyCompletionPopupScale()
        self:RefreshSettingsView()
    end)

    self.ui.settingsPopupCompactAutoHide = CreateSettingToggle(
        popupCard,
        self:L("SETTINGS_COMPACT_AUTOHIDE"),
        self:L("SETTINGS_COMPACT_AUTOHIDE_DESC"),
        function(checked)
            self.db.settings.completionToastAutoHide = checked and true or false
            self:RefreshSettingsView()
        end
    )
    self.ui.settingsPopupCompactAutoHide:SetPoint("TOPLEFT", 16, -284)
    self.ui.settingsPopupCompactAutoHide:SetPoint("TOPRIGHT", -16, -284)

    self.ui.settingsPopupCompactDuration = CreateLabeledEditBox(popupCard, self:L("SETTINGS_COMPACT_DURATION"), 150, nil)
    self.ui.settingsPopupCompactDuration:SetPoint("TOPLEFT", 16, -338)

    local compactDurationApply = CreateActionButton(popupCard, 78, 28, self:L("SETTINGS_APPLY"))
    compactDurationApply:SetPoint("TOPLEFT", self.ui.settingsPopupCompactDuration, "TOPRIGHT", 10, -18)
    compactDurationApply:SetScript("OnClick", function()
        self.db.settings.completionToastDuration = self:Clamp(tonumber(self.ui.settingsPopupCompactDuration.Input:GetText()) or 10, 3, 60)
        self:RefreshSettingsView()
    end)

    local previewButton = CreateActionButton(popupCard, 160, 28, self:L("SETTINGS_PREVIEW"))
    previewButton:SetPoint("LEFT", compactDurationApply, "RIGHT", 10, 0)
    previewButton:SetScript("OnClick", function()
        self:ShowTestCompletionPopup("full")
    end)

    local compactPreviewButton = CreateActionButton(popupCard, 170, 28, self:L("SETTINGS_PREVIEW_COMPACT"))
    compactPreviewButton:SetPoint("LEFT", previewButton, "RIGHT", 10, 0)
    compactPreviewButton:SetScript("OnClick", function()
        self:ShowTestCompletionPopup("compact")
    end)

    local tooltipCard = CreateFrame("Frame", nil, self.ui.settingsSubtabPages.Tooltip)
    tooltipCard:SetPoint("TOPLEFT", 12, -12)
    tooltipCard:SetPoint("TOPRIGHT", self.ui.settingsSubtabPages.Tooltip, "TOPRIGHT", -12, -12)
    tooltipCard:SetHeight(294)
    self:ApplySurface(tooltipCard, COLORS.sectionBG, COLORS.surface, COLORS.accentSoft)

    local tooltipTitle = CreateFont(tooltipCard, 16, COLORS.text)
    tooltipTitle:SetPoint("TOPLEFT", 16, -16)
    tooltipTitle:SetText(self:L("UI_PLAYER_TOOLTIPS"))

    self.ui.settingsTooltipEnabled = CreateSettingToggle(tooltipCard, self:L("UI_SHOW_ADDON_TOOLTIP"), self:L("UI_SHOW_ADDON_TOOLTIP_DESC"), function(checked)
        self.db.settings.showPlayerHistoryTooltips = checked and true or false
        self:RefreshSettingsView()
    end)
    self.ui.settingsTooltipEnabled:SetPoint("TOPLEFT", 16, -44)
    self.ui.settingsTooltipEnabled:SetPoint("TOPRIGHT", -16, -44)

    self.ui.settingsTooltipHeader = CreateSettingToggle(tooltipCard, self:L("UI_SHOW_ADDON_TITLE"), self:L("UI_SHOW_ADDON_TITLE_DESC"), function(checked)
        self.db.settings.tooltipShowHeader = checked and true or false
        self:RefreshSettingsView()
    end)
    self.ui.settingsTooltipHeader:SetPoint("TOPLEFT", 16, -92)
    self.ui.settingsTooltipHeader:SetPoint("TOPRIGHT", -16, -92)

    self.ui.settingsTooltipRuns = CreateSettingToggle(tooltipCard, self:L("UI_SHOW_RUN_HISTORY"), self:L("UI_SHOW_RUN_HISTORY_DESC"), function(checked)
        self.db.settings.tooltipShowRuns = checked and true or false
        self:RefreshSettingsView()
    end)
    self.ui.settingsTooltipRuns:SetPoint("TOPLEFT", 16, -140)
    self.ui.settingsTooltipRuns:SetPoint("TOPRIGHT", -16, -140)

    self.ui.settingsTooltipRating = CreateSettingToggle(tooltipCard, self:L("UI_SHOW_RATING"), self:L("UI_SHOW_RATING_DESC"), function(checked)
        self.db.settings.tooltipShowRating = checked and true or false
        self:RefreshSettingsView()
    end)
    self.ui.settingsTooltipRating:SetPoint("TOPLEFT", 16, -188)
    self.ui.settingsTooltipRating:SetPoint("TOPRIGHT", -16, -188)

    self.ui.settingsTooltipNotes = CreateSettingToggle(tooltipCard, self:L("UI_SHOW_NOTES"), self:L("UI_SHOW_NOTES_DESC"), function(checked)
        self.db.settings.tooltipShowNotes = checked and true or false
        self:RefreshSettingsView()
    end)
    self.ui.settingsTooltipNotes:SetPoint("TOPLEFT", 16, -236)
    self.ui.settingsTooltipNotes:SetPoint("TOPRIGHT", -16, -236)

    local historyCard = CreateFrame("Frame", nil, self.ui.settingsSubtabPages.General)
    historyCard:SetPoint("TOPLEFT", 12, -352)
    historyCard:SetSize(350, 184)
    self:ApplySurface(historyCard, COLORS.sectionBG, COLORS.surface, COLORS.accentSoft)

    local historyTitle = CreateFont(historyCard, 16, COLORS.text)
    historyTitle:SetPoint("TOPLEFT", 16, -16)
    historyTitle:SetText(self:L("SETTINGS_HISTORY"))

    self.ui.settingsMaxRuns = CreateLabeledEditBox(historyCard, self:L("SAVED_RUNS"), 120, nil)
    self.ui.settingsMaxRuns:SetPoint("TOPLEFT", 16, -44)

    local applyMaxRuns = CreateActionButton(historyCard, 84, 28, self:L("SETTINGS_APPLY"))
    applyMaxRuns:SetPoint("TOPLEFT", self.ui.settingsMaxRuns, "TOPRIGHT", 10, -18)
    applyMaxRuns:SetScript("OnClick", function()
        self:SetMaxRuns(self.ui.settingsMaxRuns.Input:GetText())
        self:RefreshUI()
    end)

    local clearButton = CreateActionButton(historyCard, 150, 28, self:L("SETTINGS_CLEAR_HISTORY"))
    clearButton:SetPoint("TOPLEFT", 16, -108)
    clearButton:SetScript("OnClick", function()
        self:ShowCustomModal({
            title = self:L("SETTINGS_HISTORY"),
            text = self:L("SETTINGS_CLEAR_HISTORY_CONFIRM"),
            inputMode = "none",
            confirmText = YES or "Yes",
            cancelText = NO or "No",
            onAccept = function()
                MythicTools:ClearHistory()
            end,
        })
    end)

    local historyNote = CreateFont(historyCard, 10, COLORS.subdued)
    historyNote:SetPoint("TOPLEFT", clearButton, "BOTTOMLEFT", 0, -10)
    historyNote:SetPoint("RIGHT", historyCard, "RIGHT", -16, 0)
    historyNote:SetJustifyH("LEFT")
    historyNote:SetText(self:L("SETTINGS_HISTORY_NOTE"))

    local notificationsOuterPage = self.ui.settingsSubtabPages.Notifications
    local notificationsScroll = CreateFrame("ScrollFrame", nil, notificationsOuterPage, "UIPanelScrollFrameTemplate")
    notificationsScroll:SetPoint("TOPLEFT", notificationsOuterPage, "TOPLEFT", 0, 0)
    notificationsScroll:SetPoint("BOTTOMRIGHT", notificationsOuterPage, "BOTTOMRIGHT", -12, 0)
    ApplyThinScrollBar(notificationsScroll)

    local notificationsPage = CreateFrame("Frame", nil, notificationsScroll)
    notificationsPage:SetSize(980, 800)
    notificationsPage:SetPoint("TOPLEFT", notificationsScroll, "TOPLEFT", 0, 0)
    notificationsScroll:SetScrollChild(notificationsPage)

    local alertCard = CreateFrame("Frame", nil, notificationsPage)
    alertCard:SetPoint("TOPLEFT", 12, -12)
    alertCard:SetPoint("TOPRIGHT", notificationsPage, "TOPRIGHT", -12, -12)
    alertCard:SetHeight(174)
    self:ApplySurface(alertCard, COLORS.sectionBG, COLORS.surface, COLORS.accentSoft)

    local alertTitle = CreateFont(alertCard, 16, COLORS.text)
    alertTitle:SetPoint("TOPLEFT", 16, -16)
    alertTitle:SetText(self:L("UI_NOTIFICATION_CHANNELS"))

    self.ui.settingsAlerts = CreateSettingToggle(alertCard, self:L("UI_CHAT_NOTIFICATION"), self:L("UI_CHAT_NOTIFICATION_DESC"), function(checked)
        self:SetChatAlertsEnabled(checked)
        self:RefreshSettingsView()
    end)
    self.ui.settingsAlerts:SetPoint("TOPLEFT", 16, -44)
    self.ui.settingsAlerts:SetPoint("TOPRIGHT", -16, -44)

    self.ui.settingsFloatingAlerts = CreateSettingToggle(alertCard, self:L("UI_FLOATING_NOTIFICATION"), self:L("UI_FLOATING_NOTIFICATION_DESC"), function(checked)
        self.db.settings.historyAlertFloating = checked and true or false
        self:RefreshSettingsView()
    end)
    self.ui.settingsFloatingAlerts:SetPoint("TOPLEFT", 16, -88)
    self.ui.settingsFloatingAlerts:SetPoint("TOPRIGHT", -16, -88)

    self.ui.settingsSoundAlerts = CreateSettingToggle(alertCard, self:L("UI_PLAY_SOUND"), self:L("UI_PLAY_SOUND_DESC"), function(checked)
        self.db.settings.historyAlertSound = checked and true or false
        self:RefreshSettingsView()
    end)
    self.ui.settingsSoundAlerts:SetPoint("TOPLEFT", 16, -132)
    self.ui.settingsSoundAlerts:SetPoint("TOPRIGHT", -16, -132)

    local alertMessageCard = CreateFrame("Frame", nil, notificationsPage)
    alertMessageCard:SetPoint("TOPLEFT", alertCard, "BOTTOMLEFT", 0, -12)
    alertMessageCard:SetPoint("TOPRIGHT", notificationsPage, "TOPRIGHT", -12, -198)
    alertMessageCard:SetHeight(176)
    self:ApplySurface(alertMessageCard, COLORS.sectionBG, COLORS.surface, COLORS.accentSoft)

    local alertMessageTitle = CreateFont(alertMessageCard, 16, COLORS.text)
    alertMessageTitle:SetPoint("TOPLEFT", 16, -16)
    alertMessageTitle:SetText(self:L("UI_NOTIFICATION_MESSAGES"))

    self.ui.settingsFloatingAlertTemplate = CreateLabeledEditBox(alertMessageCard, "Floating alert text", 760, function(value)
        self.db.settings.historyAlertFloatingTemplate = self:TrimText(value)
    end)
    self.ui.settingsFloatingAlertTemplate:SetPoint("TOPLEFT", 16, -48)

    self.ui.settingsChatAlertTemplate = CreateLabeledEditBox(alertMessageCard, "Chat message text", 760, function(value)
        self.db.settings.historyAlertChatTemplate = self:TrimText(value)
    end)
    self.ui.settingsChatAlertTemplate:SetPoint("TOPLEFT", 16, -96)

    local tokenHelp = CreateFont(alertMessageCard, 10, COLORS.subdued)
    tokenHelp:SetPoint("TOPLEFT", 16, -150)
    tokenHelp:SetPoint("RIGHT", alertMessageCard, "RIGHT", -16, 0)
    tokenHelp:SetJustifyH("LEFT")
    tokenHelp:SetText("Tokens: {{star}}, {{skull}}, {{square}}, {{moon}}, {{circle}}, {{diamond}}, {{triangle}}, {{cross}}, [player], [full_player], [runs], [timed], [overtime], [success_rate], [rating], [note]")

    local alertMediaCard = CreateFrame("Frame", nil, notificationsPage)
    alertMediaCard:SetPoint("TOPLEFT", alertMessageCard, "BOTTOMLEFT", 0, -12)
    alertMediaCard:SetPoint("TOPRIGHT", notificationsPage, "TOPRIGHT", -12, -386)
    alertMediaCard:SetHeight(112)
    self:ApplySurface(alertMediaCard, COLORS.sectionBG, COLORS.surface, COLORS.accentSoft)

    local alertMediaTitle = CreateFont(alertMediaCard, 16, COLORS.text)
    alertMediaTitle:SetPoint("TOPLEFT", 16, -16)
    alertMediaTitle:SetText(self:L("UI_ALERT_MEDIA"))

    local alertMediaRow = CreateAceFlowRow(alertMediaCard, -48, 56)
    self.ui.settingsAlertSound = CreateAceDropdownWidget(self:L("UI_SOUND"), 200, self:GetAlertSoundOptions(), self.db.settings.historyAlertSoundKey, function(value)
        self.db.settings.historyAlertSoundKey = value or ((self.GetDefaultAlertSoundKey and self:GetDefaultAlertSoundKey()) or "Interface\\AddOns\\SkyMythicHistory\\Media\\Sounds\\OhNo.ogg")
        self:RefreshSettingsView()
    end)
    self.ui.settingsAlertFont = CreateAceDropdownWidget(self:L("UI_FONT"), 200, self:GetAlertFontOptions(), self.db.settings.historyAlertFont, function(value)
        self.db.settings.historyAlertFont = value or "default"
        if self.RefreshHistoryFloatingAlertStyle then
            self:RefreshHistoryFloatingAlertStyle()
        end
    end)
    StyleAceDropdownWidget(self.ui.settingsAlertSound)
    StyleAceDropdownWidget(self.ui.settingsAlertFont)
    if alertMediaRow then
        alertMediaRow:AddChild(self.ui.settingsAlertSound)
        alertMediaRow:AddChild(CreateAceSpacer(10))
        alertMediaRow:AddChild(self.ui.settingsAlertFont)
    end

    local alertStyleCard = CreateFrame("Frame", nil, notificationsPage)
    alertStyleCard:SetPoint("TOPLEFT", alertMediaCard, "BOTTOMLEFT", 0, -12)
    alertStyleCard:SetPoint("TOPRIGHT", notificationsPage, "TOPRIGHT", -12, -510)
    alertStyleCard:SetHeight(236)
    self:ApplySurface(alertStyleCard, COLORS.sectionBG, COLORS.surface, COLORS.accentSoft)

    local alertStyleTitle = CreateFont(alertStyleCard, 16, COLORS.text)
    alertStyleTitle:SetPoint("TOPLEFT", 16, -16)
    alertStyleTitle:SetText(self:L("UI_ALERT_APPEARANCE"))

    local alertStyleRow = CreateAceFlowRow(alertStyleCard, -48, 56)
    self.ui.settingsAlertOutline = CreateAceDropdownWidget(self:L("UI_BORDER"), 130, self:GetAlertOutlineOptions(), self.db.settings.historyAlertOutline, function(value)
        self.db.settings.historyAlertOutline = value or ""
        if self.RefreshHistoryFloatingAlertStyle then
            self:RefreshHistoryFloatingAlertStyle()
        end
    end)
    self.ui.settingsAlertFontSize = CreateLabeledEditBox(alertStyleCard, "Size", 58, function(value)
        self.db.settings.historyAlertFontSize = self:Clamp(tonumber(value) or self.db.settings.historyAlertFontSize or 22, 10, 48)
        if self.RefreshHistoryFloatingAlertStyle then
            self:RefreshHistoryFloatingAlertStyle()
        end
    end)
    self.ui.settingsAlertFontSize:SetPoint("TOPLEFT", 176, -48)
    StyleAceDropdownWidget(self.ui.settingsAlertOutline)
    if alertStyleRow then
        alertStyleRow:AddChild(self.ui.settingsAlertOutline)
    end

    local function ApplyAlertCoordinatesFromInputs(skipRefresh)
        local currentX, currentY = self:GetHistoryAlertCoordinates()
        local nextX = self.ui.settingsAlertX and tonumber(self.ui.settingsAlertX.Input:GetText()) or currentX
        local nextY = self.ui.settingsAlertY and tonumber(self.ui.settingsAlertY.Input:GetText()) or currentY
        self:SetHistoryAlertCoordinates(nextX or currentX, nextY or currentY, skipRefresh)
        local appliedX, appliedY = self:GetHistoryAlertCoordinates()
        if self.ui.settingsAlertXSlider then
            self.ui.settingsAlertXSlider:SetValue(appliedX)
        end
        if self.ui.settingsAlertYSlider then
            self.ui.settingsAlertYSlider:SetValue(appliedY)
        end
        if self.db.settings.historyAlertLocked == false then
            self:ShowHistoryAlertMoveHandle()
        end
    end

    self.ui.settingsAlertXSlider = CreateLabeledSlider(alertStyleCard, "X position", 300, -500, 500, 1, function(value)
        local _, currentY = self:GetHistoryAlertCoordinates()
        local nextX = floor((tonumber(value) or 0) + 0.5)
        self:SetHistoryAlertCoordinates(nextX, currentY, true)
        if self.ui.settingsAlertX and not self.ui.settingsAlertX.Input:HasFocus() then
            self.ui.settingsAlertX.Input:SetText(tostring(nextX))
        end
        if self.db.settings.historyAlertLocked == false then
            self:ShowHistoryAlertMoveHandle()
        end
    end)
    self.ui.settingsAlertXSlider:SetPoint("TOPLEFT", 16, -102)

    self.ui.settingsAlertYSlider = CreateLabeledSlider(alertStyleCard, "Y position", 300, -500, 500, 1, function(value)
        local currentX = self:GetHistoryAlertCoordinates()
        local nextY = floor((tonumber(value) or 0) + 0.5)
        self:SetHistoryAlertCoordinates(currentX, nextY, true)
        if self.ui.settingsAlertY and not self.ui.settingsAlertY.Input:HasFocus() then
            self.ui.settingsAlertY.Input:SetText(tostring(nextY))
        end
        if self.db.settings.historyAlertLocked == false then
            self:ShowHistoryAlertMoveHandle()
        end
    end)
    self.ui.settingsAlertYSlider:SetPoint("TOPLEFT", 16, -164)

    self.ui.settingsAlertX = CreateLabeledEditBox(alertStyleCard, "X", 74, function()
        ApplyAlertCoordinatesFromInputs(true)
    end)
    self.ui.settingsAlertX:SetPoint("TOPLEFT", self.ui.settingsAlertXSlider, "TOPRIGHT", 30, 0)

    self.ui.settingsAlertY = CreateLabeledEditBox(alertStyleCard, "Y", 74, function()
        ApplyAlertCoordinatesFromInputs(true)
    end)
    self.ui.settingsAlertY:SetPoint("TOPLEFT", self.ui.settingsAlertYSlider, "TOPRIGHT", 30, 0)

    self.ui.settingsAlertMoveLock = CreateActionButton(alertStyleCard, 92, 28, self:L("UI_UNLOCK"))
    self.ui.settingsAlertMoveLock:SetPoint("TOPRIGHT", -128, -48)
    self:ApplyActionButtonIcon(self.ui.settingsAlertMoveLock, "Interface\\AddOns\\SkyMythicHistory\\Media\\Icons\\right.tga", 12)
    self.ui.settingsAlertMoveLock:SetScript("OnClick", function()
        self:SetHistoryAlertMoveLocked(self.db.settings.historyAlertLocked == false)
    end)

    local alertPreview = CreateActionButton(alertStyleCard, 110, 28, self:L("UI_PREVIEW"))
    alertPreview:SetPoint("TOPRIGHT", -16, -48)
    self:ApplyActionButtonIcon(alertPreview, "Interface\\AddOns\\SkyMythicHistory\\Media\\Icons\\info.tga", 12)
    alertPreview:SetScript("OnClick", function()
        self.db.settings.historyAlertFontSize = self:Clamp(tonumber(self.ui.settingsAlertFontSize.Input:GetText()) or self.db.settings.historyAlertFontSize or 22, 10, 48)
        self.db.settings.historyAlertFloatingTemplate = self:TrimText(self.ui.settingsFloatingAlertTemplate.Input:GetText())
        self.db.settings.historyAlertChatTemplate = self:TrimText(self.ui.settingsChatAlertTemplate.Input:GetText())
        self:PreviewHistoryAlert()
        self:RefreshSettingsView()
    end)

    local summaryCard = CreateFrame("Frame", nil, self.ui.settingsSubtabPages.General)
    summaryCard:SetPoint("TOPLEFT", generalCard, "TOPRIGHT", 12, 0)
    summaryCard:SetPoint("BOTTOMRIGHT", self.ui.settingsSubtabPages.General, "BOTTOMRIGHT", -12, 12)
    self:ApplySurface(summaryCard, COLORS.sectionBG, COLORS.surface, COLORS.accentSoft)

    local summaryTitle = CreateFont(summaryCard, 16, COLORS.text)
    summaryTitle:SetPoint("TOPLEFT", 16, -16)
    summaryTitle:SetText(self:L("SETTINGS_SUMMARY"))

    self.ui.settingsSummary = CreateFont(summaryCard, 12, COLORS.muted)
    self.ui.settingsSummary:SetPoint("TOPLEFT", summaryTitle, "BOTTOMLEFT", 0, -14)
    self.ui.settingsSummary:SetPoint("RIGHT", summaryCard, "RIGHT", -16, 0)
    self.ui.settingsSummary:SetJustifyH("LEFT")
    self.ui.settingsSummary:SetJustifyV("TOP")

    if not self.ui.settingsSubtabPages[self.db.ui.settingsTab] then
        self.db.ui.settingsTab = "General"
    end
    for key, subPage in pairs(self.ui.settingsSubtabPages) do
        subPage:SetShown(key == self.db.ui.settingsTab)
    end
    for key, button in pairs(self.ui.settingsSubtabButtons) do
        button._selected = key == self.db.ui.settingsTab
        if button._restyle then
            button:_restyle()
        end
    end

    return page
end

