--[[
	Addon: Taos Group Tools
	Author: TProg Taonnor
	Created by @Taonnor
]]--

--[[
	Local variables
]]--
local LOG_ACTIVE = false
local _logger = nil

--[[
	Table TGT_CommandsHandler
]]--
TGT_CommandsHandler = {}
TGT_CommandsHandler.__index = TGT_CommandsHandler

--[[
	Table Members
]]--
TGT_CommandsHandler.Name = "TGT-CommandsHandler"

--[[
	Called on /tgt command
]]--
function TGT_CommandsHandler.GetAllCommands()
	if (LOG_ACTIVE) then
        _logger:logTrace("TGT_CommandsHandler.GetAllCommands")
    end

    d("Commands active:")
    d("/tgt - Gets all available commands")
    d("/setgroupultimatestyle <STYLEID> - Sets the style (0 = SimpleList, 1 = SwimlaneList).")
    d("/setultimatesound <SOUND> - Sets SOUND value, see https://wiki.esoui.com/Sounds")
    d("/setultimateid <GROUPNAME> - Sets the static ultimate group; See /getultimategroups to get group names.")
    d("/setswimlaneid <SWIMLANE> <GROUPNAME> - Sets the ultimate group of swimlane (1-6); See /getultimategroups to get group name.")
    d("/getultimategroups - Gets all ultimate group names")
end

--[[
	Called on /setgroupultimatestyle command
]]--
function TGT_CommandsHandler.SetGroupUltimateStyleCommand(style)
	if (LOG_ACTIVE) then
        _logger:logTrace("TGT_CommandsHandler.SetGroupUltimateStyleCommand")
        _logger:logDebug("style", style)
    end

    if (style ~= nil and style ~= "") then
        TGT_SettingsHandler.SetStyleSettings(style)
    else
        d("Invalid style: " .. tostring(style))
    end
end

--[[
	Called on /setultimatesound command
]]--
function TGT_CommandsHandler.SetUltimateSoundCommand(sound)
	if (LOG_ACTIVE) then
        _logger:logTrace("TGT_CommandsHandler.SetUltimateSoundCommand")
        _logger:logDebug("sound", sound)
    end

    if (sound ~= "") then
        PlaySound(SOUNDS[sound])
        TGT_SettingsHandler.SetSoundSettings(99, sound)
    else
        d("Invalid sound: " .. tostring(sound))
    end
end

--[[
	Called on /setultimateid command
]]--
function TGT_CommandsHandler.SetUltimateIdCommand(groupName)
	if (LOG_ACTIVE) then
        _logger:logTrace("TGT_CommandsHandler.SetUltimateId")
        _logger:logDebug("groupName", groupName)
    end

    if (groupName ~= nil and groupName ~= "") then
        local ultimateGroup = TGT_UltimateGroupHandler.GetUltimateGroupByGroupName(groupName)

        if (ultimateGroup ~= nil) then
            TGT_SettingsHandler.SetStaticUltimateIDSettings(ultimateGroup.GroupAbilityId)
        else
            d("Invalid group name: " .. tostring(groupName))
        end
    else
        d("Invalid group name: " .. tostring(groupName))
    end
end

--[[
	Called on /setswimlaneid command
]]--
function TGT_CommandsHandler.SetSwimlaneIdCommand(option)
	if (LOG_ACTIVE) then
        _logger:logTrace("TGT_CommandsHandler.SetSwimlaneId")
        _logger:logDebug("option", option)
    end

    -- Parse options
    local options = {}
    local arrayLength = 0
    local searchResult = { string.match(option,"^(%S*)%s*(.-)$") }
    for i, v in pairs(searchResult) do
        if (v ~= nil and v ~= "") then
            options[i] = string.lower(v)
            arrayLength = i
        end
    end

    if (arrayLength == 2) then
        local swimlane = tonumber(options[1])
        local swimlaneGroup = options[2]
        local ultimateGroup = TGT_UltimateGroupHandler.GetUltimateGroupByGroupName(swimlaneGroup)

        if (swimlane ~= nil and ultimateGroup ~= nil and swimlane >= 1 and swimlane <= 6) then
            TGT_SettingsHandler.SetSwimlaneUltimateGroupIdSettings(swimlane, ultimateGroup)
        else
            d("Invalid options: " .. tostring(option))
        end
    else
        d("Invalid options: " .. tostring(option))
    end
end

--[[
	Called on /getultimategroups command
]]--
function TGT_CommandsHandler.GetUltimateGroupsCommand()
    if (LOG_ACTIVE) then _logger:logTrace("TGT_CommandsHandler.GetUltimateGroupsCommand") end

    local ultimateGroups = TGT_UltimateGroupHandler.GetUltimateGroups()

    d("Ultimate Groups:")

    for i, group in pairs(ultimateGroups) do
        d(group.GroupName .. " - " .. group.GroupDescription)
    end
end

--[[
	Initialize initializes TGT_CommandsHandler
]]--
function TGT_CommandsHandler.Initialize(logger)
    if (LOG_ACTIVE) then
        logger:logTrace("TGT_CommandsHandler.Initialize")
        TGT_CommandsHandler.GetAllCommands()
    end

    _logger = logger

    -- Define commands
    SLASH_COMMANDS["/tgt"] = TGT_CommandsHandler.GetAllCommands
    SLASH_COMMANDS["/setgroupultimatestyle"] = TGT_CommandsHandler.SetGroupUltimateStyleCommand
    SLASH_COMMANDS["/setultimatesound"] = TGT_CommandsHandler.SetUltimateSoundCommand
    SLASH_COMMANDS["/setultimateid"] = TGT_CommandsHandler.SetUltimateIdCommand
    SLASH_COMMANDS["/setswimlaneid"] = TGT_CommandsHandler.SetSwimlaneIdCommand
    SLASH_COMMANDS["/getultimategroups"] = TGT_CommandsHandler.GetUltimateGroupsCommand
end