--TSU
--UNIT_HEALTH NAME_PLATE_UNIT_ADDED NAME_PLATE_UNIT_REMOVED
function(allstates)
    for _,v in pairs(allstates) do
        v.show = false
        v.changed = true
    end

    for i = 1, 40 do
        local unit = "nameplate" .. i
        if UnitExists(unit) then
            if UnitIsPlayer(unit) then
                print("3", unit)
                if UnitIsEnemy("player", unit) then
                    print("4", unit)
                    local hp = UnitHealth(unit)
                    local maxHp = UnitHealthMax(unit)
                    local hppp = aura_env.round(hp / maxHp * 100, 2)
                    local shortHP = aura_env.shorten(hp)
                    local shortmaxHP = aura_env.shorten(maxHp)
                    local name = UnitName(unit)
                    local class = UnitClass(unit)
                    print(hp, maxHp, hppp, shortHP, shortmaxHP, name, class)
                    allstates[unit] = 
                    {
                        changed = true,
                        show = true,
                        resort = true,
                        progressType = "static",
                        value = hp,
                        total = maxHp,
                        index = hppp,
                        name = name,
                        hppp = hppp,
                        shortHP = shortHP,
                        shortmaxHP = shortmaxHP,
                        class = class,
                    }
                end
            end
        else
            break
        end
    end
    return true
end

--INIT
aura_env.round = function(var, n)
    if (n) then
        var = math.floor((var * 10^n) + 0.5) / (10^n)
    else
        var = math.floor(var+0.5)
    end
    return var
end
aura_env.shorten = function(val)
    local function round(var, n)
        if (n) then
            var = math.floor((var * 10^n) + 0.5) / (10^n)
        else
            var = math.floor(var+0.5)
        end
        return var
    end
    local n = 2
    if val < 1e3 then
        return round(val, n)
    elseif val > 1e3 and val < 1e6 then
        return round(val / 1e3, n) .. "k"
    elseif val > 1e6 and val < 1e9 then
        return round(val / 1e6, n) .. "M"
    elseif val > 1e9 then
        return round(val / 1e9, n) .. "G"
    end
end
aura_env.classColor = function(class)
    if class == "Death Knight" then return 0.77, 0.12, 0.23, 1 elseif
    class == "Demon Hunter" then return 0.64, 0.19, 0.79, 1 elseif
    class == "Druid" then return 1, 0.49, 0.04, 1 elseif
    class == "Hunter" then return 0.67, 0.83, 0.45, 1 elseif
    class == "Mage" then return 0.25, 0.78, 0.92, 1 elseif
    class == "Monk" then return 0, 1, 0.59, 1 elseif
    class == "Paladin" then return 0.96, 0.55, 0.73, 1 elseif
    class == "Priest" then return 1, 1, 1, 1  elseif
    class == "Rogue" then return 1, 0.96, 0.41, 1  elseif
    class == "Shaman" then return 0, 0.44, 0.87, 1 elseif
    class == "Warlock" then return 0.53, 0.53, 0.93, 1 elseif
    class == "Warrior" then return 0.78, 0.61, 0.43, 1 else
    return 1, 1, 1, 1 end
end
aura_env.round = function(var, n)
    if (n) then
        var = math.floor((var * 10^n) + 0.5) / (10^n)
    else
        var = math.floor(var+0.5)
    end
    return var
end
aura_env.grad = function(c)
    --c expected as [0, 1]
    if c > 0.5 then
        c = 1 - (2 * (c - 0.5))
        return c, 1, 0, 1
    else
        c = c * 2
        return 1, c, 0, 1
    end
end
aura_env.range = function(val, min, max, max2)
    val = 1 - (((max - val) / (max - min)) * max2)
    return val
end

--ANIMATION
function()
    if aura_env.statee then
        -- print(aura_env.state.class)
        return aura_env.classColor(aura_env.statee.class)
    end
end