--CHAT_MSG_CHANNEL
function(e, msg, _, _, channel)
	if channel:match("General") and (channel:match("Nazjatar") or channel:match("Mechagon")) then
		if msg:match("(KX%-T57 (%d+%.?%d*))") then msg = msg:gsub("(KX%-T57 (%d+%.?%d*))", "") end
		local x, y = msg:match("(%d+%.?%d*)[ ,/]+(%d+%.?%d*)")
		if x and y then
			local command = x .. " " .. y
			aura_env.waypoints[#aura_env.waypoints + 1] = {["x"] = x, ["y"] = y,}
			WeakAuras.ScanEvents("NEW_ID")
		end
	end
end

--EVERY FRAME
function()
	if not aura_env.noidee then
		local function range (val, min, max, max2)
			val = 1 - (((max - val) / (max - min)) * max2)
			return val
		end
		if not aura_env.idee then WeakAuras.ScanEvents("NEW_ID") 
		else
			local hX, hY = aura_env.waypoints[aura_env.idee]["x"], aura_env.waypoints[aura_env.idee]["y"]
			local myMapID = C_Map.GetBestMapForUnit("player")
			local pX, pY = C_Map.GetPlayerMapPosition(myMapID, "player").x * 100, C_Map.GetPlayerMapPosition(myMapID, "player").y * 100
			local X = pX - hX
			local Y = pY - hY
			local playerFace = GetPlayerFacing() or 0
			local playerA = math.floor(playerFace * 100)
			playerA = range(playerA, 0, 630, 360) - 1
			playerA = - playerA
			playerA = playerA - 90
			if playerA < 0 then playerA = playerA + 360 end
			aura_env.hyp = math.sqrt((math.abs(X ^ 2)) + (math.abs(Y ^ 2))) * 100
			aura_env.region.distance:SetText(math.floor(aura_env.hyp))
			aura_env.angle = math.deg(math.atan2(Y, X))
			aura_env.angle = aura_env.angle - playerA
			aura_env.angle = aura_env.angle - 180
			if aura_env.hyp < aura_env.wipeDistance then
				table.remove(aura_env.waypoints, aura_env.idee)
				aura_env.idee = nil
				WeakAuras.ScanEvents("NEW_ID")
			end
			return true
		end
	end
end

--NEW_ID REMOVE_ID
function(e)
	if e == "NEW_ID" then
		if not IsInInstance() then
			local maxhyp = 100000
			local myMapID = C_Map.GetBestMapForUnit("player")
			local pX, pY = C_Map.GetPlayerMapPosition(myMapID, "player").x * 100, C_Map.GetPlayerMapPosition(myMapID, "player").y * 100
			for k,v in ipairs(aura_env.waypoints) do
				local hX, hY = aura_env.waypoints[k].x, aura_env.waypoints[k].y
				local X, Y = pX - hX, pY - hY
				local hyp = math.sqrt((math.abs(X^2)) + (math.abs(Y^2)))
				if hyp < maxhyp then
					maxhyp = hyp
					aura_env.idee = k
				end
			end
			if not aura_env.idee then aura_env.noidee = true end
			if aura_env.idee then aura_env.noidee = nil end
		end
	elseif e == "REMOVE_ID" then
		if aura_env.idee then
			table.remove(aura_env.waypoints, aura_env.idee)
			aura_env.idee = nil
			WeakAuras.ScanEvents("NEW_ID")
		else
		end
	end
end

--ANIMATION
function()
	if aura_env.angle then
		return - aura_env.angle
	else
		return 0
	end
end

--INIT
aura_env.waypoints = {}
if not aura_env.region.distance then
    local distance = aura_env.region:CreateFontString(nil, aura_env.region)
    aura_env.region.distance = distance
end
aura_env.region.distance:ClearAllPoints()
aura_env.region.distance:SetFont("Interface\\AddOns\\SharedMedia_MyMedia\\font\\FiraCode-Bold.ttf", 16, "OUTLINE")
aura_env.region.distance:SetPoint("CENTER", aura_env.region, "CENTER")
aura_env.region.distance:SetJustifyH("CENTER")
aura_env.region.distance:SetJustifyV("CENTER")
aura_env.region.distance:Show()
aura_env.wipeDistance = 50
WeakAuras.ScanEvents("NEW_ID")