// This file is automatically generated by a script based on the CommandBuilder API.
// This file adds additional overloads to the CommandBuilder API with convenience parameters like colors.
using Unity.Burst;
using UnityEngine;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;

namespace Pathfinding.Drawing {
	public partial struct CommandBuilder {
		/// <summary>\copydocref{Line(float3,float3)}</summary>
		public void Line (float3 a, float3 b, Color color) {
			Reserve<Color32, LineData>();
			Add(Command.Line | Command.PushColorInline);
			Add(ConvertColor(color));
			Add(new LineData { a = a, b = b });
		}
		/// <summary>\copydocref{Ray(float3,float3)}</summary>
		public void Ray (float3 origin, float3 direction, Color color) {
			Line(origin, origin + direction, color);
		}
		/// <summary>\copydocref{Ray(Ray,float)}</summary>
		public void Ray (Ray ray, float length, Color color) {
			Line(ray.origin, ray.origin + ray.direction * length, color);
		}
		/// <summary>\copydocref{Arc(float3,float3,float3)}</summary>
		/// <param name="color">Color of the object</param>
		public void Arc (float3 center, float3 start, float3 end, Color color) {
			PushColor(color);
			var d1 = start - center;
			var d2 = end - center;
			var normal = math.cross(d2, d1);

			if (math.any(normal != 0) && math.all(math.isfinite(normal))) {
				var m = Matrix4x4.TRS(center, Quaternion.LookRotation(d1, normal), Vector3.one);
				var angle = Vector3.SignedAngle(d1, d2, normal) * Mathf.Deg2Rad;
				PushMatrix(m);
				CircleXZInternal(float3.zero, math.length(d1), 90 * Mathf.Deg2Rad, 90 * Mathf.Deg2Rad - angle);
				PopMatrix();
			}
			PopColor();
		}
		/// <summary>\copydocref{CircleXZ(float3,float,float,float)}</summary>
		/// <param name="color">Color of the object</param>
		[System.Obsolete("Use Draw.xz.Circle instead")]
		public void CircleXZ (float3 center, float radius, float startAngle, float endAngle, Color color) {
			CircleXZInternal(center, radius, startAngle, endAngle, color);
		}
		/// <summary>\copydocref{CircleXZ(float3,float,float,float)}</summary>
		/// <param name="color">Color of the object</param>
		[System.Obsolete("Use Draw.xz.Circle instead")]
		public void CircleXZ (float3 center, float radius, Color color) {
			CircleXZ(center, radius, 0f, 2 * Mathf.PI, color);
		}


		/// <summary>\copydocref{Circle(float3,float3,float)}</summary>
		public void Circle (float3 center, float3 normal, float radius, Color color) {
			Reserve<Color32, CircleData>();
			Add(Command.Circle | Command.PushColorInline);
			Add(ConvertColor(color));
			Add(new CircleData { center = center, normal = normal, radius = radius });
		}







		/// <summary>\copydocref{WireCylinder(float3,float3,float)}</summary>
		public void WireCylinder (float3 bottom, float3 top, float radius, Color color) {
			WireCylinder(bottom, top - bottom, math.length(top - bottom), radius, color);
		}
		/// <summary>\copydocref{WireCylinder(float3,float3,float,float)}</summary>
		/// <param name="color">Color of the object</param>
		public void WireCylinder (float3 position, float3 up, float height, float radius, Color color) {
			up = math.normalizesafe(up);
			if (math.all(up == 0) || math.any(math.isnan(up)) || math.isnan(height) || math.isnan(radius)) return;
			PushColor(color);

			OrthonormalBasis(up, out var basis1, out var basis2);

			PushMatrix(new float4x4(
				new float4(basis1 * radius, 0),
				new float4(up * height, 0),
				new float4(basis2 * radius, 0),
				new float4(position, 1)
				));

			CircleXZInternal(float3.zero, 1);
			if (height > 0) {
				CircleXZInternal(new float3(0, 1, 0), 1);
				Line(new float3(1, 0, 0), new float3(1, 1, 0));
				Line(new float3(-1, 0, 0), new float3(-1, 1, 0));
				Line(new float3(0, 0, 1), new float3(0, 1, 1));
				Line(new float3(0, 0, -1), new float3(0, 1, -1));
			}
			PopMatrix();
			PopColor();
		}
		/// <summary>\copydocref{WireCapsule(float3,float3,float)}</summary>
		/// <param name="color">Color of the object</param>
		public void WireCapsule (float3 start, float3 end, float radius, Color color) {
			PushColor(color);
			var dir = end - start;
			var length = math.length(dir);

			if (length < 0.0001) {
				// The endpoints are the same, we can't draw a capsule from this because we don't know its orientation.
				// Draw a sphere as a fallback
				WireSphere(start, radius);
			} else {
				var normalized_dir = dir / length;

				WireCapsule(start - normalized_dir*radius, normalized_dir, length + 2*radius, radius);
			}
			PopColor();
		}
		/// <summary>\copydocref{WireCapsule(float3,float3,float,float)}</summary>
		/// <param name="color">Color of the object</param>
		public void WireCapsule (float3 position, float3 direction, float length, float radius, Color color) {
			direction = math.normalizesafe(direction);
			if (math.all(direction == 0) || math.any(math.isnan(direction)) || math.isnan(length) || math.isnan(radius)) return;
			PushColor(color);

			if (radius <= 0) {
				Line(position, position + direction * length);
			} else {
				length = math.max(length, radius*2);
				OrthonormalBasis(direction, out var basis1, out var basis2);

				PushMatrix(new float4x4(
					new float4(basis1, 0),
					new float4(direction, 0),
					new float4(basis2, 0),
					new float4(position, 1)
					));
				CircleXZInternal(new float3(0, radius, 0), radius);
				PushMatrix(XZtoXYPlaneMatrix);
				CircleXZInternal(new float3(0, 0, radius), radius, Mathf.PI, 2 * Mathf.PI);
				PopMatrix();
				PushMatrix(XZtoYZPlaneMatrix);
				CircleXZInternal(new float3(radius, 0, 0), radius, Mathf.PI*0.5f, Mathf.PI*1.5f);
				PopMatrix();
				if (length > 0) {
					var upperY = length - radius;
					var lowerY = radius;
					CircleXZInternal(new float3(0, upperY, 0), radius);
					PushMatrix(XZtoXYPlaneMatrix);
					CircleXZInternal(new float3(0, 0, upperY), radius, 0, Mathf.PI);
					PopMatrix();
					PushMatrix(XZtoYZPlaneMatrix);
					CircleXZInternal(new float3(upperY, 0, 0), radius, -Mathf.PI*0.5f, Mathf.PI*0.5f);
					PopMatrix();
					Line(new float3(radius, lowerY, 0), new float3(radius, upperY, 0));
					Line(new float3(-radius, lowerY, 0), new float3(-radius, upperY, 0));
					Line(new float3(0, lowerY, radius), new float3(0, upperY, radius));
					Line(new float3(0, lowerY, -radius), new float3(0, upperY, -radius));
				}
				PopMatrix();
			}
			PopColor();
		}
		/// <summary>\copydocref{WireSphere(float3,float)}</summary>
		public void WireSphere (float3 position, float radius, Color color) {
			PushColor(color);
			SphereOutline(position, radius);
			Circle(position, new float3(1, 0, 0), radius);
			Circle(position, new float3(0, 1, 0), radius);
			Circle(position, new float3(0, 0, 1), radius);
			PopColor();
		}
		/// <summary>\copydocref{Polyline(List<Vector3>,bool)}</summary>
		/// <param name="color">Color of the object</param>
		[BurstDiscard]
		public void Polyline (List<Vector3> points, bool cycle, Color color) {
			PushColor(color);
			for (int i = 0; i < points.Count - 1; i++) {
				Line(points[i], points[i+1]);
			}
			if (cycle && points.Count > 1) Line(points[points.Count - 1], points[0]);
			PopColor();
		}
		/// <summary>\copydocref{Polyline(List<Vector3>,bool)}</summary>
		/// <param name="color">Color of the object</param>
		[BurstDiscard]
		public void Polyline (List<Vector3> points, Color color) {
			Polyline(points, false, color);
		}
		/// <summary>\copydocref{Polyline(Vector3[],bool)}</summary>
		/// <param name="color">Color of the object</param>
		[BurstDiscard]
		public void Polyline (Vector3[] points, bool cycle, Color color) {
			PushColor(color);
			for (int i = 0; i < points.Length - 1; i++) {
				Line(points[i], points[i+1]);
			}
			if (cycle && points.Length > 1) Line(points[points.Length - 1], points[0]);
			PopColor();
		}
		/// <summary>\copydocref{Polyline(Vector3[],bool)}</summary>
		/// <param name="color">Color of the object</param>
		[BurstDiscard]
		public void Polyline (Vector3[] points, Color color) {
			Polyline(points, false, color);
		}
		/// <summary>\copydocref{Polyline(float3[],bool)}</summary>
		/// <param name="color">Color of the object</param>
		[BurstDiscard]
		public void Polyline (float3[] points, bool cycle, Color color) {
			PushColor(color);
			for (int i = 0; i < points.Length - 1; i++) {
				Line(points[i], points[i+1]);
			}
			if (cycle && points.Length > 1) Line(points[points.Length - 1], points[0]);
			PopColor();
		}
		/// <summary>\copydocref{Polyline(float3[],bool)}</summary>
		/// <param name="color">Color of the object</param>
		[BurstDiscard]
		public void Polyline (float3[] points, Color color) {
			Polyline(points, false, color);
		}
		/// <summary>\copydocref{Polyline(NativeArray<float3>,bool)}</summary>
		/// <param name="color">Color of the object</param>
		public void Polyline (NativeArray<float3> points, bool cycle, Color color) {
			PushColor(color);
			for (int i = 0; i < points.Length - 1; i++) {
				Line(points[i], points[i+1]);
			}
			if (cycle && points.Length > 1) Line(points[points.Length - 1], points[0]);
			PopColor();
		}
		/// <summary>\copydocref{Polyline(NativeArray<float3>,bool)}</summary>
		/// <param name="color">Color of the object</param>
		public void Polyline (NativeArray<float3> points, Color color) {
			Polyline(points, false, color);
		}


		/// <summary>\copydocref{WireBox(float3,float3)}</summary>
		/// <param name="color">Color of the object</param>
		public void WireBox (float3 center, float3 size, Color color) {
			Reserve<Color32, BoxData>();
			Add(Command.WireBox | Command.PushColorInline);
			Add(ConvertColor(color));
			Add(new BoxData { center = center, size = size });
		}
		/// <summary>\copydocref{WireBox(float3,quaternion,float3)}</summary>
		/// <param name="color">Color of the object</param>
		public void WireBox (float3 center, quaternion rotation, float3 size, Color color) {
			PushColor(color);
			PushMatrix(float4x4.TRS(center, rotation, size));
			WireBox(float3.zero, new float3(1, 1, 1));
			PopMatrix();
			PopColor();
		}
		/// <summary>\copydocref{WireBox(Bounds)}</summary>
		public void WireBox (Bounds bounds, Color color) {
			WireBox(bounds.center, bounds.size, color);
		}
		/// <summary>\copydocref{WireMesh(Mesh)}</summary>
		public void WireMesh (Mesh mesh, Color color) {
#if UNITY_2020_1_OR_NEWER
			if (mesh == null) throw new System.ArgumentNullException();
			PushColor(color);

			// Use a burst compiled function to draw the lines
			// This is significantly faster than pure C# (about 5x).
			var meshDataArray = Mesh.AcquireReadOnlyMeshData(mesh);
			var meshData = meshDataArray[0];

			JobWireMesh.JobWireMeshFunctionPointer(ref meshData, ref this);
			meshDataArray.Dispose();
#else
			Debug.LogError("The WireMesh method is only suppored in Unity 2020.1 or later");
#endif
			PopColor();
		}
		/// <summary>\copydocref{WireMesh(NativeArray<float3>,NativeArray<int>)}</summary>
		public void WireMesh (NativeArray<float3> vertices, NativeArray<int> triangles, Color color) {
			PushColor(color);
#if UNITY_2020_1_OR_NEWER
			unsafe {
				JobWireMesh.WireMesh((float3*)vertices.GetUnsafeReadOnlyPtr(), (int*)triangles.GetUnsafeReadOnlyPtr(), vertices.Length, triangles.Length, ref this);
			}
#else
			Debug.LogError("The WireMesh method is only suppored in Unity 2020.1 or later");
#endif
			PopColor();
		}

		/// <summary>\copydocref{Cross(float3,float)}</summary>
		public void Cross (float3 position, float size, Color color) {
			PushColor(color);
			size *= 0.5f;
			Line(position - new float3(size, 0, 0), position + new float3(size, 0, 0));
			Line(position - new float3(0, size, 0), position + new float3(0, size, 0));
			Line(position - new float3(0, 0, size), position + new float3(0, 0, size));
			PopColor();
		}
		/// <summary>\copydocref{Cross(float3,float)}</summary>
		public void Cross (float3 position, Color color) {
			Cross(position, 1, color);
		}
		/// <summary>\copydocref{CrossXZ(float3,float)}</summary>
		[System.Obsolete("Use Draw.xz.Cross instead")]
		public void CrossXZ (float3 position, float size, Color color) {
			PushColor(color);
			size *= 0.5f;
			Line(position - new float3(size, 0, 0), position + new float3(size, 0, 0));
			Line(position - new float3(0, 0, size), position + new float3(0, 0, size));
			PopColor();
		}
		/// <summary>\copydocref{CrossXZ(float3,float)}</summary>
		[System.Obsolete("Use Draw.xz.Cross instead")]
		public void CrossXZ (float3 position, Color color) {
			CrossXZ(position, 1, color);
		}
		/// <summary>\copydocref{CrossXY(float3,float)}</summary>
		[System.Obsolete("Use Draw.xy.Cross instead")]
		public void CrossXY (float3 position, float size, Color color) {
			PushColor(color);
			size *= 0.5f;
			Line(position - new float3(size, 0, 0), position + new float3(size, 0, 0));
			Line(position - new float3(0, size, 0), position + new float3(0, size, 0));
			PopColor();
		}
		/// <summary>\copydocref{CrossXY(float3,float)}</summary>
		[System.Obsolete("Use Draw.xy.Cross instead")]
		public void CrossXY (float3 position, Color color) {
			CrossXY(position, 1, color);
		}
		/// <summary>\copydocref{Bezier(float3,float3,float3,float3)}</summary>
		/// <param name="color">Color of the object</param>
		public void Bezier (float3 p0, float3 p1, float3 p2, float3 p3, Color color) {
			PushColor(color);
			float3 prev = p0;

			for (int i = 1; i <= 20; i++) {
				float t = i/20.0f;
				float3 p = EvaluateCubicBezier(p0, p1, p2, p3, t);
				Line(prev, p);
				prev = p;
			}
			PopColor();
		}


		/// <summary>\copydocref{Arrow(float3,float3)}</summary>
		/// <param name="color">Color of the object</param>
		public void Arrow (float3 from, float3 to, Color color) {
			ArrowRelativeSizeHead(from, to, DEFAULT_UP, 0.2f, color);
		}
		/// <summary>\copydocref{Arrow(float3,float3,float3,float)}</summary>
		/// <param name="color">Color of the object</param>
		public void Arrow (float3 from, float3 to, float3 up, float headSize, Color color) {
			PushColor(color);
			var length_sq = math.lengthsq(to - from);

			if (length_sq > 0.000001f) {
				ArrowRelativeSizeHead(from, to, up, headSize * math.rsqrt(length_sq));
			}
			PopColor();
		}
		/// <summary>\copydocref{ArrowRelativeSizeHead(float3,float3,float3,float)}</summary>
		/// <param name="color">Color of the object</param>
		public void ArrowRelativeSizeHead (float3 from, float3 to, float3 up, float headFraction, Color color) {
			PushColor(color);
			Line(from, to);
			var dir = to - from;

			var normal = math.cross(dir, up);
			// Pick a different up direction if the direction happened to be colinear with that one.
			if (math.all(normal == 0)) normal = math.cross(new float3(1, 0, 0), dir);
			// Pick a different up direction if up=(1,0,0) and thus the above check would have generated a zero vector again
			if (math.all(normal == 0)) normal = math.cross(new float3(0, 1, 0), dir);
			normal = math.normalizesafe(normal) * math.length(dir);

			Line(to, to - (dir + normal) * headFraction);
			Line(to, to - (dir - normal) * headFraction);
			PopColor();
		}


		/// <summary>\copydocref{ArrowheadArc(float3,float3,float,float)}</summary>
		/// <param name="color">Color of the object</param>
		public void ArrowheadArc (float3 origin, float3 direction, float offset, float width, Color color) {
			if (!math.any(direction)) return;
			if (offset < 0) throw new System.ArgumentOutOfRangeException(nameof(offset));
			if (offset == 0) return;
			PushColor(color);

			var rot = Quaternion.LookRotation(direction, DEFAULT_UP);
			PushMatrix(Matrix4x4.TRS(origin, rot, Vector3.one));
			var a1 = math.PI * 0.5f - width * (0.5f * Mathf.Deg2Rad);
			var a2 = math.PI * 0.5f + width * (0.5f * Mathf.Deg2Rad);
			CircleXZInternal(float3.zero, offset, a1, a2);
			var p1 = new float3(math.cos(a1), 0, math.sin(a1)) * offset;
			var p2 = new float3(math.cos(a2), 0, math.sin(a2)) * offset;
			const float sqrt2 = 1.4142f;
			var p3 = new float3(0, 0, sqrt2 * offset);
			Line(p1, p3);
			Line(p3, p2);
			PopMatrix();
			PopColor();
		}
		/// <summary>\copydocref{ArrowheadArc(float3,float3,float,float)}</summary>
		/// <param name="color">Color of the object</param>
		public void ArrowheadArc (float3 origin, float3 direction, float offset, Color color) {
			ArrowheadArc(origin, direction, offset, 60, color);
		}
		/// <summary>\copydocref{WireGrid(float3,quaternion,int2,float2)}</summary>
		/// <param name="color">Color of the object</param>
		public void WireGrid (float3 center, quaternion rotation, int2 cells, float2 totalSize, Color color) {
			PushColor(color);
			cells = math.max(cells, new int2(1, 1));
			PushMatrix(float4x4.TRS(center, rotation, new Vector3(totalSize.x, 0, totalSize.y)));
			int w = cells.x;
			int h = cells.y;
			for (int i = 0; i <= w; i++) Line(new float3(i/(float)w - 0.5f, 0, -0.5f), new float3(i/(float)w - 0.5f, 0, 0.5f));
			for (int i = 0; i <= h; i++) Line(new float3(-0.5f, 0, i/(float)h - 0.5f), new float3(0.5f, 0, i/(float)h - 0.5f));
			PopMatrix();
			PopColor();
		}


		/// <summary>\copydocref{WireRectangle(float3,quaternion,float2)}</summary>
		public void WireRectangle (float3 center, quaternion rotation, float2 size, Color color) {
			WirePlane(center, rotation, size, color);
		}
		/// <summary>\copydocref{WireRectangle(Rect)}</summary>
		[System.Obsolete("Use Draw.xy.WireRectangle instead")]
		public void WireRectangle (Rect rect, Color color) {
			xy.WireRectangle(rect, color);
		}







		/// <summary>\copydocref{WirePlane(float3,float3,float2)}</summary>
		/// <param name="color">Color of the object</param>
		public void WirePlane (float3 center, float3 normal, float2 size, Color color) {
			PushColor(color);
			if (math.any(normal)) {
				WirePlane(center, Quaternion.LookRotation(calculateTangent(normal), normal), size);
			}
			PopColor();
		}
		/// <summary>\copydocref{WirePlane(float3,quaternion,float2)}</summary>
		/// <param name="color">Color of the object</param>
		public void WirePlane (float3 center, quaternion rotation, float2 size, Color color) {
			Reserve<Color32, PlaneData>();
			Add(Command.WirePlane | Command.PushColorInline);
			Add(ConvertColor(color));
			Add(new PlaneData { center = center, rotation = rotation, size = size });
		}



		/// <summary>\copydocref{SolidBox(float3,float3)}</summary>
		/// <param name="color">Color of the object</param>
		public void SolidBox (float3 center, float3 size, Color color) {
			Reserve<Color32, BoxData>();
			Add(Command.Box | Command.PushColorInline);
			Add(ConvertColor(color));
			Add(new BoxData { center = center, size = size });
		}
		/// <summary>\copydocref{SolidBox(Bounds)}</summary>
		/// <param name="color">Color of the object</param>
		public void SolidBox (Bounds bounds, Color color) {
			SolidBox(bounds.center, bounds.size, color);
		}
		/// <summary>\copydocref{SolidBox(float3,quaternion,float3)}</summary>
		/// <param name="color">Color of the object</param>
		public void SolidBox (float3 center, quaternion rotation, float3 size, Color color) {
			PushColor(color);
			PushMatrix(float4x4.TRS(center, rotation, size));
			SolidBox(float3.zero, Vector3.one);
			PopMatrix();
			PopColor();
		}
	}
}
