--UNIT_AURA
function()
    if UnitExists("target") then
        local level = UnitLevel("target")
    else
        level = 110
    end
    local pDR = GetCombatRatingBonus(CR_VERSATILITY_DAMAGE_TAKEN) / 100
    local mDR = pDR
    local armor = select(2, UnitArmor("player"))
    local armorDR = armor / (armor + (aura_env.constants[level]))
    pDR = aura_env.stackDef(pDR, armorDR)
    for k, v in ipairs(aura_env.buffs) do
        if UnitBuff("player", v) then
            --print(UnitBuff("player", v))
            if v == "Shield of the Righteous" or v == "Ardent Defender" then
                --print("YEET")
                local mitigation = abs(select(17, UnitBuff("player", v))) / 100
                pDR = aura_env.stackDef(pDR, mitigation)
                mDR = aura_env.stackDef(mDR, mitigation)
            end
            if v == "Stoneform" then
                --print("YEET")
                local mitigation = abs(select(17, UnitBuff("player", v))) / 100
                pDR = aura_env.stackDef(pDR, mitigation)
            end
            if v == "Guardian of Ancient Kings" then
                --print("YEET")
                local mitigation = abs(select(19, UnitBuff("player", v))) / 100
                pDR = aura_env.stackDef(pDR, mitigation)
                mDR = aura_env.stackDef(mDR, mitigation)
            end
        end
        if UnitDebuff("target", v) then
            if v == "Eye of Tyr" then
                local mitigation = abs(select(17, UnitDebuff("target", v))) / 100
                pDR = aura_env.stackDef(pDR, mitigation)
                mDR = aura_env.stackDef(mDR, mitigation)
            end
        end
    end
    aura_env.DR.p = pDR * 100
    aura_env.DR.m = mDR * 100
    local HP = UnitHealth("player")
    for k, v in pairs(aura_env.absorbs) do
        if UnitBuff("player", v) then
            local absorb = select(17, UnitBuff("player", v))
            HP = HP + absorb
        end
    end
    aura_env.pHP = HP / (1 - pDR)
    aura_env.mHP = HP / (1 - mDR)
    return true
end

--DISPLAY
function()
    local output = "P: " .. aura_env.round(aura_env.DR.p, 2) .. "% "
    if aura_env.pHP < aura_env.thresholds.p[1] then output = output .. "\124c" .. aura_env.pColor1 .. aura_env.shorten(aura_env.pHP) .. "\124r" end
    if aura_env.pHP > aura_env.thresholds.p[1] and aura_env.pHP < aura_env.thresholds.p[2] then output = output .. "\124c" .. aura_env.pColor2 .. aura_env.shorten(aura_env.pHP) .. "\124r" end
    if aura_env.pHP > aura_env.thresholds.p[2] and aura_env.pHP < aura_env.thresholds.p[3] then output = output .. "\124c" .. aura_env.pColor3 .. aura_env.shorten(aura_env.pHP) .. "\124r" end
    if aura_env.pHP > aura_env.thresholds.p[3] and aura_env.pHP < aura_env.thresholds.p[4] then output = output .. "\124c" .. aura_env.pColor4 .. aura_env.shorten(aura_env.pHP) .. "\124r" end
    if aura_env.pHP > aura_env.thresholds.p[4] and aura_env.pHP < aura_env.thresholds.p[5] then output = output .. "\124c" .. aura_env.pColor5 .. aura_env.shorten(aura_env.pHP) .. "\124r" end
    if aura_env.pHP > aura_env.thresholds.p[5] then output = output .. "\124c" .. aura_env.pColor6 .. aura_env.shorten(aura_env.pHP) .. "\124r" end
    output = output .. "\n"
    output = output .. "M: " .. aura_env.round(aura_env.DR.m, 2) .. "% "
    if aura_env.mHP < aura_env.thresholds.m[1] then output = output .. "\124c" .. aura_env.pColor1 .. aura_env.shorten(aura_env.mHP) .. "\124r" end
    if aura_env.mHP > aura_env.thresholds.m[1] and aura_env.mHP < aura_env.thresholds.m[2] then output = output .. "\124c" .. aura_env.pColor2 .. aura_env.shorten(aura_env.mHP) .. "\124r" end
    if aura_env.mHP > aura_env.thresholds.m[2] and aura_env.mHP < aura_env.thresholds.m[3] then output = output .. "\124c" .. aura_env.pColor3 .. aura_env.shorten(aura_env.mHP) .. "\124r" end
    if aura_env.mHP > aura_env.thresholds.m[3] and aura_env.mHP < aura_env.thresholds.m[4] then output = output .. "\124c" .. aura_env.pColor4 .. aura_env.shorten(aura_env.mHP) .. "\124r" end
    if aura_env.mHP > aura_env.thresholds.m[4] and aura_env.mHP < aura_env.thresholds.m[5] then output = output .. "\124c" .. aura_env.pColor5 .. aura_env.shorten(aura_env.mHP) .. "\124r" end
    if aura_env.mHP > aura_env.thresholds.m[5] then output = output .. "\124c" .. aura_env.pColor6 .. aura_env.shorten(aura_env.mHP) .. "\124r" end
    return output
end

--INIT
aura_env.mHP = 0
aura_env.pHP = 0
aura_env.thresholds = {
    ["p"] = {
        40e6,
        60e6,
        80e6,
        100e6,
        140e6,
    },
    ["m"] = {
        40e6,
        50e6,
        60e6,
        70e6,
        80e6,
    },
}
aura_env.constants = {
    [110] = 7390,
    [111] = 7648,
    [112] = 7906,
    [113] = 8164,
}
aura_env.buffs = {
    "Shield of the Righteous",
    "Ardent Defender",
    "Guardian of Ancient Kings",
    "Eye of Tyr",
    "Stoneform",
}
aura_env.absorbs = {
    "Bulwark of Order",
    "Bulwark of Flame"
}
aura_env.stackDef = function(def1, def2)
    local x = 1 - def1
    local y = 1 - def2
    local var = x * y
    return 1 - var
end
aura_env.DR = {
    ["p"] = 0,
    ["m"] = 0
}
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
local Color1 = {255, 0, 0} -- The color if you have < 7 million effective health
local Color2 = {255, 128, 0} -- The color if you have >= 7 million effective health
local Color3 = {0, 255, 0} -- The color if you have >= 10 million effective health
local Color4 = {0, 255, 255} -- The color if you have >= 15 million effective health
local Color5 = {255, 0, 255} -- The color if you have >= 25 million effective health
local Color6 = {255, 255, 0} -- The color if you have >= 44 million effective health
local function RGBtoHex(rgb)
    local hexadecimal = 'FF'
    for key, value in pairs(rgb) do
        local hex = ''
        
        while(value > 0)do
            local index = math.fmod(value, 16) + 1
            value = math.floor(value / 16)
            hex = string.sub('0123456789ABCDEF', index, index) .. hex
        end
        
        if(string.len(hex) == 0)then
            hex = '00'
            
        elseif(string.len(hex) == 1)then
            hex = '0' .. hex
        end
        
        hexadecimal = hexadecimal .. hex
    end
    return hexadecimal
end
aura_env.pColor1 = RGBtoHex(Color1)
aura_env.pColor2 = RGBtoHex(Color2)
aura_env.pColor3 = RGBtoHex(Color3)
aura_env.pColor4 = RGBtoHex(Color4)
aura_env.pColor5 = RGBtoHex(Color5)
aura_env.pColor6 = RGBtoHex(Color6)
