TargetMarkerAssistant = TargetMarkerAssistant or {}
TMA_Config_Notice = TMA_Config_Notice or {}

local Module = TMA_Config_Notice
local TMA = TargetMarkerAssistant

function Module.Create(ctx)
    local ui = ctx.ui
    local L = ctx.L
    local page = ctx.castAlertPage
    local module = {}

    local function CreateSectionLabel(parent, text, width) return ui.CreateSectionLabel(ctx, parent, text, width) end
    local function CreateFieldLabel(parent, text, width) return ui.CreateFieldLabel(ctx, parent, text, width) end
    local function CreateDescription(parent, text, width, style) return ui.CreateDescription(ctx, parent, text, width, style) end
    local function CreateHintDescription(parent, text, width) return ui.CreateHintDescription(ctx, parent, text, width) end
    local function CreateSectionRule(parent, anchor, width) return ui.CreateSectionRule(ctx, parent, anchor, width) end
    local function ConfigureCheckText(check, width) return ui.ConfigureCheckText(ctx, check, width) end

    local castAlertSection = CreateSectionLabel(page, L["SECTION_CAST_REMINDER"], ctx.formSectionWidth)
    castAlertSection:SetPoint("TOPLEFT", page, "TOPLEFT", ctx.formLeftX, ctx.formTopY)
    local castAlertRule = CreateSectionRule(page, castAlertSection, ctx.formSectionWidth)
    ui.CreateColumnDivider(ctx, page, "TargetMarkerAssistantCastAlertColumnDivider")

    TMA.CastSoundSection = CreateSectionLabel(page, L["SECTION_ALERT_SOUND"], ctx.formSectionWidth)
    TMA.CastSoundSection:SetPoint("TOPLEFT", page, "TOPLEFT", ctx.formRightX, ctx.formTopY)

    local castAlertCheck = CreateFrame("CheckButton", "TargetMarkerAssistantCastAlertCheckbox", page, "InterfaceOptionsCheckButtonTemplate")
    castAlertCheck:SetPoint("TOPLEFT", castAlertRule, "BOTTOMLEFT", ctx.SECTION_CONTENT_LEFT_INSET, -ctx.SECTION_RULE_CONTENT_GAP)
    castAlertCheck.Text:SetText(L["ENABLE_CAST_ALERT_DESC"])
    ConfigureCheckText(castAlertCheck, ctx.formSectionWidth - 30)
    castAlertCheck:SetScript("OnClick", function(self)
        TargetMarkerAssistantDB.enableCastAlert = self:GetChecked()
        ctx.ScheduleFocusCastAlertRefresh()
    end)

    local castAlertDescriptionWidth = ctx.formDescriptionWidth - 28

    local castAlertDesc = CreateDescription(page, L["DESC_CAST_ALERT"], castAlertDescriptionWidth)
    castAlertDesc:SetPoint("TOPLEFT", castAlertCheck.Text, "BOTTOMLEFT", 0, -ctx.TEXT_DESCRIPTION_GAP)

    local castAlertNote = CreateHintDescription(page, L["DESC_CAST_ALERT_NOTE"], castAlertDescriptionWidth)
    castAlertNote:SetPoint("TOPLEFT", castAlertDesc, "BOTTOMLEFT", 0, -ctx.DESCRIPTION_HINT_GAP)

    local castSoundRule = CreateSectionRule(page, TMA.CastSoundSection, ctx.formSectionWidth)

    local castAlertSoundLabel = CreateFieldLabel(page, L["CAST_ALERT_SOUND"])
    castAlertSoundLabel:SetPoint("TOPLEFT", castSoundRule, "BOTTOMLEFT", ctx.SECTION_CONTENT_LEFT_INSET, -ctx.SECTION_RULE_CONTENT_GAP)

    local castAlertSoundDropdown = CreateFrame("DropdownButton", "TargetMarkerAssistantCastAlertSoundDropdown", page, "WowStyle1DropdownTemplate")
    castAlertSoundDropdown:SetPoint("TOPLEFT", castAlertSoundLabel, "BOTTOMLEFT", 0, -ctx.LABEL_CONTROL_GAP)
    castAlertSoundDropdown:SetWidth(ctx.castAlertSoundDropdownWidth)
    ui.SetupRadioDropdown(castAlertSoundDropdown, "MENU_TARGETMARKERASSISTANT_CAST_ALERT_SOUND", function()
        return ctx.GetCastAlertSound(TargetMarkerAssistantDB.castAlertSound).text
    end, function()
        return ctx.GetCastAlertSound(TargetMarkerAssistantDB.castAlertSound).value
    end, function(value)
        TargetMarkerAssistantDB.castAlertSound = value
    end, function()
        return ctx.castAlertSounds
    end, ctx.PlayCastAlertSound)

    local castAlertChannelLabel = CreateFieldLabel(page, L["CAST_ALERT_CHANNEL"])
    castAlertChannelLabel:SetPoint("TOPLEFT", castAlertSoundDropdown, "BOTTOMLEFT", 0, -ctx.FIELD_BLOCK_GAP)

    local castAlertChannelDropdown = CreateFrame("DropdownButton", "TargetMarkerAssistantCastAlertChannelDropdown", page, "WowStyle1DropdownTemplate")
    castAlertChannelDropdown:SetPoint("TOPLEFT", castAlertChannelLabel, "BOTTOMLEFT", 0, -ctx.LABEL_CONTROL_GAP)
    castAlertChannelDropdown:SetWidth(ctx.compactDropdownWidth)
    ui.SetupRadioDropdown(castAlertChannelDropdown, "MENU_TARGETMARKERASSISTANT_CAST_ALERT_CHANNEL", function()
        return ctx.GetSoundChannel(TargetMarkerAssistantDB.castAlertChannel).text
    end, function()
        return TargetMarkerAssistantDB.castAlertChannel
    end, function(value)
        TargetMarkerAssistantDB.castAlertChannel = value
    end, function()
        return ctx.soundChannels
    end, ctx.PlayCastAlertSound)

    local testSoundButton = CreateFrame("Button", "TargetMarkerAssistantTestSoundButton", page, "UIPanelButtonTemplate")
    testSoundButton:SetSize(ctx.castAlertTestButtonWidth, 24)
    testSoundButton:SetPoint("LEFT", castAlertChannelDropdown, "RIGHT", ctx.castAlertInlineControlGap, 0)
    testSoundButton:SetText(L["TEST_SOUND"])
    testSoundButton:SetScript("OnClick", ctx.PlayCastAlertSound)

    local castChannelDesc = CreateDescription(page, L["DESC_CAST_CHANNEL"], ctx.formDescriptionWidth)
    castChannelDesc:SetPoint("TOPLEFT", castAlertChannelDropdown, "BOTTOMLEFT", 0, -ctx.CONTROL_DESCRIPTION_GAP)

    function module.Refresh()
        castAlertCheck:SetChecked(TargetMarkerAssistantDB.enableCastAlert)
        ui.RefreshDropdown(castAlertSoundDropdown)
        ui.RefreshDropdown(castAlertChannelDropdown)
        ui.FitScrollContentToViewport(ctx.castAlertScrollFrame, page)
    end

    return module
end
