local addonName, addon = ...

local UI = addon.UI

local function SetDropDownText(dropdown, text)
    UIDropDownMenu_SetText(
        dropdown,
        text or "NONE"
    )
end

local function CharacterDisplay(character)

    if not character then
        return "NO CHARACTER"
    end

    if character.realm
        and character.realm ~= ""
    then

        return string.format(
            "%s - %s",
            character.name or "?",
            character.realm
        )

    end

    return character.name or "?"
end

local function CreateLabel(
    parent,
    text,
    x,
    y
)

    local label =
        parent:CreateFontString(
            nil,
            "OVERLAY"
        )

    label:SetFontObject(
        GameFontNormal
    )

    label:SetPoint(
        "TOPLEFT",
        parent,
        "TOPLEFT",
        x,
        y
    )

    label:SetText(text)

    label:SetTextColor(
        unpack(UI.COLORS.muted)
    )

    return label
end

------------------------------------------------------------
-- INITIALIZE
------------------------------------------------------------

function UI:InitializeTaskInput()

    local panel =
        self.InputPanel

    --------------------------------------------------------
    -- ROW 1: TASK
    --------------------------------------------------------

    local taskNameLabel = panel:CreateFontString(nil, "OVERLAY")
    taskNameLabel:SetFontObject(GameFontNormal)
    taskNameLabel:SetPoint("TOPLEFT", panel, "TOPLEFT", 12, -10)
    taskNameLabel:SetText(UI:LT("NEW TASK"))
    taskNameLabel:SetTextColor(unpack(self.COLORS.gold))

    local taskHint = panel:CreateFontString(nil, "OVERLAY")
    taskHint:SetFontObject(GameFontHighlightSmall)
    taskHint:SetText(UI:LT("Type the task name here..."))
    taskHint:SetTextColor(unpack(self.COLORS.muted))

    local taskInput =
        CreateFrame(
            "EditBox",
            nil,
            panel,
            "InputBoxTemplate"
        )

    self.TaskInput =
        taskInput

    taskInput:SetPoint(
        "TOPLEFT",
        panel,
        "TOPLEFT",
        10,
        -32
    )

    taskInput:SetPoint(
        "TOPRIGHT",
        panel,
        "TOPRIGHT",
        -200,
        -32
    )

    taskInput:SetHeight(36)
    taskInput:SetAutoFocus(false)
    taskInput:SetMaxLetters(200)
    taskInput:SetFontObject(GameFontNormal)

    taskHint:SetPoint("LEFT", taskInput, "LEFT", 8, 0)
    taskHint:SetPoint("RIGHT", taskInput, "RIGHT", -8, 0)
    taskHint:SetJustifyH("LEFT")

    local function UpdateTaskHint()
        taskHint:SetShown((taskInput:GetText() or "") == "" and not taskInput:HasFocus())
    end

    taskInput:SetScript("OnTextChanged", UpdateTaskHint)
    taskInput:SetScript("OnEditFocusGained", function() taskHint:Hide() end)
    taskInput:SetScript("OnEditFocusLost", UpdateTaskHint)

    local addButton =
        CreateFrame(
            "Button",
            nil,
            panel,
            "UIPanelButtonTemplate"
        )

    self.AddButton =
        addButton

    addButton:SetSize(
        100,
        36
    )

    addButton:SetPoint(
        "TOPRIGHT",
        panel,
        "TOPRIGHT",
        -10,
        -32
    )

    addButton:SetText(
        self:T("CREATE", "ADD")
    )

    self:ApplyRedButton(addButton)

    local cancelEditButton =
        CreateFrame(
            "Button",
            nil,
            panel,
            "UIPanelButtonTemplate"
        )

    self.CancelEditButton =
        cancelEditButton

    cancelEditButton:SetSize(
        82,
        36
    )

    cancelEditButton:SetPoint(
        "RIGHT",
        addButton,
        "LEFT",
        -8,
        0
    )

    cancelEditButton:SetText(UI:LT("CANCEL"))

    cancelEditButton:Hide()

    cancelEditButton:SetScript(
        "OnClick",
        function()
            if self.CancelTaskEdit then
                self:CancelTaskEdit()
            end
        end
    )

    addButton:SetScript(
        "OnClick",
        function()
            self:CreateQuickTask()
        end
    )

    taskInput:SetScript(
        "OnEnterPressed",
        function()
            self:CreateQuickTask()
        end
    )
    UpdateTaskHint()

    --------------------------------------------------------
    -- ROW 2: DATE / TIME / REPEAT
    --------------------------------------------------------

    local dateLabel =
        CreateLabel(
            panel,
            "DATE:",
            12,
            -82
        )

    local dateButton =
        CreateFrame(
            "Button",
            nil,
            panel,
            "UIPanelButtonTemplate"
        )

    self.DateButton =
        dateButton

    dateButton:SetSize(
        115,
        25
    )

    dateButton:SetPoint(
        "LEFT",
        dateLabel,
        "RIGHT",
        8,
        0
    )

    dateButton:SetText(
        self:FormatDate(
            self.SelectedDate
        )
    )

    local noDateButton =
        CreateFrame(
            "Button",
            nil,
            panel,
            "UIPanelButtonTemplate"
        )

    self.NoDateButton =
        noDateButton

    noDateButton:SetSize(
        90,
        25
    )

    noDateButton:SetPoint(
        "LEFT",
        dateButton,
        "RIGHT",
        5,
        0
    )

    noDateButton:SetText(UI:LT("NO DATE"))

    local timeCheck =
        CreateFrame(
            "CheckButton",
            nil,
            panel,
            "UICheckButtonTemplate"
        )

    self.TimeCheck =
        timeCheck

    timeCheck:SetSize(
        26,
        26
    )

    timeCheck:SetPoint(
        "LEFT",
        noDateButton,
        "RIGHT",
        15,
        0
    )

    timeCheck:SetChecked(false)

    local timeTextButton =
        CreateFrame(
            "Button",
            nil,
            panel
        )

    timeTextButton:SetSize(
        42,
        24
    )

    timeTextButton:SetPoint(
        "LEFT",
        timeCheck,
        "RIGHT",
        2,
        0
    )

    local timeText =
        timeTextButton:CreateFontString(
            nil,
            "OVERLAY"
        )

    timeText:SetFontObject(
        GameFontNormal
    )

    timeText:SetAllPoints()
    timeText:SetJustifyH("LEFT")
    timeText:SetText(UI:LT("TIME"))

    local hourInput =
        CreateFrame(
            "EditBox",
            nil,
            panel,
            "InputBoxTemplate"
        )

    self.HourInput =
        hourInput

    hourInput:SetSize(
        38,
        25
    )

    hourInput:SetPoint(
        "LEFT",
        timeTextButton,
        "RIGHT",
        5,
        0
    )

    hourInput:SetAutoFocus(false)
    hourInput:SetMaxLetters(2)
    hourInput:SetText("20")
    hourInput:SetJustifyH("CENTER")

    local colon =
        panel:CreateFontString(
            nil,
            "OVERLAY"
        )

    colon:SetFontObject(
        GameFontNormal
    )

    colon:SetPoint(
        "LEFT",
        hourInput,
        "RIGHT",
        2,
        0
    )

    colon:SetText(":")

    local minuteInput =
        CreateFrame(
            "EditBox",
            nil,
            panel,
            "InputBoxTemplate"
        )

    self.MinuteInput =
        minuteInput

    minuteInput:SetSize(
        38,
        25
    )

    minuteInput:SetPoint(
        "LEFT",
        colon,
        "RIGHT",
        2,
        0
    )

    minuteInput:SetAutoFocus(false)
    minuteInput:SetMaxLetters(2)
    minuteInput:SetText("00")
    minuteInput:SetJustifyH("CENTER")

    local repeatLabel =
        CreateLabel(
            panel,
            "REPEAT:",
            555,
            -82
        )

    local repeatDropDown =
        CreateFrame(
            "Frame",
            "WhatsNextRepeatDropDown",
            panel,
            "UIDropDownMenuTemplate"
        )

    self.RepeatDropDown =
        repeatDropDown

    repeatDropDown:SetPoint(
        "LEFT",
        repeatLabel,
        "RIGHT",
        -5,
        -2
    )

    UIDropDownMenu_SetWidth(
        repeatDropDown,
        125
    )

    self.RepeatType = "none"
    self.RepeatInterval = 1
    self.RepeatUnit = "days"

    SetDropDownText(
        repeatDropDown,
        UI:LT("NONE")
    )

    local customPanel =
        CreateFrame(
            "Frame",
            nil,
            panel
        )

    self.CustomRepeatPanel =
        customPanel

    customPanel:SetSize(
        205,
        28
    )

    customPanel:SetPoint(
        "LEFT",
        repeatDropDown,
        "RIGHT",
        -5,
        0
    )

    customPanel:Hide()

    local everyText =
        customPanel:CreateFontString(
            nil,
            "OVERLAY"
        )

    everyText:SetFontObject(
        GameFontNormal
    )

    everyText:SetPoint(
        "LEFT",
        customPanel,
        "LEFT",
        0,
        0
    )

    everyText:SetText(UI:LT("EVERY"))

    local intervalInput =
        CreateFrame(
            "EditBox",
            nil,
            customPanel,
            "InputBoxTemplate"
        )

    self.IntervalInput =
        intervalInput

    intervalInput:SetSize(
        35,
        25
    )

    intervalInput:SetPoint(
        "LEFT",
        everyText,
        "RIGHT",
        5,
        0
    )

    intervalInput:SetAutoFocus(false)
    intervalInput:SetMaxLetters(3)
    intervalInput:SetText("1")
    intervalInput:SetJustifyH("CENTER")

    local unitDropDown =
        CreateFrame(
            "Frame",
            "WhatsNextUnitDropDown",
            customPanel,
            "UIDropDownMenuTemplate"
        )

    self.UnitDropDown =
        unitDropDown

    unitDropDown:SetPoint(
        "LEFT",
        intervalInput,
        "RIGHT",
        -8,
        -2
    )

    UIDropDownMenu_SetWidth(
        unitDropDown,
        85
    )

    SetDropDownText(
        unitDropDown,
        UI:LT("DAYS")
    )

    UIDropDownMenu_Initialize(
        repeatDropDown,
        function()

            local options = {
                {
                    text = "NONE",
                    value = "none",
                },
                {
                    text = "EVERY DAY",
                    value = "day",
                },
                {
                    text = "EVERY WEEK",
                    value = "week",
                },
                {
                    text = "EVERY MONTH",
                    value = "month",
                },
                {
                    text = "CUSTOM",
                    value = "custom",
                },
            }

            for _, option in ipairs(options) do

                local info =
                    UIDropDownMenu_CreateInfo()

                info.text =
                    UI:LT(option.text)

                info.func =
                    function()

                        self.RepeatType =
                            option.value

                        SetDropDownText(
                            repeatDropDown,
                            UI:LT(option.text)
                        )

                        if option.value ==
                            "custom"
                        then
                            customPanel:Show()
                        else
                            customPanel:Hide()
                        end

                    end

                UIDropDownMenu_AddButton(
                    info
                )

            end

        end
    )

    UIDropDownMenu_Initialize(
        unitDropDown,
        function()

            local options = {
                {
                    text = "DAYS",
                    value = "days",
                },
                {
                    text = "WEEKS",
                    value = "weeks",
                },
                {
                    text = "MONTHS",
                    value = "months",
                },
            }

            for _, option in ipairs(options) do

                local info =
                    UIDropDownMenu_CreateInfo()

                info.text =
                    UI:LT(option.text)

                info.func =
                    function()

                        self.RepeatUnit =
                            option.value

                        SetDropDownText(
                            unitDropDown,
                            UI:LT(option.text)
                        )

                    end

                UIDropDownMenu_AddButton(
                    info
                )

            end

        end
    )

    --------------------------------------------------------
    -- ROW 3: CATEGORY + CONTEXT
    --------------------------------------------------------

    local categoryLabel =
        CreateLabel(
            panel,
            "CATEGORY:",
            12,
            -127
        )

    local categoryDropDown =
        CreateFrame(
            "Frame",
            "WhatsNextCategoryDropDown",
            panel,
            "UIDropDownMenuTemplate"
        )

    self.CategoryDropDown =
        categoryDropDown

    categoryDropDown:SetPoint(
        "LEFT",
        categoryLabel,
        "RIGHT",
        -5,
        -2
    )

    UIDropDownMenu_SetWidth(
        categoryDropDown,
        135
    )

    self.NewTaskCategory =
        addon.DB.CATEGORIES.OTHER

    SetDropDownText(
        categoryDropDown,
        UI:LT("OTHER")
    )

    local categoryManagerButton =
        CreateFrame(
            "Button",
            nil,
            panel,
            "UIPanelButtonTemplate"
        )

    self.CategoryManagerButton =
        categoryManagerButton

    categoryManagerButton:SetSize(
        30,
        24
    )

    categoryManagerButton:SetPoint(
        "TOPLEFT",
        panel,
        "TOPLEFT",
        282,
        -122
    )

    categoryManagerButton:SetText(
        "+"
    )

    categoryManagerButton:SetScript(
        "OnEnter",
        function()
            GameTooltip:SetOwner(
                categoryManagerButton,
                "ANCHOR_TOP"
            )
            GameTooltip:SetText(UI:LT("MANAGE CUSTOM CATEGORIES"))
            GameTooltip:Show()
        end
    )

    categoryManagerButton:SetScript(
        "OnLeave",
        function()
            GameTooltip:Hide()
        end
    )

    categoryManagerButton:SetScript(
        "OnClick",
        function()
            if self.ShowCategoryManager then
                self:ShowCategoryManager()
            end
        end
    )

    local contextLabel1 =
        CreateLabel(
            panel,
            "",
            320,
            -127
        )

    local contextDropDown1 =
        CreateFrame(
            "Frame",
            "WhatsNextContextDropDown1",
            panel,
            "UIDropDownMenuTemplate"
        )

    self.ContextLabel1 =
        contextLabel1

    self.ContextDropDown1 =
        contextDropDown1

    contextDropDown1:SetPoint(
        "LEFT",
        contextLabel1,
        "RIGHT",
        -5,
        -2
    )

    UIDropDownMenu_SetWidth(
        contextDropDown1,
        205
    )

    local contextLabel2 =
        CreateLabel(
            panel,
            "",
            705,
            -127
        )

    local contextDropDown2 =
        CreateFrame(
            "Frame",
            "WhatsNextContextDropDown2",
            panel,
            "UIDropDownMenuTemplate"
        )

    self.ContextLabel2 =
        contextLabel2

    self.ContextDropDown2 =
        contextDropDown2

    contextDropDown2:SetPoint(
        "LEFT",
        contextLabel2,
        "RIGHT",
        -5,
        -2
    )

    UIDropDownMenu_SetWidth(
        contextDropDown2,
        230
    )

    --------------------------------------------------------
    -- CONTEXT STATE
    --------------------------------------------------------

    self.NewTaskCharacterKey = nil
    self.NewTaskSpecID = nil
    self.NewTaskSpecName = nil

    self.NewTaskProfessionID = nil
    self.NewTaskProfessionName = nil

    self.NewTaskInstanceID = nil
    self.NewTaskInstanceName = nil

    self.NewTaskDungeonMode = nil
    self.NewTaskRaidExpansion = nil

    function self:ResetNewTaskContext()

        self.NewTaskCharacterKey = nil
        self.NewTaskSpecID = nil
        self.NewTaskSpecName = nil

        self.NewTaskProfessionID = nil
        self.NewTaskProfessionName = nil

        self.NewTaskInstanceID = nil
        self.NewTaskInstanceName = nil

        self.NewTaskDungeonMode = nil
        self.NewTaskRaidExpansion = nil

    end

    --------------------------------------------------------
    -- DYNAMIC CONTEXT
    --------------------------------------------------------

    function self:RefreshTaskContextSelectors()

        local category =
            self.NewTaskCategory
            or "OTHER"

        contextLabel1:Hide()
        contextLabel2:Hide()

        contextDropDown1:Hide()
        contextDropDown2:Hide()

        if category == "CHARACTER" then

            contextLabel1:SetText(UI:LT("CHARACTER:"))

            contextLabel2:SetText(UI:LT("SPEC:"))

            contextLabel1:Show()
            contextLabel2:Show()

            contextDropDown1:Show()
            contextDropDown2:Show()

            local characters =
                addon.GameData:GetCharacters()

            if not self.NewTaskCharacterKey then

                self.NewTaskCharacterKey =
                    addon.GameData:GetCurrentCharacterKey()
                    or (
                        characters[1]
                        and characters[1].key
                    )

            end

            local currentCharacter =
                addon.GameData:GetCharacter(
                    self.NewTaskCharacterKey
                )

            SetDropDownText(
                contextDropDown1,
                CharacterDisplay(
                    currentCharacter
                )
            )

            UIDropDownMenu_Initialize(
                contextDropDown1,
                function()

                    for _, character in ipairs(
                        addon.GameData:GetCharacters()
                    ) do

                        local info =
                            UIDropDownMenu_CreateInfo()

                        info.text =
                            CharacterDisplay(
                                character
                            )

                        info.func =
                            function()

                                self.NewTaskCharacterKey =
                                    character.key

                                self.NewTaskSpecID = nil
                                self.NewTaskSpecName = nil

                                self:RefreshTaskContextSelectors()

                            end

                        UIDropDownMenu_AddButton(
                            info
                        )

                    end

                end
            )

            local specs =
                addon.GameData:GetSpecs(
                    self.NewTaskCharacterKey
                )

            if not self.NewTaskSpecID then

                local character =
                    addon.GameData:GetCharacter(
                        self.NewTaskCharacterKey
                    )

                self.NewTaskSpecID =
                    character
                    and character.currentSpecID
                    or (
                        specs[1]
                        and specs[1].id
                    )

            end

            local specText =
                "NO SPEC"

            for _, spec in ipairs(specs) do

                if tonumber(spec.id) ==
                    tonumber(
                        self.NewTaskSpecID
                    )
                then

                    specText =
                        spec.name

                    self.NewTaskSpecName =
                        spec.name

                    break

                end

            end

            SetDropDownText(
                contextDropDown2,
                specText
            )

            UIDropDownMenu_Initialize(
                contextDropDown2,
                function()

                    for _, spec in ipairs(specs) do

                        local info =
                            UIDropDownMenu_CreateInfo()

                        info.text =
                            spec.name

                        info.func =
                            function()

                                self.NewTaskSpecID =
                                    spec.id

                                self.NewTaskSpecName =
                                    spec.name

                                SetDropDownText(
                                    contextDropDown2,
                                    spec.name
                                )

                            end

                        UIDropDownMenu_AddButton(
                            info
                        )

                    end

                end
            )

        elseif category == "PROFESSION" then

            contextLabel1:SetText(UI:LT("CHARACTER:"))

            contextLabel2:SetText(UI:LT("PROFESSION:"))

            contextLabel1:Show()
            contextLabel2:Show()

            contextDropDown1:Show()
            contextDropDown2:Show()

            local characters =
                addon.GameData:GetCharacters()

            if not self.NewTaskCharacterKey then

                self.NewTaskCharacterKey =
                    addon.GameData:GetCurrentCharacterKey()
                    or (
                        characters[1]
                        and characters[1].key
                    )

            end

            local character =
                addon.GameData:GetCharacter(
                    self.NewTaskCharacterKey
                )

            SetDropDownText(
                contextDropDown1,
                CharacterDisplay(character)
            )

            UIDropDownMenu_Initialize(
                contextDropDown1,
                function()

                    for _, item in ipairs(
                        addon.GameData:GetCharacters()
                    ) do

                        local info =
                            UIDropDownMenu_CreateInfo()

                        info.text =
                            CharacterDisplay(item)

                        info.func =
                            function()

                                self.NewTaskCharacterKey =
                                    item.key

                                self.NewTaskProfessionID =
                                    nil

                                self.NewTaskProfessionName =
                                    nil

                                self:RefreshTaskContextSelectors()

                            end

                        UIDropDownMenu_AddButton(
                            info
                        )

                    end

                end
            )

            local professions =
                addon.GameData:GetProfessions(
                    self.NewTaskCharacterKey
                )

            local selected = nil

            for _, profession in ipairs(
                professions
            ) do

                if tonumber(profession.id) ==
                    tonumber(
                        self.NewTaskProfessionID
                    )
                then

                    selected =
                        profession

                    break

                end

            end

            if not selected then

                selected =
                    professions[1]

                if selected then

                    self.NewTaskProfessionID =
                        selected.id

                    self.NewTaskProfessionName =
                        selected.name

                end

            end

            SetDropDownText(
                contextDropDown2,
                selected
                and (
                    selected.expansionName
                    or selected.name
                )
                or UI:LT("NO PROFESSION")
            )

            UIDropDownMenu_Initialize(
                contextDropDown2,
                function()

                    for _, profession in ipairs(
                        professions
                    ) do

                        local info =
                            UIDropDownMenu_CreateInfo()

                        info.text =
                            profession.expansionName
                            or profession.name

                        info.func =
                            function()

                                self.NewTaskProfessionID =
                                    profession.id

                                self.NewTaskProfessionName =
                                    profession.name

                                SetDropDownText(
                                    contextDropDown2,
                                    profession.expansionName
                                    or profession.name
                                )

                            end

                        UIDropDownMenu_AddButton(
                            info
                        )

                    end

                end
            )

        elseif category == "DUNGEON" then

            contextLabel1:SetText(UI:LT("KEY:"))

            contextLabel1:Show()
            contextDropDown1:Show()

            self.NewTaskDungeonMode =
                "KEYS"

            local keys =
                addon.GameData:GetKeys()

            local selected = nil

            for _, keyData in ipairs(keys) do

                if tonumber(keyData.id) ==
                    tonumber(
                        self.NewTaskInstanceID
                    )
                then

                    selected =
                        keyData

                    break

                end

            end

            if not selected then

                selected =
                    keys[1]

                if selected then

                    self.NewTaskInstanceID =
                        selected.id

                    self.NewTaskInstanceName =
                        selected.name

                end

            end

            SetDropDownText(
                contextDropDown1,
                selected
                and addon.Localization:GetInstanceName("DUNGEON", selected.id, selected.name)
                or UI:LT("NO KEYS")
            )

            UIDropDownMenu_SetWidth(
                contextDropDown1,
                330
            )

            UIDropDownMenu_Initialize(
                contextDropDown1,
                function()

                    for _, keyData in ipairs(
                        keys
                    ) do

                        local info =
                            UIDropDownMenu_CreateInfo()

                        info.text =
                            addon.Localization:GetInstanceName("DUNGEON", keyData.id, keyData.name)

                        info.func =
                            function()

                                self.NewTaskInstanceID =
                                    keyData.id

                                self.NewTaskInstanceName =
                                    keyData.name

                                self.NewTaskDungeonMode =
                                    "KEYS"

                                SetDropDownText(
                                    contextDropDown1,
                                    addon.Localization:GetInstanceName("DUNGEON", keyData.id, keyData.name)
                                )

                            end

                        UIDropDownMenu_AddButton(
                            info
                        )

                    end

                end
            )

        elseif category == "RAID" then

            contextLabel1:SetText(UI:LT("EXPANSION:"))

            contextLabel2:SetText(UI:LT("RAID:"))

            contextLabel1:Show()
            contextLabel2:Show()

            contextDropDown1:Show()
            contextDropDown2:Show()

            local expansions =
                addon.GameData:GetRaidExpansions()

            if not self.NewTaskRaidExpansion then

                self.NewTaskRaidExpansion =
                    addon.GameData.currentTierName
                    or (
                        expansions[1]
                        and expansions[1].name
                    )

            end

            SetDropDownText(
                contextDropDown1,
                self.NewTaskRaidExpansion
                or UI:LT("NO EXPANSION")
            )

            UIDropDownMenu_Initialize(
                contextDropDown1,
                function()

                    for _, expansion in ipairs(
                        expansions
                    ) do

                        local info =
                            UIDropDownMenu_CreateInfo()

                        info.text =
                            expansion.name

                        info.func =
                            function()

                                self.NewTaskRaidExpansion =
                                    expansion.name

                                self.NewTaskInstanceID =
                                    nil

                                self.NewTaskInstanceName =
                                    nil

                                self:RefreshTaskContextSelectors()

                            end

                        UIDropDownMenu_AddButton(
                            info
                        )

                    end

                end
            )

            local raids =
                addon.GameData:GetRaidsForExpansion(
                    self.NewTaskRaidExpansion
                )

            local selected = nil

            for _, raid in ipairs(raids) do

                if tonumber(raid.id) ==
                    tonumber(
                        self.NewTaskInstanceID
                    )
                then

                    selected =
                        raid

                    break

                end

            end

            if not selected then

                selected =
                    raids[1]

                if selected then

                    self.NewTaskInstanceID =
                        selected.id

                    self.NewTaskInstanceName =
                        selected.name

                end

            end

            SetDropDownText(
                contextDropDown2,
                selected
                and addon.Localization:GetInstanceName("RAID", selected.id, selected.name)
                or UI:LT("NO RAID")
            )

            UIDropDownMenu_Initialize(
                contextDropDown2,
                function()

                    for _, raid in ipairs(
                        raids
                    ) do

                        local info =
                            UIDropDownMenu_CreateInfo()

                        info.text =
                            addon.Localization:GetInstanceName("RAID", raid.id, raid.name)

                        info.func =
                            function()

                                self.NewTaskInstanceID =
                                    raid.id

                                self.NewTaskInstanceName =
                                    raid.name

                                SetDropDownText(
                                    contextDropDown2,
                                    addon.Localization:GetInstanceName("RAID", raid.id, raid.name)
                                )

                            end

                        UIDropDownMenu_AddButton(
                            info
                        )

                    end

                end
            )

        end

        if category ~= "DUNGEON" then

            UIDropDownMenu_SetWidth(
                contextDropDown1,
                205
            )

        end

    end

    UIDropDownMenu_Initialize(
        categoryDropDown,
        function()

            for _, option in ipairs(
                addon.DB:GetCategoryOptions()
            ) do

                local info =
                    UIDropDownMenu_CreateInfo()

                info.text =
                    UI:LT(option.text)

                info.func =
                    function()

                        self.NewTaskCategory =
                            option.value

                        self:ResetNewTaskContext()

                        SetDropDownText(
                            categoryDropDown,
                            UI:LT(option.text)
                        )

                        self:RefreshTaskContextSelectors()

                    end

                UIDropDownMenu_AddButton(
                    info
                )

            end

        end
    )

    --------------------------------------------------------
    -- DATE / TIME BEHAVIOR
    --------------------------------------------------------

    self.NewTaskHasDate = true
    self.NewTaskHasTime = false

    local function UpdateTimeControls()

        local enabled =
            self.NewTaskHasTime == true

        hourInput:SetEnabled(enabled)
        minuteInput:SetEnabled(enabled)

        if enabled then
            timeText:SetTextColor(
                unpack(self.COLORS.text)
            )
        else
            timeText:SetTextColor(
                unpack(self.COLORS.muted)
            )
        end

    end

    self.UpdateTimeControls =
        UpdateTimeControls

    dateButton:SetScript(
        "OnClick",
        function()

            self.NewTaskHasDate =
                true

            self.SelectedDate =
                self.SelectedDate
                or self:GetToday()

            dateButton:SetText(
                self:FormatDate(
                    self.SelectedDate
                )
            )

        end
    )

    noDateButton:SetScript(
        "OnClick",
        function()

            self.NewTaskHasDate =
                false

            self.NewTaskHasTime =
                false

            timeCheck:SetChecked(
                false
            )

            UpdateTimeControls()

            dateButton:SetText(UI:LT("NO DATE"))

        end
    )

    timeCheck:SetScript(
        "OnClick",
        function(button)

            if not self.NewTaskHasDate then

                self.NewTaskHasTime =
                    false

                button:SetChecked(false)

            else

                self.NewTaskHasTime =
                    button:GetChecked()
                    == true

            end

            UpdateTimeControls()

        end
    )

    timeTextButton:SetScript(
        "OnClick",
        function()

            if not self.NewTaskHasDate then

                self.NewTaskHasTime =
                    false

                timeCheck:SetChecked(
                    false
                )

            else

                self.NewTaskHasTime =
                    not self.NewTaskHasTime

                timeCheck:SetChecked(
                    self.NewTaskHasTime
                )

            end

            UpdateTimeControls()

        end
    )

    UpdateTimeControls()
    self:RefreshTaskContextSelectors()

end

------------------------------------------------------------
-- REFRESH DYNAMIC SELECTORS
------------------------------------------------------------

function UI:RefreshDynamicSelectors()

    if self.RefreshTaskContextSelectors then
        self:RefreshTaskContextSelectors()
    end

    if self.RefreshFilterSelectors then
        self:RefreshFilterSelectors()
    end

end

------------------------------------------------------------
-- CREATE TASK
------------------------------------------------------------

function UI:CreateQuickTask()

    if self.EditingTaskID
        and self.SaveTaskEdits
    then
        self:SaveTaskEdits()
        return
    end

    local title =
        strtrim(
            self.TaskInput:GetText()
            or ""
        )

    if title == "" then
        return
    end

    local dueDate = nil

    if self.NewTaskHasDate
        and self.SelectedDate
    then

        dueDate = {
            year =
                self.SelectedDate.year,

            month =
                self.SelectedDate.month,

            day =
                self.SelectedDate.day,
        }

    end

    local hasTime =
        self.NewTaskHasTime
        and dueDate ~= nil

    local hour =
        tonumber(
            self.HourInput:GetText()
        )
        or 0

    local minute =
        tonumber(
            self.MinuteInput:GetText()
        )
        or 0

    hour =
        math.max(
            0,
            math.min(
                23,
                hour
            )
        )

    minute =
        math.max(
            0,
            math.min(
                59,
                minute
            )
        )

    local repeatType =
        self.RepeatType
        or "none"

    local interval =
        tonumber(
            self.IntervalInput:GetText()
        )
        or 1

    local repeatUnit =
        self.RepeatUnit
        or "days"

    repeatType, interval, repeatUnit =
        addon.DB:NormalizeRepeat(
            repeatType,
            interval,
            repeatUnit
        )

    addon.DB:CreateTask({
        title = title,
        dueDate = dueDate,

        hasTime = hasTime,

        dueTime =
            hasTime
            and {
                hour = hour,
                minute = minute,
            }
            or nil,

        repeatType = repeatType,
        repeatInterval = interval,
        repeatUnit = repeatUnit,

        category =
            self.NewTaskCategory
            or addon.DB.CATEGORIES.OTHER,

        characterKey =
            self.NewTaskCharacterKey,

        specID =
            self.NewTaskSpecID,

        specName =
            self.NewTaskSpecName,

        professionID =
            self.NewTaskProfessionID,

        professionName =
            self.NewTaskProfessionName,

        instanceID =
            self.NewTaskInstanceID,

        instanceName =
            self.NewTaskInstanceName,

        dungeonMode =
            self.NewTaskCategory == "DUNGEON"
            and "KEYS"
            or nil,

        raidExpansionName =
            self.NewTaskCategory == "RAID"
            and self.NewTaskRaidExpansion
            or nil,
    })

    --------------------------------------------------------
    -- RESET FAST INPUT
    --------------------------------------------------------

    self.TaskInput:SetText("")

    self.RepeatType = "none"
    self.RepeatInterval = 1
    self.RepeatUnit = "days"

    SetDropDownText(
        self.RepeatDropDown,
        UI:LT("NONE")
    )

    self.CustomRepeatPanel:Hide()

    self.NewTaskHasTime =
        false

    self.TimeCheck:SetChecked(
        false
    )

    if self.UpdateTimeControls then
        self.UpdateTimeControls()
    end

    -- Keep category/context selected.
    -- This is intentional for fast entry of several
    -- tasks of the same type.

    self:Refresh()

end
