-- COMBAT_LOG_EVENT_UNFILTERED AURA_INIT

function(
	states,
	event,
	tstamp,
	message,
	hideCaster,
	srcGUID,
	srcName,
	srcFlags,
	srcRFlags,
	destGUID,
	destName,
	destFlags,
	destRFlags,
	...
)
	if event == "OPTIONS" then
		local amount1 = Round(UnitHealth("player") / 100 * 25)
		local amount2 = Round(UnitHealth("player") / 100 * 15)
		states[""] = {
			name = "PARRY",
			icon = 644389,
			amount = "",
			percent = "",
			school = "unknown",
			index = 3,
			value = UnitHealthMax("player"),
			total = UnitHealthMax("player"),
			progressType = "static",
			autoHide = true,
			changed = true,
			show = true,
		}
		states[1] = {
			name = "Evil Fire Spell",
			icon = 644389,
			amount = aura_env.format(amount1),
			percent = "[25%]",
			schoolIndex = 4,
			index = 2,
			value = UnitHealth("player") / 100 * 50,
			total = UnitHealthMax("player"),
			additionalProgress = { [1] = { direction = "forward", width = amount1 } },
			progressType = "static",
			autoHide = true,
			changed = true,
			show = true,
		}
		states[2] = {
			name = "Evil Shadow Spell",
			icon = 644389,
			amount = aura_env.format(amount2),
			percent = "[15%]",
			schoolIndex = 32,
			index = 1,
			value = UnitHealth("player") / 100 * 35,
			total = UnitHealthMax("player"),
			additionalProgress = { [1] = { direction = "forward", width = amount2 } },
			progressType = "static",
			autoHide = true,
			changed = true,
			show = true,
		}
		states[3] = {
			name = "Healing",
			icon = 644389,
			amount = aura_env.format(amount2),
			percent = "[15%]",
			schoolIndex = 99,
			index = 1,
			value = UnitHealth("player") / 100 * 35,
			total = UnitHealthMax("player"),
			additionalProgress = { [1] = { direction = "forward", width = amount2 } },
			progressType = "static",
			autoHide = true,
			changed = true,
			show = true,
		}
		local aura_env = aura_env
		C_Timer.After(0.1, function() aura_env.sortAndOffset(true) end)
		return true
	end

	if event == "COMBAT_LOG_EVENT_UNFILTERED" and destGUID == WeakAuras.myGUID and ... then
		local c = aura_env.config
		if message == "SWING_DAMAGE" and c.swing then
			local amount, overkill, school, resisted, blocked, absorbed, critical, glancing, crushing = ...
			if amount then
				local healthCurrent, healthMax = UnitHealth("player"), UnitHealthMax("player")
				local threshold = amount * 100 / healthMax
				if threshold >= c.threshold then
					local schools = aura_env.spellSchools
					local SC, SCI = 0, 0
					if schools[school] then
						SC, SCI = schools[school][1], schools[school][2]
					end
					local healthPercentLost = string.format("%.1f%%", threshold)
					--formatted health
					local healthAfterHit = overkill > 0 and 0 or healthCurrent - amount
					--formatted amount
					local color = aura_env.schoolColors[school] or "ffffff"
					local prefix = critical and "|cFFFFD100CRIT:|r"
						or glancing and "|cFFFFD100GLANCE:|r"
						or crushing and "|cFFFFD100CRUSH:|r"
						or "|cFFFFD100HIT:|r"
					local Famount = ("%s |cFF%s%s|r|n"):format(prefix, color, aura_env.format(amount))
					--formatted absorb
					local Fabsorbed = absorbed
							and ("|cFFFFD100ABSORBED:|r |cFFFFFFFF%s|r|n"):format(aura_env.format(absorbed))
						or ""
					--formatted block
					local Fblocked = blocked
							and ("|cFFFFD100BLOCKED:|r |cFFFFFFFF%s|r|n"):format(aura_env.format(blocked))
						or ""
					--formatted resist
					local Fresisted = resisted
							and ("|cFFFFD100RESISTED:|r |cFFFFFFFF%s|r|n"):format(aura_env.format(resisted))
						or ""
					--formatted overkill
					local Foverkill = overkill > 0
							and ("|cFFFFD100OVERKILL:|r |cFFFFFFFF%s|r"):format(aura_env.format(overkill))
						or ""
					local Ftime = ('|cFFFFD100"%s"|r'):format(date("%H:%M:%S"))
					local Fname = srcName and ("|cFFFFFFFF%s|r"):format(srcName) or "|cFFbbbbbbUNKNOWN|r"
					local Finfo = ("|cFFFFFFFFMelee hit|r [%s]"):format(SC == 0 and "unknown" or SC)
					local Fdamage = ("|cFF00FF00%s|r > |cFF00FF00%s|r (%s)"):format(
						aura_env.format(healthCurrent),
						aura_env.format(healthAfterHit),
						healthPercentLost
					)
					local Fdetails = ("%s%s%s%s%s"):format(Famount, Fabsorbed, Fblocked, Fresisted, Foverkill)
					local tooltip = ("%s %s|n|n%s|n%s|n|n%s"):format(Ftime, Fname, Finfo, Fdamage, Fdetails)
					local new = {
						name = "",
						icon = 135274,
						spellid = 0,
						amount = aura_env.format(amount),
						percent = ("[%s]"):format(healthPercentLost),
						school = SC,
						schoolIndex = SCI,
						index = 0,
						value = healthCurrent or 100,
						total = healthMax or 100,
						death = overkill > 0,
						tooltip = tooltip,
						duration = c.duration,
						expirationTime = GetTime() + c.duration,
						additionalProgress = { [1] = { direction = "backward", width = amount } },
						progressType = "static",
						autoHide = true,
						show = true,
						changed = true,
					}
					tinsert(states, new)
					for k, v in pairs(states) do
						v.index = v.index + 1
						v.changed = true
						if v.index > c.limit then --clone limit
							v.show = false
						end
					end
					return true
				end
			end
		elseif message == "SWING_MISSED" and c.missType then
			local missType, isOffHand, amount = ...
			if amount and amount >= c.missThreshold or not amount then
				local healthCurrent, healthMax = UnitHealth("player"), UnitHealthMax("player")
				local Famount = amount and ("|cFFFFD100HIT:|r |cFFFFFFFF%s|r"):format(aura_env.format(amount)) or ""
				local Ftime = ('|cFFFFD100"%s"|r'):format(date("%H:%M:%S"))
				local Fname = srcName and ("|cFFFFFFFF%s|r"):format(srcName) or "|cFFbbbbbbUNKNOWN|r"
				local tooltip = ("%s %s|n|n|cFFFFFFFFMelee hit|r |cFFFF0000%s|r|n%s"):format(
					Ftime,
					Fname,
					missType,
					Famount
				)
				local new = {
					name = missType,
					icon = 135274,
					spellid = 0,
					amount = amount and aura_env.format(amount) or "",
					percent = "",
					school = "unknown",
					schoolIndex = 0,
					index = 0,
					value = healthCurrent or 100,
					total = healthMax or 100,
					tooltip = tooltip,
					duration = c.duration,
					expirationTime = GetTime() + c.duration,
					additionalProgress = { [1] = {} },
					progressType = "static",
					autoHide = true,
					show = true,
					changed = true,
				}

				tinsert(states, new)
				for k, v in pairs(states) do
					v.index = v.index + 1
					v.changed = true
					if v.index > c.limit then --clone limit
						v.show = false
					end
				end

				return true
			end
		elseif message == "ENVIRONMENTAL_DAMAGE" and c.env then --enviromental
			local envType, amount, overkill, school, resisted, blocked, absorbed, critical, glancing, crushing = ...
			if amount then
				local healthCurrent, healthMax = UnitHealth("player"), UnitHealthMax("player")
				local threshold = amount * 100 / healthMax
				if threshold >= c.threshold then
					local schools = aura_env.spellSchools
					local SC, SCI = 0, 0
					if schools[school] then
						SC, SCI = schools[school][1], schools[school][2]
					end

					local healthPercentLost = string.format("%.1f%%", threshold)

					--formatted health
					local healthAfterHit = overkill > 0 and 0 or healthCurrent - amount
					--formatted amount
					local color = aura_env.schoolColors[school] or "ffffff"
					local prefix = critical and "|cFFFFD100CRIT:|r"
						or glancing and "|cFFFFD100GLANCE:|r"
						or crushing and "|cFFFFD100CRUSH:|r"
						or "|cFFFFD100HIT:|r"

					local Famount = ("%s |cFF%s%s|r|n"):format(prefix, color, aura_env.format(amount))
					--formatted absorb
					local Fabsorbed = absorbed
							and ("|cFFFFD100ABSORBED:|r |cFFFFFFFF%s|r|n"):format(aura_env.format(absorbed))
						or ""
					--formatted block
					local Fblocked = blocked
							and ("|cFFFFD100BLOCKED:|r |cFFFFFFFF%s|r|n"):format(aura_env.format(blocked))
						or ""
					--formatted resist
					local Fresisted = resisted
							and ("|cFFFFD100RESISTED:|r |cFFFFFFFF%s|r|n"):format(aura_env.format(resisted))
						or ""
					--formatted overkill
					local Foverkill = overkill > 0
							and ("|cFFFFD100OVERKILL:|r |cFFFFFFFF%s|r"):format(aura_env.format(overkill))
						or ""

					local Ftime = ('|cFFFFD100"%s"|r'):format(date("%H:%M:%S"))
					local Fname = srcName and ("|cFFFFFFFF%s|r"):format(srcName) or "|cFFbbbbbbUNKNOWN|r"
					local Finfo = ("|cFFFFFFFFEnvironmental|r [%s]"):format(SC == 0 and "unknown" or SC)
					local Fdamage = ("|cFF00FF00%s|r > |cFF00FF00%s|r (%s)"):format(
						aura_env.format(healthCurrent),
						aura_env.format(healthAfterHit),
						healthPercentLost
					)
					local Fdetails = ("%s%s%s%s%s"):format(Famount, Fabsorbed, Fblocked, Fresisted, Foverkill)
					local tooltip = ("%s %s|n|n%s|n%s|n|n%s"):format(Ftime, Fname, Finfo, Fdamage, Fdetails)

					local eTypes = aura_env.envTypes

					local new = {
						name = envType,
						icon = eTypes[envType] or 132996,
						spellid = 0,
						amount = aura_env.format(amount),
						percent = ("[%s]"):format(healthPercentLost),
						school = SC,
						schoolIndex = SCI,
						index = 0,
						value = healthCurrent or 100,
						total = healthMax or 100,
						death = overkill > 0,
						tooltip = tooltip,
						duration = c.duration,
						expirationTime = GetTime() + c.duration,
						additionalProgress = { [1] = { direction = "forward", width = amount } },
						progressType = "static",
						autoHide = true,
						show = true,
						changed = true,
					}

					tinsert(states, new)
					for k, v in pairs(states) do
						v.index = v.index + 1
						v.changed = true
						if v.index > c.limit then --clone limit
							v.show = false
						end
					end
					return true
				end
			end
		elseif message == "ENVIRONMENTAL_MISSED" and c.missType then
			local envType, missType, isOffHand, amount = ...
			if amount and amount >= c.missThreshold or not amount then
				local healthCurrent, healthMax = UnitHealth("player"), UnitHealthMax("player")
				local Famount = amount and ("|cFFFFD100HIT:|r |cFFFFFFFF%s|r"):format(aura_env.format(amount)) or ""

				local Ftime = ('|cFFFFD100"%s"|r'):format(date("%H:%M:%S"))
				local Fname = envType and ("|cFFFFFFFF%s|r"):format(envType) or "|cFFbbbbbbUNKNOWN|r"
				local tooltip = ("%s %s|n|n|cFFFFFFFFEnvironmental|r |cFFFF0000%s|r|n%s"):format(
					Ftime,
					Fname,
					missType,
					Famount
				)

				local eTypes = aura_env.envTypes

				local new = {
					name = missType,
					icon = eTypes[envType] or 132996,
					spellid = 0,
					amount = amount and aura_env.format(amount) or "",
					percent = "",
					school = "unknown",
					schoolIndex = 0,
					index = 0,
					value = healthCurrent or 100,
					total = healthMax or 100,
					tooltip = tooltip,
					duration = c.duration,
					expirationTime = GetTime() + c.duration,
					additionalProgress = { [1] = {} },
					progressType = "static",
					autoHide = true,
					show = true,
					changed = true,
				}

				tinsert(states, new)
				for k, v in pairs(states) do
					v.index = v.index + 1
					v.changed = true
					if v.index > c.limit then --clone limit
						v.show = false
					end
				end
				return true
			end
		elseif string.find(message, "_DAMAGE") and c.other then
			--SPELL_DAMAGE SPELL_PERIODIC_DAMAGE RANGE_DAMAGE
			local spellid, spellName, spellSchool, amount, overkill, school, resisted, blocked, absorbed, critical, glancing, crushing =
				...
			if (c.stagger and spellid ~= 124255 or not c.stagger) and amount then
				local healthCurrent, healthMax = UnitHealth("player"), UnitHealthMax("player")
				local threshold = amount * 100 / healthMax
				if threshold >= c.threshold then
					local schools = aura_env.spellSchools
					local SC, SCI = 0, 0
					if schools[school] then
						SC, SCI = schools[school][1], schools[school][2]
					end

					local healthCurrent, healthMax = UnitHealth("player"), UnitHealthMax("player")
					local healthPercentLost = string.format("%.1f%%", threshold)

					--formatted health
					local healthAfterHit = overkill > 0 and 0 or healthCurrent - amount
					--formatted amount
					local color = aura_env.schoolColors[school] or "ffffff"
					local prefix = critical and "|cFFFFD100CRIT:|r"
						or glancing and "|cFFFFD100GLANCE:|r"
						or crushing and "|cFFFFD100CRUSH:|r"
						or "|cFFFFD100HIT:|r"

					local Famount = ("%s |cFF%s%s|r|n"):format(prefix, color, aura_env.format(amount))
					--formatted absorb
					local Fabsorbed = absorbed
							and ("|cFFFFD100ABSORBED:|r |cFFFFFFFF%s|r|n"):format(aura_env.format(absorbed))
						or ""
					--formatted block
					local Fblocked = blocked
							and ("|cFFFFD100BLOCKED:|r |cFFFFFFFF%s|r|n"):format(aura_env.format(blocked))
						or ""
					--formatted resist
					local Fresisted = resisted
							and ("|cFFFFD100RESISTED:|r |cFFFFFFFF%s|r|n"):format(aura_env.format(resisted))
						or ""
					--formatted overkill
					local Foverkill = overkill > 0
							and ("|cFFFFD100OVERKILL:|r |cFFFFFFFF%s|r"):format(aura_env.format(overkill))
						or ""

					local Ftime = ('|cFFFFD100"%s"|r'):format(date("%H:%M:%S"))
					local Fname = srcName and ("|cFFFFFFFF%s|r"):format(srcName) or "|cFFbbbbbbUNKNOWN|r"
					local Finfo = ("|cFFFFFFFF%s|r [|cFF%s%s|r]"):format(
						spellName or "unknown",
						color,
						SC == 0 and "unknown" or SC
					)
					local Fdamage = ("|cFF00FF00%s|r > |cFF00FF00%s|r (%s)"):format(
						aura_env.format(healthCurrent),
						aura_env.format(healthAfterHit),
						healthPercentLost
					)
					local Fdetails = ("%s%s%s%s%s"):format(Famount, Fabsorbed, Fblocked, Fresisted, Foverkill)
					local tooltip = ("%s %s|n|n%s|nspellID: |cFF00FFFF%s|r|n|n%s|n|n%s"):format(
						Ftime,
						Fname,
						Finfo,
						spellid or "",
						Fdamage,
						Fdetails
					)

					local new = {
						name = spellName,
						icon = GetSpellTexture(spellid),
						spellid = spellid,
						amount = aura_env.format(amount),
						percent = ("[%s]"):format(healthPercentLost),
						school = SC,
						schoolIndex = SCI,
						index = 0,
						value = healthCurrent or 100,
						total = healthMax or 100,
						death = overkill > 0,
						tooltip = tooltip,
						duration = c.duration,
						expirationTime = GetTime() + c.duration,
						additionalProgress = { [1] = { direction = "forward", width = amount } },
						progressType = "static",
						autoHide = true,
						show = true,
						changed = true,
					}

					tinsert(states, new)
					for k, v in pairs(states) do
						v.index = v.index + 1
						v.changed = true
						if v.index > c.limit then --clone limit
							v.show = false
						end
					end
					return true
				end
			end
		elseif string.find(message, "_HEAL") and c.other then
			local spellid, spellName, spellSchool, amount, overkill, school, resisted, blocked, absorbed, critical, glancing, crushing =
				...
			if c.healing and amount then
				local healthCurrent, healthMax = UnitHealth("player"), UnitHealthMax("player")
				local threshold = amount * 100 / healthMax
				if threshold >= c.hthreshold then
					local SC, SCI = 0, 99
					local healthCurrent, healthMax = UnitHealth("player"), UnitHealthMax("player")
					local healthPercentLost = string.format("%.1f%%", threshold)
					local healthAfterHit = min(healthMax, healthCurrent + amount)
					local color = aura_env.schoolColors[school] or "ffffff"
					local prefix = critical and "|cFFFFD100CRIT:|r" or "|cFFFFD100HIT:|r"
					local Famount = ("%s |cFF%s%s|r|n"):format(prefix, color, aura_env.format(amount))
					local Ftime = ('|cFFFFD100"%s"|r'):format(date("%H:%M:%S"))
					local Fname = srcName and ("|cFFFFFFFF%s|r"):format(srcName) or "|cFFbbbbbbUNKNOWN|r"
					local Finfo = ("|cFFFFFFFF%s|r [|cFF%s%s|r]"):format(
						spellName or "unknown",
						color,
						SC == 0 and "" or SC
					)
					local Fdamage = ("|cFF00FF00%s|r > |cFF00FF00%s|r (%s)"):format(
						aura_env.format(healthCurrent),
						aura_env.format(healthAfterHit),
						healthPercentLost
					)
					local Fdetails = ("%s"):format(Famount)
					local tooltip = ("%s %s|n|n%s|nspellID: |cFF00FFFF%s|r|n|n%s|n|n%s"):format(
						Ftime,
						Fname,
						Finfo,
						spellid or "",
						Fdamage,
						Fdetails
					)

					local new = {
						name = spellName,
						icon = GetSpellTexture(spellid),
						spellid = spellid,
						amount = aura_env.format(amount),
						percent = ("[%s]"):format(healthPercentLost),
						school = SC,
						schoolIndex = SCI,
						index = 0,
						value = healthCurrent or 100,
						total = healthMax or 100,
						death = overkill > 0,
						tooltip = tooltip,
						duration = c.duration,
						expirationTime = GetTime() + c.duration,
						additionalProgress = { [1] = { direction = "forward", width = amount } },
						progressType = "static",
						autoHide = true,
						show = true,
						changed = true,
					}

					tinsert(states, new)
					for k, v in pairs(states) do
						v.index = v.index + 1
						v.changed = true
						if v.index > c.limit then --clone limit
							v.show = false
						end
					end
					return true
				end
			end
		elseif string.find(message, "_MISSED") and c.missType then
			--SPELL_MISSED SPELL_PERIODIC_MISSED RANGE_MISSED
			local spellid, spellName, school, missType, isOffHand, amount = ...
			if c.stagger and spellid ~= 124255 or not c.stagger then
				if amount and amount >= c.missThreshold or not amount then
					local healthCurrent, healthMax = UnitHealth("player"), UnitHealthMax("player")
					local Famount = amount and ("|cFFFFD100HIT:|r |cFFFFFFFF%s|r"):format(aura_env.format(amount)) or ""

					local Ftime = ('|cFFFFD100"%s"|r'):format(date("%H:%M:%S"))
					local Fname = srcName and ("|cFFFFFFFF%s|r"):format(srcName) or "|cFFbbbbbbUNKNOWN|r"
					local tooltip = ("%s %s|n|n|cFFFFFFFF%s|r |cFFFF0000%s|r|nspellID: |cFF00FFFF%s|r|n%s"):format(
						Ftime,
						Fname,
						spellName,
						missType,
						spellid,
						Famount
					)

					local new = {
						name = missType,
						icon = GetSpellTexture(spellid),
						spellid = spellid,
						amount = amount and aura_env.format(amount) or "",
						percent = "",
						school = "unknown",
						schoolIndex = 0,
						index = 0,
						value = healthCurrent or 100,
						total = healthMax or 100,
						tooltip = tooltip,
						duration = c.duration,
						expirationTime = GetTime() + c.duration,
						additionalProgress = { [1] = {} },
						progressType = "static",
						autoHide = true,
						show = true,
						changed = true,
					}

					tinsert(states, new)
					for k, v in pairs(states) do
						v.index = v.index + 1
						v.changed = true
						if v.index > c.limit then --clone limit
							v.show = false
						end
					end
					return true
				end
			end
		end
	end
end
