--

local scripts_folder = script.Parent;
local frame = scripts_folder.Parent;
local icon = frame.PlayerIcon;
local template = icon:Clone();
local mode_folder = frame.Mode;
local radar = frame.Parent;
waitForChild(radar, {'Identifier'; 'Location'})
local identifierLabel = radar.Identifier;
local locationButton = radar.Location;
waitForChild(mode_folder, {'Search'; 'Target'});
local mode = mode_folder.Search;
local target = mode_folder.Target;
local colorList = {
	[BrickColor.new(226)] = true;
	[BrickColor.new(24)] = true;
	[BrickColor.new(106)] = true;
	[BrickColor.new(119)] = true;
	[BrickColor.new(1025)] = true;
	[BrickColor.new(38)] = true;
	[BrickColor.new(11)] = true;
	[BrickColor.new(1006)] = true;
	[BrickColor.new(1016)] = true;
	[BrickColor.new(21)] = true;
}
local modeList = {
	'Closest';
	'Farthest';
}

local function getCurrentModeNumber()
	for index, choice in next, modeList do
		if mode.Value == choice then
			return index;
		end
	end
end

local function toV2(v3)
	return Vector2.new(v3.x, v3.z);
end

local function addCharactersInRange()
	for index, instance in next, workspace:GetChildren() do
		if instance:IsA('Model') and findChildren(instance, body) and not (instance == character) then
			if not ((toV2(instance.Torso.Position) - toV2(character.Torso.Position)).magnitude > distance) then
				characters[#characters + 1] = {instance; instance.Torso.Position - character.Torso.Position};
			end
		end
	end
end

local function clearParentOf(parent, list)
	local iterations = #parent:GetChildren();
	for index, name in next, list do
		for i = 1, iterations do
			if parent:FindFirstChild(name) then
				parent[name]:Destroy();
			end
		end
		iterations = #parent:GetChildren();
	end
end

local function assignColor(instance)
	local chosen
	spawn(function()
		if not chosen then
			for color, enabled in next, colorList do
				colorList[color] = true;
			end
			assignColor(instance);
		end
	end)
	for color, enabled in next, colorList do
		if enabled then
			colors[instance] = color;
			chosen = true;
			colorList[color] = false;
			return
		end
	end
end

local function returnRangeFromMode(mode)
	if mode == 'Closest' then
		return 50;
	elseif mode == 'Farthest' then
		return 0;
	end
end

local function searchPriority()
	local range = returnRangeFromMode(mode.Value);
	local result = false;
	for index, information in next, characters do
		local located = information[1];
		local deltaP = information[2];
		local current = toV2(deltaP).magnitude;
		if mode.Value == 'Closest' then
			if current < range then
				range = current;
				target.Value = located.Name;
				result = true;
			end
		elseif mode.Value == 'Farthest' then
			if current > range then
				range = current;
				target.Value = located.Name;
				result = true;
			end
		end
	end
	if not result then
		target.Value = 'None';
	end
end

locationButton.MouseButton1Click:connect(function()
	local current = getCurrentModeNumber();
	local nextNumber = not (current + 1 > #modeList) and current + 1 or 1;
	mode.Value = modeList[nextNumber];
end)

mode.Changed:connect(function()
	locationButton.Text = mode.Value .. ':';
end)

game:GetService('RunService').RenderStepped:connect(function()
	clearParentOf(frame, {'Player'});
	local characterSize = character:GetModelSize()
	local lookDirection = character.Head.CFrame.lookVector;
	local cameraDirection = camera.CoordinateFrame.lookVector;
	local rotation = math.atan2(lookDirection.Z, lookDirection.X);
	icon.Rotation = math.deg(rotation) + 180;
	rotation = math.atan2(cameraDirection.Z, cameraDirection.X);
	frame.Rotation = -(math.deg(rotation) - 90);
	characters = {};
	addCharactersInRange();
	for index, information in next, characters do
		local clone = template:Clone();
		local located = information[1];
		local deltaP = information[2];
		local symbol = deltaP.Y < -characterSize.Y and '-' or (deltaP.Y > characterSize.Y and '+' or '');
		local facingDirection = located.Head.CFrame.lookVector
		local rotation = math.atan2(facingDirection.Z, facingDirection.X);
		local xScale, yScale = 0.4 + (-deltaP.X / distance) / (3), 0.4 + (-deltaP.Z / distance) / (3);
		-- xScale = (not (xScale > 1) and xScale or 1) and (not (xScale < 0) and xScale or 0);
		-- yScale = (not (yScale > 1) and yScale or 1) and (not (yScale < 0) and yScale or 0);
		if not colors[located] then
			assignColor(located);
		end
		clone.ZIndex = deltaP.Y < -characterSize.Y and (icon.ZIndex - 1) or (deltaP.Y > characterSize.Y and (icon.ZIndex + 1));
		clone.Rotation = math.deg(rotation) + 180;
		clone.TextColor = colors[located] or BrickColor.Random();
		clone.Name = 'Player';
		clone.Position = UDim2.new(xScale, 0, yScale, 0);
		clone.Text = symbol .. '>'
		clone.Parent = frame
	end
	searchPriority();
	identifierLabel.Text = target.Value;
end)