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

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

-------------------------------------------------------------------------------------------------
-- PUBLIC FUNCTIONS --
-------------------------------------------------------------------------------------------------
function MementoBar_KeyBindingPressed(keyId)
    local control = MementoBar.WM:GetMouseOverControl()
    if control ~= nil then
        for collectibleId, _ in pairs(MementoBar.Saved.SelectedMementos) do
            if MementoBar.Buttons[collectibleId] == control then
                MementoBar:HotkeyUpdate(keyId, collectibleId)
                MementoBar:InitializeButtons()
                return
            end
        end
    end

    if MementoBar.Saved.KeyBindings[keyId] > 0 then
        MementoBar.ButtonOnClicked(MementoBar.Buttons[MementoBar.Saved.KeyBindings[keyId]])
    end
end

-------------------------------------------------------------------------------------------------
-- PRIVATE FUNCTIONS --
-------------------------------------------------------------------------------------------------
function MementoBar:HoykeyCreateSelections()
    for key, value in ipairs(MementoBar.Saved.KeyBindings) do
        ZO_CreateStringId("SI_BINDING_NAME_MementoBar_" .. key, "MementoBar_" .. key)
    end
end

function MementoBar:HotkeyUpdate(newKeyId, newCollectibleId)
    MementoBar.Global.ReverseKeyBindings = {}
    for keyId, collectibleId in ipairs(MementoBar.Saved.KeyBindings) do
        if newCollectibleId and newKeyId == keyId then
            MementoBar.Saved.KeyBindings[keyId] = newCollectibleId
            MementoBar.Global.ReverseKeyBindings[newCollectibleId] = keyId
        elseif newCollectibleId == collectibleId and newKeyId ~= keyId then
            MementoBar.Saved.KeyBindings[keyId] = 0
        elseif collectibleId > 0 and not MementoBar.Saved.SelectedMementos[collectibleId] then
            MementoBar.Saved.KeyBindings[keyId] = 0
        elseif collectibleId > 0 then
            MementoBar.Global.ReverseKeyBindings[collectibleId] = keyId
        end
    end
end

function MementoBar:HoykeyGetKey(button, collectibleId)
    local result = ""
    local keyId = MementoBar.Global.ReverseKeyBindings[collectibleId]
    if keyId then
        local keyBindingTable = {GetHighestPriorityActionBindingInfoFromName("MementoBar_" .. keyId)}
        for _, keyValue in pairs(keyBindingTable) do
            if keyValue > 0 then
                result = MementoBar:HotkeyGetKeyName(result, keyValue)
            end
        end
    end
    return result
end

function MementoBar:HotkeyGetKeyName(result, keyValue)
    if keyValue > 0 then
        if result ~= "" then
            result = string.format("%s+%s", result, GetKeyName(keyValue))
        else
            result = GetKeyName(keyValue)
        end
    end
    return result
end

function MementoBar:HotkeyUpdateColor(frame)
    local fontColor = MementoBar.Saved.FontColor
    frame:SetColor(fontColor.r, fontColor.g, fontColor.b, fontColor.a)
end

function MementoBar:HotKeyGetLocationValue(value)
    local result
    if value == "bottom" then
        result = BOTTOM
    elseif value == "bottomleft" then
        result = BOTTOMLEFT
    elseif value == "bottomright" then
        result = BOTTOMRIGHT
    elseif value == "center" then
        result = CENTER
    elseif value == "left" then
        result = LEFT
    elseif value == "right" then
        result = RIGHT
    elseif value == "top" then
        result = TOP
    elseif value == "topleft" then
        result = TOPLEFT
    elseif value == "topright" then
        result = TOPRIGHT
    end
    return result
end

function MementoBar:HotKeyGetLocationText(value)
    local result
    if value == BOTTOM then
        result = "bottom"
    elseif value == BOTTOMLEFT then
        result = "bottomleft"
    elseif value == BOTTOMRIGHT then
        result = "bottomright"
    elseif value == CENTER then
        result = "center"
    elseif value == LEFT then
        result = "left"
    elseif value == RIGHT then
        result = "right"
    elseif value == TOP then
        result = "top"
    elseif value == TOPLEFT then
        result = "topleft"
    elseif value == TOPRIGHT then
        result = "topright"
    end
    return result
end