--COMBAT_LOG_EVENT_UNFILTERED
function(...)
	local se = select(3, ...)
    if se == "SWING_DAMAGE" then
        local dest = select(10, ...)
        if dest == UnitName("player") then
        	local amount = select(13, ...)
        	local block = select(17, ...) or 0
        	local absorb = select(18, ...) or 0
        	if amount then
        		local output = amount
        		local total = amount
    			total = total + block + absorb
    			local rel = aura_env.round(((absorb + block) / total) * 100, 2) or 0
    			output = output .. " " .. block .. " " .. absorb .. " " .. rel
		        if #aura_env.melee == aura_env.lines then
		            table.remove(aura_env.melee, 1)
                    table.insert(aura_env.melee, output)
		        else
		        	table.insert(aura_env.melee, output)
		        end
		        return true
		    end
		end
    elseif se == "SPELL_DAMAGE" then
        local dest = select(10, ...)
        if dest == UnitName("player") then
            local amount = select(16, ...)
            local block = select(20, ...) or 0
            local absorb = select(21, ...) or 0
            if amount then
                local output = amount
                local total = amount
                total = total + block + absorb
                local rel = aura_env.round(((absorb + block) / total) * 100, 2) or 0
                output = output .. " " .. block .. " " .. absorb .. " " .. rel
                if #aura_env.melee == aura_env.lines then
                    table.remove(aura_env.melee, 1)
                    table.insert(aura_env.melee, output)
                else
                    table.insert(aura_env.melee, output)
                end
                return true
            end
        end
    end
end

--DISPLAY
function()
    local meleeT, blockT, absorbT, perT = "", "", "", ""
    local melee, block, absorb, per = 0, 0, 0, 0
    for i = #aura_env.melee, 1, -1 do
        melee = aura_env.melee[i]:match("(%d+)")
        if melee then
            melee = tonumber(melee)
            block = aura_env.melee[i]:match("%d+ (%d+)") or 0
            block = tonumber(block)
            absorb = aura_env.melee[i]:match("%d+ %d+ (%d+)") or 0
            absorb = tonumber(absorb)
            if block or absorb then
                per = aura_env.melee[i]:match("%d+ %d+ %d+ (%d+%.?%d*)") or 0
                per = tonumber(per)
            end
            meleeT = meleeT .. aura_env.shorten(melee) .. "\n"
            blockT = blockT .. aura_env.shorten(block) .. "\n"
            absorbT = absorbT .. aura_env.shorten(absorb) .. "\n"
            perT = perT .. aura_env.shorten(per) .. "\n"
        end
    end
    aura_env.region.melee:SetText(meleeT)
    aura_env.region.block:SetText(blockT)
    aura_env.region.absorb:SetText(absorbT)
    aura_env.region.percentage:SetText(perT)
end

--INIT
aura_env.lines = 10

----------------------------------------
-- PLEASE DO NOT EDIT BELOW THIS LINE --
----------------------------------------
local fontsize = select(2, aura_env.region.text:GetFont())
aura_env.region:SetWidth(fontsize * 20)
aura_env.region:SetHeight(fontsize * aura_env.lines)
aura_env.melee = {""}
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
if not aura_env.region.melee then
    local melee = aura_env.region:CreateFontString(nil, aura_env.region)
    aura_env.region.melee = melee
end
aura_env.region.melee:SetFont(aura_env.region.text:GetFont())
aura_env.region.melee:SetTextColor(1, 1, 1)
aura_env.region.melee:SetPoint("CENTER", aura_env.region, "CENTER", -fontsize * 7.5, 0)
aura_env.region.melee:SetWidth(fontsize * 5)
aura_env.region.melee:SetJustifyH("LEFT")
aura_env.region.melee:SetText("")
aura_env.region.melee:Show()

if not aura_env.region.block then
    local block = aura_env.region:CreateFontString(nil, aura_env.region)
    aura_env.region.block = block
end
aura_env.region.block:SetFont(aura_env.region.text:GetFont())
aura_env.region.block:SetTextColor(0.75, 0.23, 0.23)
aura_env.region.block:SetPoint("CENTER", aura_env.region, "CENTER", -fontsize * 2.5, 0)
aura_env.region.block:SetWidth(fontsize * 5)
aura_env.region.block:SetJustifyH("LEFT")
aura_env.region.block:SetText("")
aura_env.region.block:Show()

if not aura_env.region.absorb then
    local absorb = aura_env.region:CreateFontString(nil, aura_env.region)
    aura_env.region.absorb = absorb
end
aura_env.region.absorb:SetFont(aura_env.region.text:GetFont())
aura_env.region.absorb:SetTextColor(0.1227, 0.4089, 0.65)
aura_env.region.absorb:SetPoint("CENTER", aura_env.region, "CENTER", fontsize * 2.5, 0)
aura_env.region.absorb:SetWidth(fontsize * 5)
aura_env.region.absorb:SetJustifyH("LEFT")
aura_env.region.absorb:SetText("")
aura_env.region.absorb:Show()

if not aura_env.region.percentage then
    local percentage = aura_env.region:CreateFontString(nil, aura_env.region)
    aura_env.region.percentage = percentage
end
aura_env.region.percentage:SetFont(aura_env.region.text:GetFont())
aura_env.region.percentage:SetTextColor(0.32, 0.32, 0.34)
aura_env.region.percentage:SetPoint("CENTER", aura_env.region, "CENTER", fontsize * 7.5, 0)
aura_env.region.percentage:SetWidth(fontsize * 5)
aura_env.region.percentage:SetJustifyH("LEFT")
aura_env.region.percentage:SetText("")
aura_env.region.percentage:Show()

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