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

-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local sharedBaseModule = JarthSharedBase

-------------------------------------------------------------------------------------------------
-- FUNCTIONS --
-------------------------------------------------------------------------------------------------
function sharedBaseModule: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 sharedBaseModule:HotkeyUpdate(baseModule, newKeyId, newCollectibleId)
    baseModule.Global.ReverseKeyBindings = {}

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

function sharedBaseModule:HotkeyUpdateColor(baseModule, frame)
    local fontColor = baseModule.Saved.FontColor

    frame:SetColor(fontColor.r, fontColor.g, fontColor.b, fontColor.a)
end

function sharedBaseModule: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 sharedBaseModule: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

function sharedBaseModule:HotkeyGetKey(baseModule, keyBinding)
    local result = ""
    keyBinding = baseModule.Global.ReverseKeyBindings and baseModule.Global.ReverseKeyBindings[keyBinding] or keyBinding

    if keyBinding then
        local keyBindingTable = {GetHighestPriorityActionBindingInfoFromName(baseModule.Addon.Name .. "_" .. keyBinding)}

        for _, keyValue in pairs(keyBindingTable) do
            if keyValue > 0 then
                result = sharedBaseModule:HotkeyGetKeyName(result, keyValue)
            end
        end
    end
    return result
end