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

public class scrTaskManager : MonoBehaviour {

	[System.NonSerialized] public List<Transform> liTasks = new List<Transform>(); //A list of Tasks in the game
	[System.NonSerialized] public List<Transform> liTasksP1 = new List<Transform>(); //A list of the Tasks for P1
	[System.NonSerialized] public List<Transform> liTasksP2 = new List<Transform>(); //A list of the Tasks for P2

	public Transform trPissTask;
	public Transform trP1List;
	public Transform trP2List;
	public scrPlayer scrPlayer1;
	public scrPlayer scrPlayer2;
	public Sprite sprRedArrow;

	public GameObject goP1Wins;
	public GameObject goP2Wins;

	// Use this for initialization
	void Start () {
	
		doAssignTasks();

	}

	#region doAssignTasks
	void doAssignTasks() {

		int _intTasksForP1 = 0;
		int _intTasksForP2 = 0;

		int _intNextTaskIsForWho = 0; //0 = P1, 1 = P2

		for (int i = 0; i < 8 && i < liTasks.Count; i++) {

			_intNextTaskIsForWho = Random.Range(0,2); 

			if ( _intNextTaskIsForWho == 0 && _intTasksForP1 >= 4 ) {

				_intNextTaskIsForWho = 1;

			} else if ( _intNextTaskIsForWho == 1 && _intTasksForP2 >= 4 ) {

				_intNextTaskIsForWho = 0;

			}

			if ( _intNextTaskIsForWho == 0 ) {

				_intTasksForP1++;

				liTasks[i].tag = "Player1";
				liTasks[i].FindChild("arrow").FindChild("Image").GetComponent<SpriteRenderer>().sprite = sprRedArrow;
				liTasksP1.Add(liTasks[i]);

				trP1List.GetChild(_intTasksForP1).GetComponent<Text>().text = liTasks[i].GetComponent<scrTask>().strTaskDescr;

			} else {

				_intTasksForP2++;

				liTasks[i].tag = "Player2";
				//liTasks[i].GetComponent<SpriteRenderer>().color = Color.blue;
				liTasksP2.Add(liTasks[i]);

				trP2List.GetChild(_intTasksForP2).GetComponent<Text>().text = liTasks[i].GetComponent<scrTask>().strTaskDescr;

			}


		}

		liTasksP1.Add(trPissTask);
		liTasksP2.Add(trPissTask);
		trP1List.GetChild(5).GetComponent<Text>().text = trPissTask.GetComponent<scrTask>().strTaskDescr;
		trP2List.GetChild(5).GetComponent<Text>().text = trPissTask.GetComponent<scrTask>().strTaskDescr;

	}
	#endregion

	public void doUpdateLists() {

		int _intCompletedP1 = 0;
		int _intCompletedP2 = 0;

		for (int i = 0; i < liTasksP1.Count; i++) {

			if ( scrPlayer1.liCompletedTasks.Contains(liTasksP1[i]) ) {

				_intCompletedP1++;
				trP1List.GetChild(i+1).GetComponent<Text>().color = Color.green;

			} else {

				trP1List.GetChild(i+1).GetComponent<Text>().color = Color.white;

			}

		}

		for (int i = 0; i < liTasksP2.Count; i++) {

			if ( scrPlayer2.liCompletedTasks.Contains(liTasksP2[i]) ) {

				_intCompletedP2++;
				trP2List.GetChild(i+1).GetComponent<Text>().color = Color.green;

			} else {

				trP2List.GetChild(i+1).GetComponent<Text>().color = Color.white;

			}

		}

		if ( _intCompletedP1 >= 5 ) {

			Time.timeScale = 0;
			goP1Wins.SetActive(true);

		} else if ( _intCompletedP2 >= 5 ) {

			Time.timeScale = 0;
			goP2Wins.SetActive(true);

		}

	}

	public void doReset() {

		Time.timeScale = 1;
		Application.LoadLevel(Application.loadedLevel);

	}

}
