local addonName, addon = ...

local UI = addon.UI

local function CharacterName(
    characterKey
)

    if not characterKey
        or not addon.GameData
    then
        return nil
    end

    local character =
        addon.GameData:GetCharacter(
            characterKey
        )

    if not character then
        return characterKey
    end

    return character.name
        or characterKey
end

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

function UI:InitializeTaskList()
    self.DeletePopup = nil
end

------------------------------------------------------------
-- FILTER
------------------------------------------------------------

function UI:TaskPassesFilter(task)

    if not task then
        return false
    end

    local category =
        self.CategoryFilter
        or "ALL"

    if category ~= "ALL"
        and task.category ~= category
    then
        return false
    end

    if category == "CHARACTER" then

        if self.FilterCharacterKey ~= "ALL"
            and task.characterKey ~=
                self.FilterCharacterKey
        then
            return false
        end

        if self.FilterSpecID ~= "ALL"
            and tonumber(task.specID) ~=
                tonumber(
                    self.FilterSpecID
                )
        then
            return false
        end

    elseif category == "PROFESSION" then

        if self.FilterCharacterKey ~= "ALL"
            and task.characterKey ~=
                self.FilterCharacterKey
        then
            return false
        end

        if self.FilterProfessionID ~= "ALL"
            and tonumber(
                task.professionID
            ) ~= tonumber(
                self.FilterProfessionID
            )
        then
            return false
        end

    elseif category == "DUNGEON" then

        -- DUNGEON is currently used only for Mythic+ keys.
        if task.dungeonMode ~= "KEYS" then
            return false
        end

        if self.FilterInstanceID ~= "ALL"
            and tonumber(
                task.instanceID
            ) ~= tonumber(
                self.FilterInstanceID
            )
        then
            return false
        end

    elseif category == "RAID" then

        local taskExpansion =
            task.raidExpansionName
            or addon.GameData.currentTierName

        if self.FilterRaidExpansion ~= "ALL"
            and taskExpansion ~=
                self.FilterRaidExpansion
        then
            return false
        end

        if self.FilterInstanceID ~= "ALL"
            and tonumber(
                task.instanceID
            ) ~= tonumber(
                self.FilterInstanceID
            )
        then
            return false
        end

    end

    return true
end

function UI:FilterTasks(tasks)

    local result = {}

    for _, task in ipairs(
        tasks or {}
    ) do

        if self:TaskPassesFilter(
            task
        ) then

            table.insert(
                result,
                task
            )

        end

    end

    return result
end

------------------------------------------------------------
-- CONTEXT
------------------------------------------------------------

function UI:GetTaskContextText(task)

    if not task then
        return ""
    end

    if task.category == "CHARACTER" then

        local character =
            CharacterName(
                task.characterKey
            )

        if character
            and task.specName
        then

            return character
                .. " | "
                .. task.specName

        end

        return character
            or task.specName
            or ""

    elseif task.category ==
        "PROFESSION"
    then

        local character =
            CharacterName(
                task.characterKey
            )

        if character
            and task.professionName
        then

            return character
                .. " | "
                .. task.professionName

        end

        return task.professionName
            or character
            or ""

    elseif task.category ==
        "DUNGEON"
    then

        return task.instanceName
            or "KEY"

    elseif task.category ==
        "RAID"
    then

        local expansion =
            task.raidExpansionName
            or addon.GameData.currentTierName

        if expansion
            and task.instanceName
        then

            return expansion
                .. " | "
                .. task.instanceName

        end

        return task.instanceName
            or expansion
            or ""

    end

    return ""
end
