local addonName, addon = ...

local UI = addon.UI

local TIMER_HEIGHT = 44

function UI:InitializeTimers()
    local sidebar = self.Sidebar

    local timersPanel = CreateFrame(
        "Frame",
        nil,
        sidebar,
        "BackdropTemplate"
    )

    self.TimersPanel = timersPanel

    timersPanel:SetPoint(
        "TOPLEFT",
        self.CalendarPanel,
        "BOTTOMLEFT",
        0,
        -6
    )

    timersPanel:SetPoint(
        "BOTTOMRIGHT",
        sidebar,
        "BOTTOMRIGHT",
        -8,
        8
    )

    timersPanel:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        edgeSize = 8,
    })

    timersPanel:SetBackdropColor(
        0.04,
        0.04,
        0.05,
        0.8
    )

    timersPanel:SetBackdropBorderColor(
        unpack(self.COLORS.border)
    )

    local title = timersPanel:CreateFontString(
        nil,
        "OVERLAY"
    )

    title:SetFontObject(GameFontNormal)
    title:SetPoint(
        "TOPLEFT",
        timersPanel,
        "TOPLEFT",
        8,
        -7
    )
    title:SetText(self:T("TIMERS", "TIMERS"))
    title:SetTextColor(unpack(self.COLORS.gold))

    local scrollFrame = CreateFrame(
        "ScrollFrame",
        nil,
        timersPanel,
        "UIPanelScrollFrameTemplate"
    )

    self.TimersScrollFrame = scrollFrame

    scrollFrame:SetPoint(
        "TOPLEFT",
        timersPanel,
        "TOPLEFT",
        8,
        -27
    )

    scrollFrame:SetPoint(
        "BOTTOMRIGHT",
        timersPanel,
        "BOTTOMRIGHT",
        -25,
        8
    )

    local content = CreateFrame(
        "Frame",
        nil,
        scrollFrame
    )

    self.TimersContent = content

    content:SetWidth(245)
    content:SetHeight(50)

    scrollFrame:SetScrollChild(content)

    self.TimerRows = {}
end

function UI:CreateTimerRow(task, y)
    local row = CreateFrame(
        "Frame",
        nil,
        self.TimersContent
    )

    row:SetSize(245, TIMER_HEIGHT)
    row:SetPoint(
        "TOPLEFT",
        self.TimersContent,
        "TOPLEFT",
        0,
        y
    )

    local timeText = row:CreateFontString(nil, "OVERLAY")
    timeText:SetFontObject(GameFontNormal)
    timeText:SetPoint(
        "TOPLEFT",
        row,
        "TOPLEFT",
        0,
        0
    )
    timeText:SetWidth(42)
    timeText:SetJustifyH("LEFT")
    timeText:SetText(self:GetTaskTimeText(task))
    timeText:SetTextColor(unpack(self.COLORS.gold))

    local titleText = row:CreateFontString(nil, "OVERLAY")
    titleText:SetFontObject(GameFontNormalSmall)
    titleText:SetPoint(
        "TOPLEFT",
        row,
        "TOPLEFT",
        45,
        0
    )
    titleText:SetPoint(
        "RIGHT",
        row,
        "RIGHT",
        -4,
        0
    )
    titleText:SetHeight(17)
    titleText:SetJustifyH("LEFT")
    titleText:SetJustifyV("MIDDLE")
    titleText:SetWordWrap(false)
    titleText:SetText(tostring(task.title or ""))

    local remainingText = row:CreateFontString(nil, "OVERLAY")
    remainingText:SetFontObject(GameFontNormalSmall)
    remainingText:SetPoint(
        "TOPLEFT",
        row,
        "TOPLEFT",
        45,
        -20
    )
    remainingText:SetPoint(
        "RIGHT",
        row,
        "RIGHT",
        -4,
        0
    )
    remainingText:SetHeight(16)
    remainingText:SetJustifyH("LEFT")
    remainingText:SetJustifyV("MIDDLE")
    remainingText:SetWordWrap(false)

    return {
        row = row,
        timeText = timeText,
        titleText = titleText,
        remainingText = remainingText,
        task = task,
    }
end

function UI:RefreshTimers()
    if not self.TimersContent then
        return
    end

    self:ClearFrame(self.TimersContent)
    self.TimerRows = {}

    local tasks = addon.DB:GetUpcomingTimedTasks(10)

    local y = 0

    for _, task in ipairs(tasks) do
        local timer = self:CreateTimerRow(task, y)

        timer.remainingText:SetText(
            self:FormatRemaining(
                addon.DB:GetRemainingSeconds(task)
            )
        )

        timer.remainingText:SetTextColor(
            unpack(self.COLORS.muted)
        )

        table.insert(self.TimerRows, timer)

        y = y - TIMER_HEIGHT
    end

    if #tasks == 0 then
        local empty = self.TimersContent:CreateFontString(
            nil,
            "OVERLAY"
        )

        empty:SetFontObject(GameFontNormalSmall)
        empty:SetPoint(
            "TOPLEFT",
            self.TimersContent,
            "TOPLEFT",
            0,
            0
        )
        empty:SetWidth(225)
        empty:SetWordWrap(false)
        empty:SetText(self:T("NO_UPCOMING_TIMED_TASKS", "NO UPCOMING TIMED TASKS"))
        empty:SetTextColor(unpack(self.COLORS.muted))

        y = -25
    end

    self.TimersContent:SetHeight(
        math.max(math.abs(y) + 10, 50)
    )
end

function UI:UpdateTimers()
    if not self.TimerRows then
        return
    end

    for _, timer in ipairs(self.TimerRows) do
        local task = addon.DB:FindTask(
            timer.task.id
        )

        if task and not task.completed then
            timer.timeText:SetText(
                self:GetTaskTimeText(task)
            )

            timer.remainingText:SetText(
                self:FormatRemaining(
                    addon.DB:GetRemainingSeconds(task)
                )
            )
        end
    end
end
