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

public class DiscoBall : MonoBehaviour {

	Vector3 discoRot, c, a;
	[HideInInspector]
	public List<Light> lights;
	int[] signs = { 1, -1, 1 };

	// Use this for initialization
	void Start () {
		c = new Vector3 (0f, 0.3f, 0.6f);
		a = new Vector3 (60f, 45f, 52.5f);

		for (int i = 0; i < lights.Count; i++) {
			lights [i].range += Mathf.Round (Vector3Average (transform.localScale)) * 5f - 5f;
		}
	}
	
	// Update is called once per frame
	void Update () {
		discoRot += new Vector3 (0f, 45f * Time.deltaTime, 0f);
		transform.localEulerAngles = discoRot;

		if (lights.Count == 3) {
			lights [0].color = Color.HSVToRGB (c.x, 1, 1, true);
			lights [1].color = Color.HSVToRGB (c.y, 1, 1, true);
			lights [2].color = Color.HSVToRGB (c.z, 1, 1, true);

			lights [0].transform.localEulerAngles = new Vector3 (a.x, lights [0].transform.localEulerAngles.y, lights [0].transform.localEulerAngles.z);
			lights [1].transform.localEulerAngles = new Vector3 (a.y, lights [1].transform.localEulerAngles.y, lights [1].transform.localEulerAngles.z);
			lights [2].transform.localEulerAngles = new Vector3 (a.z, lights [2].transform.localEulerAngles.y, lights [2].transform.localEulerAngles.z);
		}
		Vector3 colorHold = new Vector3 (SpinColorWheel(c.x), SpinColorWheel(c.y), SpinColorWheel(c.z));
		Vector3 angleHold = new Vector3 (UpdateAngle(a.x, 0), UpdateAngle(a.y, 1), UpdateAngle(a.z, 2));
		c = colorHold;
		a = angleHold;
	}

	float SpinColorWheel (float color) {
		color = (color >= 1f) ? 0 : color + 0.0025f;
		return color;
	}

	float UpdateAngle (float angle, int spot) {
		if (angle >= 60f || angle <= 45f)
			signs [spot] = -signs [spot];

		angle += signs [spot] * 0.25f;

		return angle;
	}

	float Vector3Average (Vector3 myVector) {
		float average = myVector.x + myVector.y + myVector.z;
		average = average / 3f;
		return average;
	}
}
