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

--[[
	Global callbacks
]]--
TGT_PLAYER_DATA_CHANGED = "TGT-PlayerDataChanged"

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

local REFRESHRATE = 2000 -- ms; RegisterForUpdate is in miliseconds

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

--[[
	Table Members
]]--
TGT_MapPingHandler.Name = "TGT-MapPingHandler"

--[[
	Called on new data from Communication
]]--
function TGT_MapPingHandler.OnData(pingTag, abilityPing, relativeUltimate)
    if (LOG_ACTIVE) then _logger:logTrace("TGT_MapPingHandler.OnData") end

    local ultimateGroup = TGT_UltimateGroupHandler.GetUltimateGroupByAbilityPing(abilityPing)

    if (ultimateGroup ~= nil and relativeUltimate ~= -1) then
        local player = {}
        local playerName = ""
        local isPlayerDead = false

        playerName = GetUnitName(pingTag)
        isPlayerDead = IsUnitDead(pingTag)

        player.PingTag = pingTag
        player.PlayerName = playerName
        player.IsPlayerDead = isPlayerDead
        player.UltimateGroup = ultimateGroup
        player.UltimateName = GetAbilityName(ultimateGroup.GroupAbilityId)
        player.UltimateIcon = GetAbilityIcon(ultimateGroup.GroupAbilityId)
        player.RelativeUltimate = relativeUltimate

        if (LOG_ACTIVE) then
            _logger:logDebug("player.PingTag", player.PingTag)
            _logger:logDebug("player.PlayerName", player.PlayerName)
            _logger:logDebug("player.IsPlayerDead", player.IsPlayerDead)
            _logger:logDebug("player.UltimateGroup.GroupName", player.UltimateGroup.GroupName)
            _logger:logDebug("player.RelativeUltimate", player.RelativeUltimate)
        end

        CALLBACK_MANAGER:FireCallbacks(TGT_PLAYER_DATA_CHANGED, player)
    else
        _logger:logError("TGT_MapPingHandler.OnMapPing, Ping invalid ultimateGroup: " .. tostring(ultimateGroup) .. "; relativeUltimate: " .. tostring(relativeUltimate))
    end
end

--[[
	Called on refresh of timer
]]--
function TGT_MapPingHandler.OnTimedUpdate(eventCode)
    if (LOG_ACTIVE) then _logger:logTrace("TGT_MapPingHandler.OnTimedUpdate") end

    -- only if player is in group
	if (IsUnitGrouped("player")) then
        local abilityGroup = TGT_UltimateGroupHandler.GetUltimateGroupByAbilityId(TGT_SettingsHandler.GetStaticUltimateIDSettings())

        if (abilityGroup ~= nil) then
	        TGT_Communicator.SendData(abilityGroup)
        else
            _logger:logError("TGT_MapPingHandler.OnTimedUpdate, abilityGroup is nil, change ultimate. StaticID: " .. tostring(TGT_SettingsHandler.GetStaticUltimateIDSettings()))
        end
    end
end

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

    _logger = logger

    -- Register callbacks
    CALLBACK_MANAGER:RegisterCallback(TGT_MAP_PING_CHANGED, TGT_MapPingHandler.OnData)

	-- Start timer
	EVENT_MANAGER:RegisterForUpdate(TGT_MapPingHandler.Name, REFRESHRATE, TGT_MapPingHandler.OnTimedUpdate)
end