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

public class Wave : MonoBehaviour {
	public static Wave instance;
	public GameObject[] tops = new GameObject[3];

	//private fields
	private Rigidbody rgd;
	private Vector3 startPos;

	//Initialization
	void Start () {
		instance = this;
		startPos = transform.position;
		rgd = GetComponent<Rigidbody> ();
	}
		
	public void Stop(){
		transform.position = startPos;
		Turn (3);
		rgd.velocity = Vector3.zero;
	}

	//called by UI to start the game
	public void Play(){
		rgd.velocity = Vector3.forward * 10;
	}

	//Turn the wave when hit montains. 0 for right, 1 for left, 3 for front;
	private void Turn(int side){
		if (side == 0) {
			tops [0].SetActive (false);
			tops [1].SetActive (true);
			rgd.velocity = Vector3.right * 10;
		} else if (side == 1) {
			tops [0].SetActive (false);
			tops [2].SetActive (true);
			rgd.velocity = Vector3.left * 10;
		} else {
			tops [0].SetActive (true);
			tops [1].SetActive (false);
			tops [2].SetActive (false);
			rgd.velocity = Vector3.forward * 10;
		}
	}
		

	public void OnCollisionEnter(Collision col){
		Collider coll= col.collider;
		if (coll.tag == "Mountain") {
			if (coll.name == "RightMountain")
				Turn (0);
			else if (coll.name == "LeftMountain")
				Turn (1);
			else
				Turn (3);
			coll.gameObject.SetActive (false);
			StartCoroutine (Reactivate (coll.gameObject));

		}
	}

	IEnumerator Reactivate(GameObject mountain){
		yield return new WaitForSeconds (5);
		mountain.SetActive (true);
	}

}
