using UnityEngine;
using System.Collections;

public class TrainCarManagerScript : MonoBehaviour {

	private int trainLadyLocation = 0;
	private float timeSinceLocationChange = 0.0f;

	private GameObject[] garbageSpawners;
	private GameObject garbageSpawner1;
	private GameObject garbageSpawner2;
	private GameObject garbageSpawner3;

	private GameObject[] roomDoors;
	private GameObject[] roomDoors1;
	private GameObject[] roomDoors2;
	private GameObject[] roomDoors3;

	private GameObject[] cabinBenches;
	private GameObject[] cabinBenches1;
	private GameObject[] cabinBenches2;
	private GameObject[] cabinBenches3;

	private GameObject[] cabinWindows;
	private GameObject[] cabinWindows1;
	private GameObject[] cabinWindows2;
	private GameObject[] cabinWindows3;

	private GameObject[] WCDoors;
	private GameObject WCDoor1;
	private GameObject WCDoor2;
	private GameObject WCDoor3;

	private GameObject[] trainCarDoors;

	// Punishment and Fear point tallies
	private int piecesOfGarbageOnFloor = 0;
	private int windowsOpen = 0;
	private int benchesDown = 0;
	private int noisyCabins = 0;
	private int openTrainCarDoors = 0;

	private GameObject thePlayer;

	// Random train car mess timer stuff
	private float minTimeToMess = 20.0f;
	private float maxTimeToMess = 35.0f;
	private float timeToNextMessCounter = 0.0f;


	// Use this for initialization
	void Awake () {
	
		thePlayer = GameObject.FindGameObjectWithTag("Player");
		if(!thePlayer){
			Debug.LogError("TrainCarManager:: Cannot find Player!");
		}

		//TODO: find garbage spawner for each car
		// find room doors for each car
		// windows for each car
		// benches for each car
		// water closet door for each car


		/// Garbage Spawners ///
		garbageSpawners = GameObject.FindGameObjectsWithTag("GarbageSpawner");
		//Debug.Log ("Found " + garbageSpawners.Length + " Garbage Spawners");

		foreach(GameObject go in garbageSpawners){

			if(go.GetComponent<GarbageSpawnerScript>().trainCarNumber == 1){
				garbageSpawner1 = go;
			}else if(go.GetComponent<GarbageSpawnerScript>().trainCarNumber == 2){
				garbageSpawner2 = go;
			}else if(go.GetComponent<GarbageSpawnerScript>().trainCarNumber == 3){
				garbageSpawner3 = go;
			}

		}

		if(!garbageSpawner1 || !garbageSpawner2 || !garbageSpawner3){
			Debug.LogError("Cannot find one of the garbage spawners!");
		}

		/// Room Doors ///
		roomDoors = GameObject.FindGameObjectsWithTag("RoomDoor");
		//Debug.Log ("Found " + roomDoors.Length + " Room Doors");

		roomDoors1 = new GameObject[0];
		roomDoors2 = new GameObject[0];
		roomDoors3 = new GameObject[0];

		foreach(GameObject go in roomDoors){

			if(go.GetComponent<RoomDoorScript>().trainCarNumber == 1){

				System.Array.Resize(ref roomDoors1, roomDoors1.Length + 1);
				roomDoors1[roomDoors1.Length-1] = go;
				
			}else if(go.GetComponent<RoomDoorScript>().trainCarNumber == 2){
				
				System.Array.Resize(ref roomDoors2, roomDoors2.Length + 1);
				roomDoors2[roomDoors2.Length-1] = go;
				
			}else if(go.GetComponent<RoomDoorScript>().trainCarNumber == 3){
				
				System.Array.Resize(ref roomDoors3, roomDoors3.Length + 1);
				roomDoors3[roomDoors3.Length-1] = go;
				
			}

		}

		/// Cabin Benches ///
		cabinBenches = GameObject.FindGameObjectsWithTag("CabinBench");
		//Debug.Log ("Found " + cabinBenches.Length + " Cabin Benches");
		
		cabinBenches1 = new GameObject[0];
		cabinBenches2 = new GameObject[0];
		cabinBenches3 = new GameObject[0];

		foreach(GameObject go in cabinBenches){
			
			if(go.GetComponent<CabinBenchScript>().trainCarNumber == 1){
				
				System.Array.Resize(ref cabinBenches1, cabinBenches1.Length + 1);
				cabinBenches1[cabinBenches1.Length-1] = go;
				
			}else if(go.GetComponent<CabinBenchScript>().trainCarNumber == 2){
				
				System.Array.Resize(ref cabinBenches2, cabinBenches2.Length + 1);
				cabinBenches2[cabinBenches2.Length-1] = go;
				
			}else if(go.GetComponent<CabinBenchScript>().trainCarNumber == 3){
				
				System.Array.Resize(ref cabinBenches3, cabinBenches3.Length + 1);
				cabinBenches3[cabinBenches3.Length-1] = go;
				
			}
			
		}

		/// Cabin Windows ///
		cabinWindows = GameObject.FindGameObjectsWithTag("CabinWindow");
		//Debug.Log ("Found " + cabinWindows.Length + " Cabin Windows");

		cabinWindows1 = new GameObject[0];
		cabinWindows2 = new GameObject[0];
		cabinWindows3 = new GameObject[0];

		foreach(GameObject go in cabinWindows){
			
			if(go.GetComponent<TrainCarWindowScript>().trainCarNumber == 1){
				
				System.Array.Resize(ref cabinWindows1, cabinWindows1.Length + 1);
				cabinWindows1[cabinWindows1.Length-1] = go;
				
			}else if(go.GetComponent<TrainCarWindowScript>().trainCarNumber == 2){
				
				System.Array.Resize(ref cabinWindows2, cabinWindows2.Length + 1);
				cabinWindows2[cabinWindows2.Length-1] = go;
				
			}else if(go.GetComponent<TrainCarWindowScript>().trainCarNumber == 3){
				
				System.Array.Resize(ref cabinWindows3, cabinWindows3.Length + 1);
				cabinWindows3[cabinWindows3.Length-1] = go;
				
			}

		}

		/// WC Doors ///
		WCDoors = GameObject.FindGameObjectsWithTag("WCDoor");
		//Debug.Log ("Found " + WCDoors.Length + " WC Doors");
		
		foreach(GameObject go in WCDoors){
			
			if(go.GetComponent<WaterClosetDoorScript>().trainCarNumber == 1){
				WCDoor1 = go;
			}else if(go.GetComponent<WaterClosetDoorScript>().trainCarNumber == 2){
				WCDoor2 = go;
			}else if(go.GetComponent<WaterClosetDoorScript>().trainCarNumber == 3){
				WCDoor3 = go;
			}
			
		}
		
		if(!WCDoor1 || !WCDoor2 || !WCDoor3){
			Debug.LogError("Cannot find one of the WC Doors!");
		}

		trainCarDoors = GameObject.FindGameObjectsWithTag("CarDoor");

		if(trainCarDoors.Length == 0){
			Debug.LogError ("Failed to find train car doors!");
		}

		// set timer to random value
		timeToNextMessCounter = Random.Range(minTimeToMess, maxTimeToMess);

	}//Awake

	void Start(){

		// Create initial mess
		makeRandomTrainCarNoise();

	}
	
	// Update is called once per frame
	void Update () {

		timeSinceLocationChange += Time.deltaTime;

		/*
		if(Input.GetKeyUp(KeyCode.R)){

			makeRandomTrainCarNoise();

			//messUpTrainCar(1);

		}
		*/

		/*
		if(Input.GetKeyUp (KeyCode.T)){
			resetTrainCarsForNextLeg();
		}
		*/

		// Count messed up items by category
		piecesOfGarbageOnFloor = 0;
		windowsOpen = 0;
		benchesDown = 0;
		noisyCabins = 0;
		openTrainCarDoors = 0;

		GameObject[] garbageObjects = GameObject.FindGameObjectsWithTag("Garbage");
		piecesOfGarbageOnFloor = garbageObjects.Length;

		foreach(GameObject bench in cabinBenches){
			if(bench.GetComponent<CabinBenchScript>().isBenchLowered()){
				benchesDown++;
			}
		}

		foreach(GameObject window in cabinWindows){
			if(window.GetComponent<TrainCarWindowScript>().isWindowDown()){
				windowsOpen++;
			}
		}

		foreach(GameObject door in roomDoors){
			if(door.GetComponent<RoomDoorScript>().isDoorNoisy()){
				noisyCabins++;
			}
		}

		foreach(GameObject traincardoor in trainCarDoors){
			if(traincardoor.GetComponent<TrainCarDoorScript>().isTrainCarDoorOpen()){
				openTrainCarDoors++;
			}
		}

		// Mess timer stuff
		timeToNextMessCounter -= Time.deltaTime;

		if(timeToNextMessCounter <= 0.0f && gameObject.GetComponent<CountdownTimerScript>().getSecondsRemaining() > 60){
			//Debug.Log("Mess Timer expired");
			makeRandomTrainCarNoise();
			timeToNextMessCounter = Random.Range(minTimeToMess, maxTimeToMess);
		}
	
	}


	void makeRandomTrainCarNoise(){

		// Only make noise and other things happen in OTHER train cars
		if(trainLadyLocation == 1){

			if(Random.Range(0,2) == 1){
				//Debug.Log("messing up train car 2");
				messUpTrainCar(2);
			}else{
				//Debug.Log("messing up train car 3");
				messUpTrainCar(3);
			}


		}else if(trainLadyLocation == 2){

			if(Random.Range(0,2) == 1){
				//Debug.Log("messing up train car 1");
				messUpTrainCar(1);
			}else{
				//Debug.Log("messing up train car 3");
				messUpTrainCar(3);
			}


		}else{

			if(Random.Range(0,2) == 1){
				//Debug.Log("messing up train car 1");
				messUpTrainCar(1);
			}else{
				//Debug.Log("messing up train car 2");
				messUpTrainCar(2);
			}


		}

	}

	void messUpTrainCar(int trainCarNumber){

		if(trainCarNumber == 1){

			garbageSpawner1.GetComponent<GarbageSpawnerScript>().spawnGarbage();
			thePlayer.GetComponent<PlayerInteractionScript>().increaseFearBy(1.0f);

			int randomRoomIndex = Random.Range(0, roomDoors1.Length);
			roomDoors1[randomRoomIndex].GetComponent<RoomDoorScript>().makeDoorNoisy();

			int randomBenchIndex = Random.Range(0, cabinBenches1.Length);
			cabinBenches1[randomBenchIndex].GetComponent<CabinBenchScript>().lowerBench();
			thePlayer.GetComponent<PlayerInteractionScript>().increaseFearBy(1.0f);

			int randomWindowIndex = Random.Range(0, cabinWindows1.Length);
			cabinWindows1[randomWindowIndex].GetComponent<TrainCarWindowScript>().openWindow();
			thePlayer.GetComponent<PlayerInteractionScript>().increaseFearBy(1.0f);

			if(Random.Range(0,2) == 1){
				WCDoor1.GetComponent<WaterClosetDoorScript>().makeVacant(true);
			}else{
				WCDoor1.GetComponent<WaterClosetDoorScript>().makeVacant(false);
			}

		}else if(trainCarNumber == 2){

			garbageSpawner2.GetComponent<GarbageSpawnerScript>().spawnGarbage();
			thePlayer.GetComponent<PlayerInteractionScript>().increaseFearBy(1.0f);
			
			int randomRoomIndex = Random.Range(0, roomDoors2.Length);
			roomDoors2[randomRoomIndex].GetComponent<RoomDoorScript>().makeDoorNoisy();
			
			int randomBenchIndex = Random.Range(0, cabinBenches2.Length);
			cabinBenches2[randomBenchIndex].GetComponent<CabinBenchScript>().lowerBench();
			thePlayer.GetComponent<PlayerInteractionScript>().increaseFearBy(1.0f);
			
			int randomWindowIndex = Random.Range(0, cabinWindows2.Length);
			cabinWindows2[randomWindowIndex].GetComponent<TrainCarWindowScript>().openWindow();
			thePlayer.GetComponent<PlayerInteractionScript>().increaseFearBy(1.0f);
			
			if(Random.Range(0,2) == 1){
				WCDoor2.GetComponent<WaterClosetDoorScript>().makeVacant(true);
			}else{
				WCDoor2.GetComponent<WaterClosetDoorScript>().makeVacant(false);
			}

		}else{

			garbageSpawner3.GetComponent<GarbageSpawnerScript>().spawnGarbage();
			thePlayer.GetComponent<PlayerInteractionScript>().increaseFearBy(1.0f);
			
			int randomRoomIndex = Random.Range(0, roomDoors3.Length);
			roomDoors3[randomRoomIndex].GetComponent<RoomDoorScript>().makeDoorNoisy();
			
			int randomBenchIndex = Random.Range(0, cabinBenches3.Length);
			cabinBenches3[randomBenchIndex].GetComponent<CabinBenchScript>().lowerBench();
			thePlayer.GetComponent<PlayerInteractionScript>().increaseFearBy(1.0f);
			
			int randomWindowIndex = Random.Range(0, cabinWindows3.Length);
			cabinWindows3[randomWindowIndex].GetComponent<TrainCarWindowScript>().openWindow();
			thePlayer.GetComponent<PlayerInteractionScript>().increaseFearBy(1.0f);
			
			if(Random.Range(0,2) == 1){
				WCDoor3.GetComponent<WaterClosetDoorScript>().makeVacant(true);
			}else{
				WCDoor3.GetComponent<WaterClosetDoorScript>().makeVacant(false);
			}

		}

	}

	// TODO: Reset all things for next leg
	public void resetTrainCarsForNextLeg(){
		
		GameObject[] garbageObjects = GameObject.FindGameObjectsWithTag("Garbage");
		foreach(GameObject garbage in garbageObjects){
			DestroyObject(garbage);
		}
		
		foreach(GameObject bench in cabinBenches){
			if(bench.GetComponent<CabinBenchScript>().isBenchLowered()){
				bench.GetComponent<CabinBenchScript>().raiseBench();
			}
		}
		
		foreach(GameObject window in cabinWindows){
			if(window.GetComponent<TrainCarWindowScript>().isWindowDown()){
				window.GetComponent<TrainCarWindowScript>().closeWindow();
			}
		}
		
		foreach(GameObject door in roomDoors){
			if(door.GetComponent<RoomDoorScript>().isDoorNoisy()){
				door.GetComponent<RoomDoorScript>().makeDoorQuiet();
			}
		}
		
	}

	public int getTrainLadyLocation(){
		return trainLadyLocation;
	}

	public void setTrainLadyLocation(int location){
		trainLadyLocation = location;
		timeSinceLocationChange = 0.0f;
	}

	public int getGarbageCount(){
		return piecesOfGarbageOnFloor;
	}

	public int getOpenWindowsCount(){
		return windowsOpen;
	}

	public int getLoweredBenchesCount(){
		return benchesDown;
	}

	public int getNoisyCabinCount(){
		return noisyCabins;
	}

	public int getOpenTrainCarDoorsCount(){
		return openTrainCarDoors;
	}

	//DEBUG
	/*
	void OnGUI(){
		GUI.color = Color.white;
		GUI.Label(new Rect (250.0f, 250.0f, 128, 24), "MC=" + timeToNextMessCounter);
	}
	*/

}
