using System.Collections.Generic;
using UnityEngine;

public struct PointData
{
	public Vector3 position;
	public Color color;
}

public class PointHistory : MonoBehaviour
{
	public List<PointData> points;
	public int depth = 100;
	public int interval = 2;
	private int intervalCountdown = 0;
	private Rigidbody2D rb2;

	void FixedUpdate()
	{
		if (intervalCountdown <= 0)
		{
			CapturePoint();
			intervalCountdown = interval-1;
		}
		else
		{
			intervalCountdown--;
		}
	}

	private void CapturePoint()
	{
		PointData dat = new PointData
		{
			position = transform.position,
			color = GetEnergyColor()
		};

		points.Insert(0, dat);
		if (points.Count > depth)
		{
			// 102 count, 100 depth:  need to delete 100, 101
			// 100, 2
			points.RemoveRange(depth, (points.Count - depth));
		}
	}

	private Color GetEnergyColor()
	{
		float energyFactor = Mathf.Min(1.0f, (EnergyTracker.totalEnergy-20) / 100f);
		float velocityFactor = Mathf.Min(1.0f, rb2.velocity.magnitude / 30f);

		float hue = energyFactor;
		float sat = (energyFactor < 0.1f) ? 0f : (energyFactor - 0.1f) / (0.9f);
		float value = 0.5f + 0.25f * velocityFactor + 0.25f * energyFactor;

		return Color.HSVToRGB(hue, sat, value);
	}

	void Start()
    {
		points = new List<PointData>();
		rb2 = GetComponent<Rigidbody2D>();
	}
}
