using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class Curve : MonoBehaviour
{
	[SerializeField] private bool _update = false;
	[SerializeField] private bool _generatePoints = false;
	[SerializeField] private bool _addCurve = false;

	[SerializeField] private List<Vector3> _points;
	private int _nbPoints = 100;




	public List<Vector3> Points => _points;
	public int NbPoints => _nbPoints;

	// public void SetPoints()
	// {
	// 	// _points = new List<Vector3>(nbPoint);
	// }

	public void OnValidate()
	{
		// if (_generatePoints)
		// {
		// 	_generatePoints = false;
		// 	GeneratePoints();
		// }
		//
		// if (_addCurve)
		// {
		// 	_addCurve = false;
		// 	UpdateCurve();
		// }
	}

	public void Update()
	{
		if (_update == false)
		{
			return;
		}

		if (_points == null)
		{
			return;
		}

		DrawCurve();
	}

	public void GeneratePoints(int nbPoints)
	{
		_nbPoints = nbPoints;
		if (_points != null)
		{
			_points.Clear();
		}

		_points = new List<Vector3>(_nbPoints);
		for (int i = 0; i < _nbPoints; i++)
		{
			float time = (float)i / (float)_nbPoints;
			float x = GetLinearPosition(time);
			_points.Add(new Vector3(x, time * 2f, 0f));
		}
	}

	public void UpdateCurve()
	{
		for (int i = 0; i < _nbPoints; i++)
		{
			float time = (float)i / (float)_nbPoints;

			float x = GetPosition(time);
			_points[i] = new Vector3(x, time * 2f, 0f);
		}
	}

	public void DrawCurve()
	{
		Vector3 lastPosition = Vector3.zero;

		for (int i = 1; i < _nbPoints; i++)
		{
			Vector3 point = _points[i];

			Vector3 dir = point - lastPosition;

			Debug.DrawRay(lastPosition, dir, Color.green);
			lastPosition = point;
		}
	}

	public float GetLinearPosition(float time)
	{
		return time;
	}

	public float GetPosition(float time)
	{
		return Mathf.Sin(time * Mathf.PI * 2f);
	}


}
