using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Uses a Line renderer to display a Trajectory
/// </summary>
public class TrajectoryViewer : MonoBehaviour
{
	[SerializeField] private LineRenderer _lineRenderer;
	[SerializeField] private int _nbPoints = 100;
	[SerializeField] private float _scale = 1f;

	private Transform _transform;
	private Vector3[] _points;

	public void Awake()
	{
		_transform = transform;
		Initialize();
	}

	public void Initialize()
	{
		_lineRenderer.positionCount = _nbPoints;
		_points = new Vector3[_nbPoints];
		for (int i = 0; i < _nbPoints; i++)
		{
			float time = (float) i / (float) _nbPoints;
			Vector3 position = new Vector3(0f, time, -1f)* _scale + _transform.position;
			_points[i] = position;
		}

		_lineRenderer.SetPositions(_points);
	}

	public void SetScale(float scale)
	{
		_scale = scale;
	}

	public void HideTrajectory()
	{
		_lineRenderer.enabled = false;
	}

	public void DisplayTrajectory(Trajectory trajectory)
	{
		for (int i = 0; i < _nbPoints; i++)
		{
			float time = (float) i / (float) _nbPoints;
			float x = trajectory.Evaluate(time);
			Vector3 position = new Vector3(x, time, -1f) * _scale + _transform.position;
			_points[i] = position;
		}

		_lineRenderer.enabled = true;
		_lineRenderer.SetPositions(_points);
	}

	public void DisplaySinusoid(Sinusoid sinusoid)
	{
		for (int i = 0; i < _nbPoints; i++)
		{
			float time = (float) i / (float) _nbPoints;
			float x = sinusoid.GetPosition(time);
			Vector3 position = new Vector3(x, time, -1f) * _scale + _transform.position;
			_points[i] = position;
		}

		_lineRenderer.enabled = true;
		_lineRenderer.SetPositions(_points);
	}
}
