using UnityEngine;
using UnityEngine.InputSystem;

/// <summary>
/// Commands the Trajectory Generator
/// </summary>
public class TrajectoryCommand : MonoBehaviour
{
	[SerializeField] private TrajectoryViewer _trajectoryViewer = null;
	[SerializeField] private TrajectoryGenerator _trajectoryGenerator = null;

	private Sinusoid _sinusoidA = null;
	private Sinusoid _sinusoidB = null;
	private Sinusoid _sinusoidC = null;

	private int _nbCurves = 0;

	public int NbCurves => _nbCurves;

	public TrajectoryGenerator TrajectoryGenerator => _trajectoryGenerator;

	public void Start()
	{
		ResetTrajectory();
	}

	public void SetSinusoids(Sinusoid sinusoidA, Sinusoid sinusoidB, Sinusoid sinusoidC)
	{
		_sinusoidA = sinusoidA;
		_sinusoidB = sinusoidB;
		_sinusoidC = sinusoidC;
	}

	public void ResetTrajectory()
	{
		_nbCurves = 0;
		_trajectoryGenerator.CreateTrajectory();

		_trajectoryViewer.Initialize();
		_trajectoryViewer.HideTrajectory();
		// _trajectoryViewer.DisplayTrajectory(_trajectoryGenerator.CurrentTrajectory);
	}

	public void AddA()
	{
		if (CanAddCurve() == false)
		{
			return;
		}

		_trajectoryGenerator.AddCurve(_sinusoidA);
		ProcessAddedCurve();
	}

	public void AddB()
	{
		if (CanAddCurve() == false)
		{
			return;
		}

		_trajectoryGenerator.AddCurve(_sinusoidB);
		ProcessAddedCurve();
	}

	public void AddC()
	{
		if (CanAddCurve() == false)
		{
			return;
		}

		_trajectoryGenerator.AddCurve(_sinusoidC);
		ProcessAddedCurve();
	}

	public bool CanAddCurve()
	{
		return _nbCurves < 5;
	}

	public void ProcessAddedCurve()
	{
		_nbCurves++;
		_trajectoryViewer.DisplayTrajectory(_trajectoryGenerator.CurrentTrajectory);
	}
}
