using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Networking.Server;
using Networking;

public class PortalSetup : MonoBehaviour
{
	public ClientList clientDataList;
	public List<ClientData> ConnectedClients;
	public Color[] playerColours;
	public List<int> validIndex;
	public float collectAmount;
	public float waitTime;
	int currentIndex = 0;
	Color deactiveGrey = new Color32 (120, 120, 120, 255);
	Color deactiveGreyA = new Color32 (120, 120, 120, 0);
	bool collectedFive = false;

	//load portal
	public ParticleSystem Edge;
	public ParticleSystem InSpark;
	public ParticleSystem RandomSparks;
	public ParticleSystem Ring;
	public ParticleSystem Smoke;

	ParticleSystem.MainModule mainEdge;
	ParticleSystem.MainModule mainInSpark;
	ParticleSystem.MainModule mainRandomSparks;
	ParticleSystem.MainModule mainRing;
	ParticleSystem.MainModule mainSmoke;

	IEnumerator ColorChange ()
	{
		while(true){
			mainEdge.startColor = playerColours[validIndex[currentIndex]];
			mainInSpark.startColor = playerColours[validIndex[currentIndex]];
			mainRandomSparks.startColor = playerColours[validIndex[currentIndex]];
			mainRing.startColor = playerColours[validIndex[currentIndex]];
			mainSmoke.startColor = playerColours[validIndex[currentIndex]];

			currentIndex++;

			if(currentIndex >= validIndex.Count){
				currentIndex = 0;
			}
			yield return new WaitForSeconds (5.0f);
		}
	}

	void Update()
	{
		/*if (!collectedFive && (ConnectedClients[0].SceneScore >= collectAmount || ConnectedClients[1].SceneScore >= collectAmount || ConnectedClients[2].SceneScore >= collectAmount || ConnectedClients[3].SceneScore >= collectAmount)){
			collectedFive = true;
			StartCoroutine (ColorChange ());
		}*/
	}

	private void Awake ()
	{
		//get list of players
		ConnectedClients = clientDataList.ConnectedClients;
		playerColours = new Color[ConnectedClients.Count];
		validIndex = new List<int>();
		for (int i = 0; i < ConnectedClients.Count; i++){
			playerColours[i] = ConnectedClients[i].Color;
		}

		//link portal particle sections
		mainEdge = Edge.main;
		mainInSpark = InSpark.main;
		mainRandomSparks = RandomSparks.main;
		mainRing = Ring.main;
		mainSmoke = Smoke.main;

		//set start mode for portal
		mainEdge.startColor = deactiveGrey;
		mainInSpark.startColor = deactiveGreyA;
		mainRandomSparks.startColor = deactiveGreyA;
		mainRing.startColor = deactiveGrey;
		mainSmoke.startColor = deactiveGreyA;
	}
}
