--GROUP_ROSTER_UPDATE INSPECT_READY
function(allstates, e)
	if e == "GROUP_ROSTER_UPDATE" then
		for i = 1, 40 do
			local u = "raid" .. i
			if UnitExists(u) and not UnitIsUnit(u, "player") then
				if not aura_env.data[u] then
					aura_env.data[u] = {
						["name"] = UnitName(u),
						["class"] = select(3, UnitClass(u)),
						["neckilvl"] = 0,
						["backilvl"] = 0,
						["corruption"] = 0,
						["inspectQueue"] = false,
						["inspected"] = false
					}
				end
				if aura_env.data[u] then
					if aura_env.data[u].name ~= UnitName(u) then
						aura_env.data[u] = {
							["name"] = UnitName(u),
							["class"] = select(3, UnitClass(u)),
							["neckilvl"] = 0,
							["backilvl"] = 0,
							["corruption"] = 0,
							["inspectQueue"] = false,
							["inspected"] = false
						}
					end
				end
			elseif not UnitExists(u) and (aura_env.data[u] or allstates[u]) then
				if aura_env.data[u] then
					aura_env.data[u] = nil
				end
				for k,v in pairs(aura_env.inspectQueue) do
					if v == u then
						table.remove(aura_env.inspectQueue, k)
					end
				end
				if allstates[u] then
					allstates[u].show = false
					allstates[u].changed = true
				end
			end
		end
	elseif e == "INSPECT_READY" then
		if aura_env.notifyInspect ~= false and aura_env.inspecting == false then
			InspectUnit(aura_env.notifyInspect)
			aura_env.inspecting = true
		elseif aura_env.notifyInspect ~= false and aura_env.inspecting == true then
			for i = 1, 17 do
				local link = GetInventoryItemLink(aura_env.notifyInspect, i) or 0
				if link ~= 0 then
					local stats = {}
					GetItemStats(link, stats)
					local rarity = select(3, GetItemInfo(link))
					local ilvl = select(4, GetItemInfo(link))
					if aura_env.config.debug == true then 
						for k,v in pairs(stats) do
						end
					end
					if i == 2 then
						aura_env.data[aura_env.notifyInspect].neckilvl = ilvl
					elseif i == 15 and rarity == 5 then
						aura_env.data[aura_env.notifyInspect].backilvl = ilvl
					end
					if stats["ITEM_MOD_CORRUPTION"] then
						aura_env.data[aura_env.notifyInspect].corruption = aura_env.data[aura_env.notifyInspect].corruption + stats["ITEM_MOD_CORRUPTION"]
					end
					if stats["ITEM_MOD_CORRUPTION_RESISTANCE"] then
						aura_env.data[aura_env.notifyInspect].corruption = aura_env.data[aura_env.notifyInspect].corruption - stats["ITEM_MOD_CORRUPTION_RESISTANCE"]
					end
				end
			end
			if aura_env.data[aura_env.notifyInspect].backilvl > 0 then
				local bil = aura_env.data[aura_env.notifyInspect].backilvl
				if bil >= 470 and bil < 472 then
					aura_env.data[aura_env.notifyInspect].corruption = aura_env.data[aura_env.notifyInspect].corruption - 5
				elseif bil >= 472 and bil <= 474 then 
					aura_env.data[aura_env.notifyInspect].corruption = aura_env.data[aura_env.notifyInspect].corruption - 12
				elseif bil >= 476 and bil <= 480 then 
					aura_env.data[aura_env.notifyInspect].corruption = aura_env.data[aura_env.notifyInspect].corruption - 20
				elseif bil >= 482 and bil <= 484 then 
					aura_env.data[aura_env.notifyInspect].corruption = aura_env.data[aura_env.notifyInspect].corruption - 27
				elseif bil >= 486 and bil <= 488 then 
					aura_env.data[aura_env.notifyInspect].corruption = aura_env.data[aura_env.notifyInspect].corruption - 35
				elseif bil >= 490 and bil <= 494 then 
					aura_env.data[aura_env.notifyInspect].corruption = aura_env.data[aura_env.notifyInspect].corruption - 42
				elseif bil >= 494 then 
					aura_env.data[aura_env.notifyInspect].corruption = aura_env.data[aura_env.notifyInspect].corruption - 50
				end
			end
			aura_env.data[aura_env.notifyInspect].inspectQueue = false
			aura_env.data[aura_env.notifyInspect].inspected = true
			aura_env.notifyInspect = false
			ClearInspectPlayer()
		end
	end

	for k,v in pairs(aura_env.data) do
		if v.inspected == false and v.inspectQueue == false then
			aura_env.inspectQueue[#aura_env.inspectQueue + 1] = k
			v.inspectQueue = true
		end
		if v.neckilvl == 0 and v.inspectQueue == false then
			aura_env.inspectQueue[#aura_env.inspectQueue + 1] = k
			v.inspectQueue = true
		end
	end

	for k,v in pairs(aura_env.data) do
		allstates[k] = {
			show = true,
			changed = true,
			name = v.name,
			class = v.class,
			classColor = aura_env.classColor(v.class),
			backilvl = v.backilvl,
			backColor = "|cff" .. aura_env.config.ccolor,
			neckilvl = v.neckilvl,
			neckColor = "|cff" .. aura_env.config.ncolor,
			corruption = v.corruption,
			corruptionColor = "|cff" .. aura_env.config.cocolor,
			resort = true,
			index = v.name,
		}
		if aura_env.config.sort == 1 then
			allstates[k].index = v.name
		elseif aura_env.config.sort == 2 then
			allstates[k].index = v.backilvl
		elseif aura_env.config.sort == 3 then
			allstates[k].index = v.neckilvl
		elseif aura_env.config.sort == 4 then
			allstates[k].index = v.corruption
		end
	end
	return true
end

--DISPLAY
%classColor%n %neckColor%neckilvl %backColor%backilvl %corruptionColor%corruption

--INIT
if WeakAuras.IsOptionsOpen() then
	if aura_env.ticker then
		aura_env.ticker:Cancel()
		aura_env.ticker = nil
	end
end
local aura_env = aura_env
aura_env.data = {}
aura_env.inspectQueue = {}
aura_env.insIndex = 1
aura_env.notifyInspect = false
aura_env.inspecting = false
aura_env.output = ""
if not WeakAurasSaved.CustomTrash then WeakAurasSaved.CustomTrash = {} end
if not WeakAurasSaved.CustomTrash.Debug then WeakAurasSaved.CustomTrash.Debug = {} end

aura_env.printTable = function(table, n)
	if not n then n = 0 end
	for k,v in pairs(table) do
		local printText = ""
		for i = 1, n do
			printText = printText .. "    "
		end
		printText = printText .. "[" .. k .. "] = " .. tostring(v)
		print(printText)
		if type(v) == "table" then
			n = n + 1
			aura_env.printTable(v, n)
		end
	end
end

local function uninspect()
	aura_env.inspecting = false
end
hooksecurefunc("ClearInspectPlayer", uninspect)

aura_env.classColor = function(class)
      if class == 6 then return "|cFFC41F3B" elseif
      class == 12 then return "|cFFA330C9" elseif
      class == 11 then return "|cFFFF7D0A" elseif
      class == 3 then return "|cFFABD473" elseif
      class == 8 then return "|cFF40C7EB" elseif
      class == 10 then return "|cFF00FF96" elseif
      class == 2 then return "|cFFF58CBA" elseif
      class == 5 then return "|cFFFFFFFF"  elseif
      class == 4 then return "|cFFFFF569"  elseif
      class == 7 then return "|cFF0070DE" elseif
      class == 9 then return "|cFF8787ED" elseif
      class == 1 then return "|cFFC79C6E" else
      return "|cFF000000" end
end

aura_env.inspectPing = function()
	if aura_env.inspecting == false and aura_env.notifyInspect == false then
		if #aura_env.inspectQueue > 0 then
			--print("There are units to inspect D:")
			if aura_env.insIndex >= #aura_env.inspectQueue then
				aura_env.insIndex = 1
			end
			for i = aura_env.insIndex, #aura_env.inspectQueue do
				if CheckInteractDistance(aura_env.inspectQueue[i], 2) == true then
					--print("QUEUEING INSPECT FOR", aura_env.inspectQueue[i])
					ClearInspectPlayer()
					NotifyInspect(aura_env.inspectQueue[i])
					aura_env.notifyInspect = aura_env.inspectQueue[i]
					table.remove(aura_env.inspectQueue, i)
					break
				end
			end
		end
	end
	for k,v in pairs(aura_env.data) do
		if v.neckilvl == 0 and v.inspectQueue == false then
			aura_env.inspectQueue[#aura_env.inspectQueue + 1] = k
			v.inspectQueue = true
		end
	end
end
aura_env.ticker = C_Timer.NewTicker(0.5, aura_env.inspectPing)