--CLEU PLAYER_TARGET_CHANGED PLAYER_FOCUS_CHANGED
function(e, ...)
	if aura_env.config.tsuenable == false then
		local se = select(2, ...)
		if se == "SPELL_AURA_APPLIED_DOSE" or e == "PLAYER_TARGET_CHANGED" then
			aura_env.owndebuff = {["stacks"] = 0, ["damage"] = 0,}
			aura_env.totaldebuff = {["stacks"] = 0, ["damage"] = 0,}
			aura_env.damagepercentage = 0
			local unit = "target"
			if aura_env.config.tar == 2 then unit = "focus" elseif aura_env.config.tar == 3 then unit = "Boss1" end
			local bossmax = UnitHealthMax(unit)
			if aura_env.config.pdmg == true then
				for i = 1, 200 do
					local name = UnitDebuff(unit, i)
					if name == "Conductive Ink" then
						local caster = select(7, UnitDebuff(unit, i))
						local stacks = select(3, UnitDebuff(unit, i))
						aura_env.totaldebuff.stacks = aura_env.totaldebuff.stacks + stacks
						aura_env.myTooltip:SetUnitDebuff(unit, i)
						local text =  _G["MyAddOnTooltipTextLeft2"]:GetText()
						if text:match("Falling below %d*%% health will cause") then
							local damage = text:match("inflict (%d+%,?%d*)")
							damage = damage:gsub("%,", "")
							damage = damage:gsub(" ", "")
							damage = tonumber(damage)
							aura_env.totaldebuff.damage = aura_env.totaldebuff.damage + damage
							if caster == "player" then aura_env.owndebuff.damage = damage; aura_env.owndebuff.stacks = stacks end
						end
					elseif not name then break end
				end
			else
				for i = 1, 200 do
					local name = UnitDebuff(unit, i)
					if name == "Conductive Ink" then
						aura_env.totaldebuff.stacks = aura_env.totaldebuff.stacks + select(3, UnitDebuff(unit, i))
						aura_env.myTooltip:SetUnitDebuff(unit, i)
						local text =  _G["MyAddOnTooltipTextLeft2"]:GetText()
						if text:match("Falling below %d*%% health will cause") then
							aura_env.totaldebuff.damage = text:match("inflict (%d+%,?%d*)")
							aura_env.totaldebuff.damage = aura_env.totaldebuff.damage:gsub("%,", "")
							aura_env.totaldebuff.damage = aura_env.totaldebuff.damage:gsub(" ", "")
							aura_env.totaldebuff.damage = tonumber(aura_env.totaldebuff.damage)
						end
						break
					elseif not name then break end
				end
			end
			aura_env.damagepercentage = aura_env.round(((aura_env.totaldebuff.damage / bossmax) * 100), 2)
			return true
		end
	end
end

--TSU
--CLEU PLAYER_TARGET_CHANGED PLAYER_FOCUS_CHANGED
function(allstates, e, ...)
    if aura_env.config.tsuenable == true then
        if InCombatLockdown() == true then
            --Base setup
            local unit = "target"
            if not aura_env.data then aura_env.data = {} end
            local crit = false
            local function update_allstates(data, unit)
                local maxdmg, maxstack = 0, 0
                local totaldmg, totalstack = 0, 0
                --Get max and total damage
                for k,v in pairs(data) do
                    if v.d > maxdmg then
                        maxdmg = v.d
                    end
                    if v.s > maxstack then
                        maxstack = v.s
                    end
                    totaldmg = totaldmg + v.d
                    totalstack = totalstack + v.s
                end
                --Get damage in boss %
                local perc = 0
                local pp = 0
                local islow = false
                if UnitExists(unit) then
                    local hp, maxhp = UnitHealth(unit), UnitHealthMax(unit)
                    pp = hp/maxhp
                    if pp < 0.3 then islow = true end
                    perc = aura_env.round(((totaldmg / maxhp) * 100), 2)
                end
                --Store all members in allstates
                for k,v in pairs(data) do
                    allstates[k] = {
                        show = true,
                        changed = true,
                        resort = true,
                        progressType = "static",
                        caster = k,
                        class = v.c,
                        value = v.d,
                        total = maxdmg,
                        index = v.d,
                        stacks = v.s,
                        crit = v.cr,
                    }
                end
                --Total bar control
                if UnitExists(unit) then
                    allstates[1] = {
                        show = true,
                        changed = true,
                        resort = true,
                        progressType = "static",
                        caster = "Total",
                        class = 5, --priest; White
                        value = totaldmg,
                        total = totaldmg,
                        index = totaldmg + 1,
                        stacks = totalstack,
                        percent = perc,
                    }
                    if islow == true then
                        allstates[1].class = 15
                    end
                end
            end
            --Debug and options
            local command = UnitDebuff; if aura_env.config.debug == true then command = aura_env.UnitDebuffC end
            if aura_env.config.tar == 2 then unit = "focus" elseif aura_env.config.tar == 3 then unit = "Boss1" end
            --Trying to store info of debuff into data table from <unit>, looking for damage, stacks and caster, also included debug
            if UnitExists(unit) then
                if e == "PLAYER_TARGET_CHANGED" or e == "PLAYER_FOCUS_CHANGED" then
                    for k,v in pairs(allstates) do
                        v.show = false
                        v.changed = true
                    end
                    aura_env.data = {}
                    if UnitExists(unit) then
                        local hp, maxhp = UnitHealth(unit), UnitHealthMax(unit)
                        for i = 1, 100 do
                            local name = command(unit, i)
                            if name == "Conductive Ink" then
                                local caster = select(7, command(unit, i))
                                caster = UnitName(caster)
                                local stacks = select(3, command(unit, i))
                                if aura_env.config.debug == false then
                                    aura_env.myTooltip:SetUnitDebuff(unit, i)
                                    local text =  _G["MyAddOnTooltipTextLeft2"]:GetText()
                                    if text:match("Falling below %d*%% health will cause") then
                                        local damage = text:match("inflict (%d+%,?%d*)")
                                        damage = damage:gsub("%,", "")
                                        damage = damage:gsub(" ", "")
                                        damage = tonumber(damage)
                                        aura_env.data[caster] = {
                                            ["d"] = damage,
                                            ["s"] = stacks,
                                            ["c"] = select(3, UnitClass(caster)),
                                        }
                                    end
                                else
                                    local damage = i * 523427
                                    aura_env.data[caster] = {
                                        ["d"] = damage,
                                        ["s"] = stacks,
                                        ["c"] = select(4, command(unit, i)),
                                    }
                                end
                            elseif not name then
                                break
                            end
                        end
                        update_allstates(aura_env.data, unit)
                    end
                elseif e == "COMBAT_LOG_EVENT_UNFILTERED" then
                    local se = select(2, ...)
                    local hp, maxhp = UnitHealth(unit), UnitHealthMax(unit)
                    --Looking for debuff info upon applying new stack, to update data table
                    if se == "SPELL_AURA_APPLIED_DOSE" then
                        for i = 1, 100 do
                            local name = command(unit, i)
                            if name == "Conductive Ink" then
                                local caster = select(7, command(unit, i))
                                caster = UnitName(caster)
                                local stacks = select(3, command(unit, i))
                                if aura_env.config.debug == false then
                                    aura_env.myTooltip:SetUnitDebuff(unit, i)
                                    local text =  _G["MyAddOnTooltipTextLeft2"]:GetText()
                                    if text:match("Falling below %d*%% health will cause") then
                                        local damage = text:match("inflict (%d+%,?%d*)")
                                        damage = damage:gsub("%,", "")
                                        damage = damage:gsub(" ", "")
                                        damage = tonumber(damage)
                                        aura_env.data[caster] = {
                                            ["d"] = damage,
                                            ["s"] = stacks,
                                            ["c"] = select(3, UnitClass(caster)),
                                        }
                                    end
                                else
                                    local damage = i * 12312
                                    aura_env.data[caster] = {
                                        ["d"] = damage,
                                        ["s"] = stacks,
                                        ["c"] = select(4, command(unit, i)),
                                    }
                                end
                            elseif not name then
                                break
                            end
                            update_allstates(aura_env.data, unit)
                        end
                        --Looking for damage done by debuff at 30% and storing into data as new damage
                    elseif se == "SPELL_DAMAGE" and select(13, ...) == "Conductive Ink" then
                        local caster = select(5, ...)
                        local hit = select(15, ...)
                        local crit = select(21, ...)
                        if aura_env.data[caster] then
                            aura_env.data[caster].d = hit
                            aura_env.data[caster].cr = crit
                            update_allstates(aura_env.data, unit)
                        end
                    end
                end
            end
            return true
        end
    end
end

--CUSTOM TRIGGER
function(t)
	if aura_env.config.tsuenable == false then return t[1] and t[2] elseif aura_env.config.tsuenable == true then return t[3] and t[2] end
end

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

--DISPLAY
function()
	if aura_env.config.tsuenable == false then
		if aura_env.config.pdmg == true then
			if aura_env.config.sh == true then aura_env.owndebuff.damage = aura_env.shorten(aura_env.owndebuff.damage); aura_env.totaldebuff.damage = aura_env.shorten(aura_env.totaldebuff.damage) end
			aura_env.region.text2:SetText(aura_env.owndebuff.stacks .. "\n" .. aura_env.owndebuff.damage .. "\n" .. aura_env.totaldebuff.stacks .. "\n" .. aura_env.totaldebuff.damage .. "\n" .. aura_env.damagepercentage .. "%")
			if aura_env.config.stext == true then aura_env.region.text3:SetText("Player stacks\nPlayer damage\nTotal stacks\nTotal damage\nDamage in %") else aura_env.region.text3:SetText("") end
		else
			if aura_env.config.sh == true then aura_env.totaldebuff.damage = aura_env.shorten(aura_env.totaldebuff.damage) end
			aura_env.region.text2:SetText(aura_env.totaldebuff.stacks .. "\n" .. aura_env.totaldebuff.damage .. "\n" .. aura_env.damagepercentage .. "%")
			if aura_env.config.stext == true then aura_env.region.text3:SetText("Total stacks\nTotal damage\nDamage in %") else aura_env.region.text3:SetText("") end
		end
	else
		if aura_env.statee.caster == "Total" then
			return aura_env.shorten(aura_env.statee.value) .. " - " .. aura_env.statee.stacks .. " - " .. aura_env.statee.percent .. "%"
		else
            if aura_env.statee.crit == true then
                return aura_env.statee.stacks .. " - " .. aura_env.shorten(aura_env.statee.value) .. "(C)"
            else
                return aura_env.statee.stacks .. " - " .. aura_env.shorten(aura_env.statee.value)
            end
		end
	end
end

--INIT
aura_env.myTooltip = CreateFrame("GameTooltip", "MyAddOnTooltip", UIParent, "GameTooltipTemplate")
aura_env.myTooltip:SetOwner(UIParent, "ANCHOR_NONE")
aura_env.owndebuff = {["stacks"] = 0, ["damage"] = 0,}
aura_env.totaldebuff = {["stacks"] = 0, ["damage"] = 0,}

if aura_env.config.tsuenable == false then
    if not aura_env.region.text2 then
        local text2 = aura_env.region:CreateFontString(nil, aura_env.region)
        aura_env.region.text2 = text2
    end
    local font, size, flags = aura_env.region.text:GetFont()
    aura_env.region.text2:SetFont(font, size, flags)
    aura_env.region.text2:SetTextColor(aura_env.config.color[1], aura_env.config.color[2], aura_env.config.color[3], aura_env.config.color[4])
    aura_env.region.text2:SetPoint("CENTER", aura_env.region, "CENTER", aura_env.config.x, 0) 
    aura_env.region.text2:SetText("")
    aura_env.region.text2:Show()
    
    if not aura_env.region.text3 then
        local text3 = aura_env.region:CreateFontString(nil, aura_env.region)
        aura_env.region.text3 = text3
    end
    local font, size, flags = aura_env.region.text:GetFont()
    aura_env.region.text3:SetFont(font, size, flags)
    aura_env.region.text3:SetTextColor(aura_env.config.color[1], aura_env.config.color[2], aura_env.config.color[3], aura_env.config.color[4])
    aura_env.region.text3:SetPoint("CENTER", aura_env.region, "CENTER", - aura_env.config.x, 0) 
    aura_env.region.text3:SetText("")
    aura_env.region.text3:Show()
    aura_env.region.bar:Hide()
end

if aura_env.config.tsuenable == true and aura_env.region.text2 then aura_env.region.text2:Hide() end
if aura_env.config.tsuenable == true and aura_env.region.text3 then aura_env.region.text3:Hide() end
if aura_env.config.tsuenable == true and aura_env.region.bar then aura_env.region.bar:Show() 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.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.classColor = function(class)
    if class == 1 then return 0.78, 0.61, 0.43 
    elseif class == 2 then return 0.96, 0.55, 0.73 
	elseif class == 3 then return 0.67, 0.83, 0.45 
	elseif class == 4 then return 1, 0.96, 0.41, 1  
	elseif class == 5 then return 1, 1, 1 
	elseif class == 6 then return 0.77, 0.12, 0.23 
	elseif class == 7 then return 0, 0.44, 0.87 
	elseif class == 8 then return 0.25, 0.78, 0.92 
	elseif class == 9 then return 0.53, 0.53, 0.93 
	elseif class == 10 then return 0, 1, 0.59 
	elseif class == 11 then return 1, 0.49, 0.04 
	elseif class == 12 then return 0.64, 0.19, 0.79 
	elseif class == 15 then return 0, 1, 0
    else return 1, 1, 1 end
end
--TESTING
--Conductive Ink 1500960 1 Magic 120 119774.845 player false false 302565 false false true false 1
aura_env.UnitDebuffC = function(unit, i)
    if i == 1 then return "Conductive Ink", 1500960, 1, 2, 120, 119774.845, "Pinko"
    elseif i == 1 then return "Conductive Ink", 1500960, 3, 4, 120, 119774.845, "Billy"
    elseif i == 2 then return "Conductive Ink", 1500960, 7, 7, 120, 119774.845, "Mary"
    elseif i == 3 then return "Conductive Ink", 1500960, 35, 11, 120, 119774.845, "Anna"
    elseif i == 4 then return "Conductive Ink", 1500960, 8, 6, 120, 119774.845, "John"
    elseif i == 5 then return "Conductive Ink", 1500960, 72, 2, 120, 119774.845, "Frank"
    elseif i == 6 then return "Conductive Ink", 1500960, 25, 7, 120, 119774.845, "Emma"
    elseif i == 7 then return "Conductive Ink", 1500960, 45, 8, 120, 119774.845, "Sophia"
    elseif i == 8 then return "Conductive Ink", 1500960, 37, 5, 120, 119774.845, "Henry"
    elseif i == 9 then return "Conductive Ink", 1500960, 4, 3, 120, 119774.845, "Ethan"
    elseif i == 10 then return "Conductive Ink", 1500960, 1, 1, 120, 119774.845, "Michael"
    end
end