--QUEST_LOG_UPDATE
function(e, unit)
	if e == "QUEST_LOG_UPDATE" then
		local entries = GetNumQuestLogEntries()
		for i = 1, entries do
			if GetQuestLogLeaderBoard(1, i) then
				for j = 1, 40 do
					local text, type, finished = GetQuestLogLeaderBoard(1, i)
					if not text then
						break
					elseif text and not finished then
						if type == "monster" and text:match("slain") then
							local count, mcount = text:match("(%d*)%/(%d*)")
							local rem = mcount - count
							local mob = text:match("%d*%/%d* (.+) slain")
							aura_env.mobs[mob] = rem
						end
					elseif finished and aura_env.mobs[mob] then
						local mob = text:match("%d*%/%d* (.+) slain")
						table.remove(aura_env.mobs, mob)
					end
				end
			end
		end
	end
end

--NAME_PLATE_UNIT_ADDED NAME_PLATE_UNIT_REMOVED
--TSU
function(allstates, e, unit)
	if e == "NAME_PLATE_UNIT_ADDED" then
		if UnitExists(unit) then
			if aura_env.mobs[UnitName(unit)] then
				local name = UnitName(unit)
				allstates[unit] = 
				{
					show = true,
					changed = true,
					unit = unit,
					name = name,
					count = aura_env.mobs[name],
				}
				return true
			end
		end
	elseif e == "NAME_PLATE_UNIT_REMOVED" then
		if allstates[unit] then
			allstates[unit] = 
			{
				show = false,
				changed = true,
			}
			return true
		end
	end
end

--ON SHOW
if not aura_env.region.text then
    local text = aura_env.region:CreateFontString(nil, "OVERLAY")
    aura_env.region.text = text
end
aura_env.region:SetHeight(128)
aura_env.region:SetWidth(512)
aura_env.region.text:SetFont("Interface\\AddOns\\SharedMedia_MyMedia\\font\\FiraCode-Regular.ttf", 18, "OUTLINE")
aura_env.region.text:SetTextColor(1,1,1,1)
aura_env.region.text:SetText(aura_env.statee.count)
if not aura_env.region.texture then
    local texture = aura_env.region:CreateTexture(nil, "OVERLAY")
    aura_env.region.texture = texture
end
aura_env.region.texture:SetTexture("Interface\\Addons\\WeakAuras\\PowerAurasMedia\\Auras\\Aura51")
aura_env.region.texture:SetHeight(32)
aura_env.region.texture:SetWidth(32)
local plate = C_NamePlate.GetNamePlateForUnit(aura_env.statee.unit)
if plate then
	print("found plate pls work")
    aura_env.region.texture:ClearAllPoints()
    aura_env.region.texture:SetPoint("TOP", plate, "TOP", 0, 32)
	aura_env.region.texture:Show()
	aura_env.region.text:ClearAllPoints()
	aura_env.region.text:SetPoint("CENTER", plate, "CENTER", 32, 48)
	aura_env.region.text:Show()
end

--INIT
aura_env.mobs = {}
local entries = GetNumQuestLogEntries()
for i = 1, entries do
	if GetQuestLogLeaderBoard(1, i) then
		for j = 1, 40 do
			local text, type, finished = GetQuestLogLeaderBoard(1, i)
			if not text then
				break
			elseif text and not finished then
				if type == "monster" and text:match("slain") then
					local count, mcount = text:match("(%d*)%/(%d*)")
					local rem = mcount - count
					local mob = text:match("%d*%/%d* (.+) slain")
					aura_env.mobs[mob] = rem
				end
			elseif finished and aura_env.mobs[mob] then
				local mob = text:match("%d*%/%d* (.+) slain")
				table.remove(aura_env.mobs, mob)
			end
		end
	end
end