--------------------------------------------------------------------------------
-- File: AoUMeterUtil.lua
-- Desc: some useful functions, constants and enums
-- Auth: Resha
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- Functions
--------------------------------------------------------------------------------
-- Get minutes & seconds from an int
function GetMinSec (sec)
    local lmin = math.floor(sec / 60)
    return lmin, sec - lmin * 60
end
--------------------------------------------------------------------------------
-- Make a deep copy of an object (http://oentend.blogspot.com/2009/08/lua.html)
function DeepCopyObject( object )
    local lookup_table = {}
    
    local function _copy( object )
        if type( object ) ~= "table" then
            return object
        elseif lookup_table[object] then
            return lookup_table[object]
        end
        
        local new_table = {}
        lookup_table[object] = new_table
        for index, value in pairs( object ) do
            new_table[ _copy( index ) ] = _copy( value )
        end
        
        return setmetatable( new_table, _copy( getmetatable( object ) ) )
    end
    
    return _copy( object )
end
--------------------------------------------------------------------------------
-- Get pourcentage from ratio Value / ValueAt
function GetPercentageAt( Value, ValueAt )
    return math.floor( ( ValueAt ~= 0 and Value / ValueAt or 0 ) * 100 )
end
--------------------------------------------------------------------------------
-- Get information of a spell
-- Return SpellInfo, ID
function GetSpellInfoFromParams(params)
    if params then
        local info = params.spellId and avatar.GetSpellInfo ~= nil and avatar.GetSpellInfo(params.spellId) -- version < 5.0
        or params.spellId and spellLib.GetDescription(params.spellId) -- version >= 5.0
        or params.buffId and object.GetBuffInfo(params.buffId)
        or params.abilityId and avatar.GetAbilityInfo(params.abilityId)
        
        if info then
            if params.source then
                if unit.IsPlayer(params.source) or group.IsCreatureInGroup(params.source) then
                    info.name = not common.IsEmptyWString(info.name) and info.name or userMods.ToWString("?")
                elseif unit.IsPet(params.source) then
                    info.name = userMods.ToWString(StrPet .. " - " .. userMods.FromWString(params.ability))
                elseif unit.GetRace(params.source).creatureRace == 1 or unit.GetFollowerMaster(params.source) then
                    info.name = params.sourceName
                end
            elseif params.healerId then
                info.name = not common.IsEmptyWString(info.name) and info.name or userMods.ToWString("?")
            end
            
            info.id = params.spellId or params.buffId or params.abilityId
            info.debugName = userMods.FromWString(info.name)
            info.type = params.sysSubElement or "ENUM_SubElement_HOLY"
            info.identifier = info.debugName .. info.type
            
            return info
        end
    end
end
--------------------------------------------------------------------------------
function IsThisStringValue(value1, value2)
    if common.IsWString(value1) and common.IsWString(value2) then
        return common.CompareWString(value1, value2) == 0
    else
        return type(value1) == "string" and value1 == value2
    end
end
--------------------------------------------------------------------------------
-- Compare the name of 2 spells in order to sort them
--------------------------------------------------------------------------------
function CompareSpellDetails(A, B)
    local AA = tonumber(string.sub(A, 3))
    local BB = tonumber(string.sub(B, 3))
    return AA > BB
end
--------------------------------------------------------------------------------
-- Sort spell details by Count
--------------------------------------------------------------------------------
function SortSpellDetailsByCount(DetailsList)
    local a = {}
    local Type, DamageDetails
    
    for Type, DamageDetails in pairs(DetailsList) do
        table.insert(a, Type.."_"..DamageDetails.Count)
    end
    
    table.sort(a, CompareSpellDetails)
    
    local i = 0					-- iterator variable
    local iter = function ()	-- iterator function
        i = i + 1
        if a[i] == nil then return nil
        else return tonumber(string.sub(a[i], 1, 1))
        end
    end
    return iter
end
--------------------------------------------------------------------------------
-- Sort spell details by Count
--------------------------------------------------------------------------------
function SortSpellDetailsByAmount(DetailsList)
    local a = {}
    local Type, DamageDetails
    
    for Type, DamageDetails in pairs(DetailsList) do
        table.insert(a, Type.."_"..DamageDetails.Amount)
    end
    
    table.sort(a, CompareSpellDetails)
    
    local i = 0					-- iterator variable
    local iter = function ()	-- iterator function
        i = i + 1
        if a[i] == nil then return nil
        else return tonumber(string.sub(a[i], 1, 1))
        end
    end
    return iter
end