local addonName, addon = ...

local UI = addon.UI

local CONTENT_WIDTH = 790

local function RowStep()
    return UI:Metric(60, 56, 90)
end

local function GroupTasks(tasks)

    local groups = {}
    local order = {}

    for _, task in ipairs(tasks) do

        local key =
            addon.DB:GetDateKey(
                task
            )

        if not groups[key] then

            groups[key] = {}

            table.insert(
                order,
                key
            )

        end

        table.insert(
            groups[key],
            task
        )

    end

    table.sort(
        order,
        function(a, b)

            if a == "NO_DATE" then
                return false
            end

            if b == "NO_DATE" then
                return true
            end

            return a < b

        end
    )

    return groups, order
end

------------------------------------------------------------
-- DAY VIEW
------------------------------------------------------------

function UI:BuildDayView()

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

    self.SelectedDate =
        selected

    local tasks =
        self:FilterTasks(
            addon.DB:GetTasksForDate(
                selected.year,
                selected.month,
                selected.day
            )
        )

    local y = 0

    y =
        self:CreateTaskGroup(
            self.Content,
            tasks,
            self:GetTaskDateText({
                dueDate = selected,
            }),
            y
        )

    local undated =
        self:FilterTasks(
            addon.DB:GetUndatedTasks()
        )

    if #undated > 0 then

        y =
            self:CreateTaskGroup(
                self.Content,
                undated,
                "NO DATE",
                y
            )

    end

    self.Content:SetSize(
        CONTENT_WIDTH,
        math.max(
            math.abs(y) + 30,
            100
        )
    )

end

------------------------------------------------------------
-- ALL VIEW
------------------------------------------------------------

function UI:BuildAllView()

    local tasks =
        self:FilterTasks(
            addon.DB:GetAllTasks()
        )

    local groups, order =
        GroupTasks(tasks)

    local y = 0

    for _, key in ipairs(order) do

        local group =
            groups[key]

        local headerText

        if key == "NO_DATE" then
            headerText = "NO DATE"
        else
            headerText =
                self:GetTaskDateText(
                    group[1]
                )
        end

        y =
            self:CreateTaskGroup(
                self.Content,
                group,
                headerText,
                y
            )

    end

    if #order == 0 then

        local empty =
            self.Content:CreateFontString(
                nil,
                "OVERLAY"
            )

        empty:SetFontObject(
            GameFontNormal
        )

        empty:SetPoint(
            "TOPLEFT",
            self.Content,
            "TOPLEFT",
            5,
            0
        )

        empty:SetText(
            self:T("NO_TASKS_FILTER", "NO TASKS FOR THIS FILTER")
        )

        empty:SetTextColor(
            unpack(self.COLORS.muted)
        )

        y = -30

    end

    self.Content:SetSize(
        CONTENT_WIDTH,
        math.max(
            math.abs(y) + 30,
            100
        )
    )

end

------------------------------------------------------------
-- REFRESH
------------------------------------------------------------

function UI:RefreshTaskList()

    self:ClearFrame(
        self.Content
    )

    if self.ViewMode == "all" then
        self:BuildAllView()
    else
        self:BuildDayView()
    end

end
