--[[
Author: Jarth
Filename: MementoBar_Buttons.lua
]] --

-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local baseModule = MementoBar
local sharedBaseModule = JarthSharedBase

-------------------------------------------------------------------------------------------------
-- FUNCTIONS --
-- Button highlight --
-------------------------------------------------------------------------------------------------
function baseModule.ButtonOnClicked(button)
    local id = button:GetId()

    if id > 0 then
        local cooldownRemaining, cooldownDuration = GetCollectibleCooldownAndDuration(id)
        if cooldownRemaining == 0 then
            baseModule:Activate(id)
        end
    end
end

-------------------------------------------------------------------------------------------------
-- PRIVATE FUNCTIONS --
-------------------------------------------------------------------------------------------------

function baseModule:SetButtonFrameWidth(collection)
    local count, height, width = 0, 0, 0

    for key, _ in pairs(collection) do
        if IsCollectibleUnlocked(key) and collection[key] then
            count = count + 1
        end
    end

    if count > 0 then
        local depth = math.ceil(count / baseModule.Saved.BarDepth)
        height = baseModule.Saved.Height * (baseModule.Saved.Horizontal and depth or baseModule.Saved.BarDepth)
        width = baseModule.Saved.Width * (not baseModule.Saved.Horizontal and depth or baseModule.Saved.BarDepth)
    end

    sharedBaseModule:SetFrameSettings(baseModule.Frame, count == 0, height, width)
end

function baseModule:InitializeButtons(frame, orderedCollection)
    local index = 1
    local position = TOPLEFT

    for _, _value in ipairs(orderedCollection) do
        local id = _value.Id
        local left, top = sharedBaseModule:GetButtonPosition(baseModule, index)

        if baseModule.Saved.SelectedMementos[id] and IsCollectibleUnlocked(id) then
            if baseModule.Buttons[id] == nil then
                baseModule.Buttons[id] = baseModule.WM:CreateControlFromVirtual("MementoBar_Button", frame, "MementoBar_Button", id)
                baseModule.Buttons[id].Saved = baseModule.Saved
                baseModule.Buttons[id]:SetId(id)
            end

            sharedBaseModule:SetupButton(baseModule, id, left, top, position)
            sharedBaseModule:SetupButtonBinding(baseModule, id)
            sharedBaseModule:SetupButtonBackdrop(baseModule, id, left, top, position)
            sharedBaseModule:SetupButtonTexture(baseModule, id, left, top, baseModule.Mementos, position)
            baseModule.Buttons[id]:SetHandler("OnMouseEnter", sharedBaseModule.ButtonHighlightEnter)
            baseModule.Buttons[id]:SetHandler("OnMouseExit", sharedBaseModule.ButtonHighlightExit)
            index = index + 1
        elseif baseModule.Buttons[id] ~= nil then
            baseModule.Buttons[id]:SetHidden(true)
        end
    end
end

function baseModule:RestoreButtons(orderedCollection)
    local centerColor = baseModule.Saved.CenterColor
    local edgeColor = baseModule.Saved.EdgeColor
    local timerIsTimerEnabled = baseModule.Saved.IsTimerEnabled
    local timerFont = baseModule.Saved.TimerFont
    local timerFontColor = baseModule.Saved.TimerFontColor

    for _, _value in ipairs(orderedCollection) do
        if baseModule.Buttons[_value.Id] ~= nil then
            local backdrop = GetControl(baseModule.Buttons[_value.Id], "Backdrop")

            if backdrop ~= nil then
                backdrop:SetCenterColor(centerColor.r, centerColor.g, centerColor.b, centerColor.a)
                backdrop:SetEdgeColor(edgeColor.r, edgeColor.g, edgeColor.b, edgeColor.a)
            end

            local label = GetControl(baseModule.Buttons[_value.Id], "Timer")

            if label ~= nil then
                label:SetHidden(not timerIsTimerEnabled)
                label:SetFont(timerFont)
                label:SetColor(timerFontColor.r, timerFontColor.g, timerFontColor.b, timerFontColor.a)
            end
        end
    end
end

function baseModule:Activate(collectibleId)
    if collectibleId and IsCollectibleUsable(collectibleId) then
        baseModule.CountDown.StartTime = GetTimeStamp()
        baseModule.CountDown.CollectibleId = collectibleId
        EVENT_MANAGER:UnregisterForUpdate(baseModule.Addon.Name .. baseModule.CountDown.Event)
        UseCollectible(collectibleId)
        EVENT_MANAGER:RegisterForUpdate(baseModule.Addon.Name .. baseModule.CountDown.Event, baseModule.CountDown.Tick, baseModule.Update)
    end
end

function baseModule.Update()
    local cooldownRemaining, cooldownDuration = GetCollectibleCooldownAndDuration(baseModule.CountDown.CollectibleId)

    if cooldownRemaining > 0 then
        baseModule:UpdateLabel(cooldownRemaining)
    elseif (cooldownDuration > 0 and (GetTimeStamp() < (baseModule.CountDown.StartTime + math.floor(cooldownDuration / 1000)))) then
        baseModule:UpdateLabel(cooldownDuration)
    else
        baseModule:UpdateLabel("")
        EVENT_MANAGER:UnregisterForUpdate(baseModule.Addon.Name .. baseModule.CountDown.Event)
    end
end

function baseModule:UpdateLabel(remaining)
    if type(remaining) == "number" and remaining > 0 then
        remaining = string.format("%.1fs", (math.floor(remaining / 100) / 10))
    end

    for collectibleId in pairs(baseModule.Saved.SelectedMementos) do
        local buttonTimer = GetControl(baseModule.Buttons[collectibleId], "Timer")

        if buttonTimer and baseModule.Mementos[collectibleId] then
            buttonTimer:SetText(remaining)
        end
    end
end