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

-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local MementoBar = MementoBar

-------------------------------------------------------------------------------------------------
-- FUNCTIONS --
-- Button highlight --
-------------------------------------------------------------------------------------------------
function MementoBar.ButtonHighlightEnter(frame)
    local highlightColor = MementoBar.Saved.HighlightColor
    local buttonBackdrop = GetControl(frame, "Backdrop")
    if buttonBackdrop ~= nil then
        PlaySound(SOUNDS.QUICKSLOT_MOUSEOVER)
        buttonBackdrop:SetEdgeColor(highlightColor.r, highlightColor.g, highlightColor.b, highlightColor.a)
    end
end

function MementoBar.ButtonHighlightExit(frame)
    local edgeColor = MementoBar.Saved.EdgeColor
    local buttonBackdrop = GetControl(frame, "Backdrop")
    if buttonBackdrop ~= nil then
        buttonBackdrop:SetEdgeColor(edgeColor.r, edgeColor.g, edgeColor.b, edgeColor.a)
    end
end

function MementoBar.ButtonOnClicked(button)
    local id = button:GetId()
    if id > 0 then
        local cooldownRemaining, cooldownDuration = GetCollectibleCooldownAndDuration(id)
        if cooldownRemaining == 0 then
            MementoBar:Activate(id)
        end
    end
end

-------------------------------------------------------------------------------------------------
-- PRIVATE FUNCTIONS --
-------------------------------------------------------------------------------------------------
function MementoBar:SetupButton(button, left, top)
    button:ClearAnchors()
    button:SetAnchor(TOPLEFT, MementoBar_Frame, TOPLEFT, left, top)
    button:SetHeight(MementoBar.Saved.Height)
    button:SetWidth(MementoBar.Saved.Width)
    button:SetHidden(false)
end

function MementoBar:SetupButtonBinding(button, key, left, top)
    local buttonBinding = GetControl(button, "Binding")
    if buttonBinding ~= nil then
        MementoBar:HotkeyUpdateColor(buttonBinding)
        buttonBinding:SetText(MementoBar:HoykeyGetKey(button, key))
        buttonBinding:SetDrawLevel(3)
        buttonBinding:ClearAnchors()
        buttonBinding:SetAnchor(MementoBar.Saved.KeyBindingLocation, button, MementoBar.Saved.KeyBindingLocation, 0, 0)
    end
    if buttonBinding ~= nil then
        buttonBinding:SetHidden(not MementoBar.Saved.ShowKeyBinding)
    end
end

function MementoBar:SetupButtonTexture(button, key, left, top)
    local buttonTexture = GetControl(button, "Texture")
    if buttonTexture ~= nil then
        button:SetHandler("OnClicked", MementoBar.ButtonOnClicked)
        buttonTexture:SetDrawLevel(3)
        buttonTexture:SetTexture(MementoBar.Mementos[key].EnabledTexture)
        buttonTexture:ClearAnchors()
        buttonTexture:SetAnchor(TOPLEFT, button, TOPLEFT, MementoBar.Saved.Margin, MementoBar.Saved.Margin)
        buttonTexture:SetHeight(button:GetHeight() - (2 * MementoBar.Saved.Margin))
        buttonTexture:SetWidth(button:GetWidth() - (2 * MementoBar.Saved.Margin))
    end
end

function MementoBar:SetupButtonBackdrop(button, key, left, top)
    local buttonBackdrop = GetControl(button, "Backdrop")
    if buttonBackdrop ~= nil then
        buttonBackdrop:SetDrawLevel(2)
        buttonBackdrop:SetEdgeTexture(nil, 1, 1, MementoBar.Saved.Margin)
        buttonBackdrop:ClearAnchors()
        buttonBackdrop:SetAnchor(TOPLEFT, MementoBar_Frame, TOPLEFT, left, top)
        buttonBackdrop:SetHeight(button:GetHeight())
        buttonBackdrop:SetWidth(button:GetWidth())
    end
end

function MementoBar:SetButtonFrameWidth()
    local count, depth, height, width = 0, 0, 0, 0

    for key, value in pairs(MementoBar.Saved.SelectedMementos) do
        if IsCollectibleUnlocked(key) and MementoBar.Saved.SelectedMementos[key] then
            count = count + 1
        end
    end

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

    MementoBar:SetFrameSettings(MementoBar_Frame, count == 0, height, width)
    MementoBar:SetFrameSettings(MementoBar_FrameBackdrop, count == 0, height, width)
end

function MementoBar:SetFrameSettings(frame, isHidden, height, width)
    if frame ~= nil then
        frame:SetHidden(count == 0)
        frame:SetHeight(height)
        frame:SetWidth(width)
    end
end

function MementoBar:InitializeButtons()
    local index = 1
    for _, _value in ipairs(MementoBar.OrderedMementos) do
        local id = _value.Id
        local left, top = MementoBar:GetButtonPosition(index)
        if MementoBar.Saved.SelectedMementos[id] and IsCollectibleUnlocked(id) then
            if MementoBar.Buttons[id] == nil then
                MementoBar.Buttons[id] = WINDOW_MANAGER:CreateControlFromVirtual("MementoBar_Button", MementoBar_Frame, "MementoBar_Button", id)
                MementoBar.Buttons[id]:SetId(id)
            end
            MementoBar:SetupButton(MementoBar.Buttons[id], left, top)
            MementoBar:SetupButtonBinding(MementoBar.Buttons[id], id, left, top)
            MementoBar:SetupButtonBackdrop(MementoBar.Buttons[id], id, left, top)
            MementoBar:SetupButtonTexture(MementoBar.Buttons[id], id, left, top)
            MementoBar.Buttons[id]:SetHandler("OnMouseEnter", MementoBar.ButtonHighlightEnter)
            MementoBar.Buttons[id]:SetHandler("OnMouseExit", MementoBar.ButtonHighlightExit)
            index = index + 1
        elseif MementoBar.Buttons[id] ~= nil then
            MementoBar.Buttons[id]:SetHidden(true)
        end
    end
end

function MementoBar:GetButtonPosition(index)
    local left = MementoBar.Saved.Width * ((index - 1) % MementoBar.Saved.BarDepth)
    local top = MementoBar.Saved.Height * (math.floor((index - 1) / MementoBar.Saved.BarDepth))
    if MementoBar.Saved.Horizontal then
        left = MementoBar.Saved.Height * (math.floor((index - 1) / MementoBar.Saved.BarDepth))
        top = MementoBar.Saved.Width * ((index - 1) % MementoBar.Saved.BarDepth)
    end
    return left, top
end

function MementoBar:ButtonsBackdropColor()
    local centerColor = MementoBar.Saved.CenterColor
    local edgeColor = MementoBar.Saved.EdgeColor

    for _, _value in ipairs(MementoBar.OrderedMementos) do
        if MementoBar.Buttons[_value.Id] ~= nil then
            local buttonBackdrop = GetControl(MementoBar.Buttons[_value.Id], "Backdrop")
            if buttonBackdrop ~= nil then
                buttonBackdrop:SetCenterColor(centerColor.r, centerColor.g, centerColor.b, centerColor.a)
                buttonBackdrop:SetEdgeColor(edgeColor.r, edgeColor.g, edgeColor.b, edgeColor.a)
            end
        end
    end
end

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

function MementoBar.Update()
    local cooldownRemaining, cooldownDuration = GetCollectibleCooldownAndDuration(MementoBar.CountDown.CollectibleId)
    if cooldownRemaining > 0 then
        MementoBar:UpdateLabel(cooldownRemaining)
    elseif (cooldownDuration > 0 and (GetTimeStamp() < (MementoBar.CountDown.StartTime + math.floor(cooldownDuration / 1000)))) then
        MementoBar:UpdateLabel(cooldownDuration)
    else
        MementoBar:UpdateLabel("")
        EVENT_MANAGER:UnregisterForUpdate(MementoBar.Addon.Name .. MementoBar.CountDown.Event)
    end
end

function MementoBar:UpdateLabel(remaining)
    if type(remaining) == "number" and remaining > 0 then
        remaining = string.format("%.1fs", (math.floor(remaining / 100) / 10))
    end
    for collectibleId in pairs(MementoBar.Saved.SelectedMementos) do
        local buttonTimer = GetControl(MementoBar.Buttons[collectibleId], "Timer")
        if buttonTimer and MementoBar.Mementos[collectibleId] then
            buttonTimer:SetText(remaining)
        end
    end
end