using UnityEngine;
using System.Collections;

public abstract class Thinkie : MonoBehaviour {

	public enum ThinkieType{GOOD, BAD, UGLY};
	public ThinkieType type;

	public GameObject corpse;

	private float speed;
	private float distance;
	private float startTime;

	private bool isArrived;
	private bool goingToExit;
	public bool burned;

	void Awake(){
		speed = Random.Range (Constants.THINKIE_MIN_SPEED, Constants.THINKIE_MAX_SPEED);
		startTime = Time.time;
		distance = transform.position.magnitude;
		isArrived = false;
		goingToExit = false;
		burned = false;
	}

	void FixedUpdate () {
		if (type == ThinkieType.UGLY)
			return;

		if (!isArrived) {
			float distCovered = (Time.time - startTime) * speed;
			transform.position = Vector3.Lerp (transform.position, Vector3.zero, distCovered / distance);
		} else if(goingToExit){
			float distCovered = (Time.time - startTime) * speed;
			transform.position = Vector3.Lerp (Constants.EXIT_TUNNEL_START, Constants.EXIT_TUNNEL_END, distCovered / distance);
		}
	}

	public abstract void BulletHit ();
	public abstract void IdeaQueueReaction ();

	public void MakeInvisible(){
		collider2D.enabled = false;
		renderer.enabled = false;
	}

	public void SetArrived(bool b){
		isArrived = b;
	}

	public void GoToExit(){
		GetComponent<SpriteRenderer> ().sortingOrder = 5;
		transform.eulerAngles = new Vector3 (0.0f, 0.0f, -90.0f);
		transform.localScale = new Vector3 (0.5f, 0.5f, 0.5f);
		startTime = Time.time;
		distance = Vector3.Distance (Constants.EXIT_TUNNEL_START, Constants.EXIT_TUNNEL_END);
		goingToExit = true;
		MakeVisible ();
		collider2D.enabled = false;
	}

	public void LaserHit(){
		isArrived = true;
		StartCoroutine ("Burn");
	}

	IEnumerator Burn(){
		burned = true;
		GetComponent<Animator> ().SetBool ("isLazer", true);
		yield return new WaitForSeconds (0.25f);
		Destroy (this.gameObject);
	}


	public void Boom(){
		Instantiate (corpse, transform.position, Quaternion.identity);
		transform.GetChild (0).gameObject.SetActive (true);
		/*
		transform.GetChild(0).GetChild(0).GetComponent<ParticleRenderer>().sortingOrder = 50;
		transform.GetChild(0).GetChild(1).GetComponent<ParticleRenderer>().sortingOrder = 50;
		transform.GetChild(0).GetChild(2).GetComponent<ParticleRenderer>().sortingOrder = 50;
		transform.GetChild(0).GetChild(3).GetComponent<ParticleRenderer>().sortingOrder = 50;
		*/
		GetComponent<SpriteRenderer> ().enabled = false;
		isArrived = true;
		StartCoroutine ("DestroyThinkie");
	}

	IEnumerator DestroyThinkie(){
		yield return new WaitForSeconds (3.0f);
		Destroy (this.gameObject);
	}

	public bool isGoingToExit(){
		return goingToExit;
	}

	public void MakeVisible(){
		collider2D.enabled = true;
		renderer.enabled = true;
	}

	void OnTriggerEnter2D(Collider2D other){
		if(other.tag == "Bullet"){
			Bullet b = other.GetComponent<Bullet>();
			BulletHit();
			int i = Random.Range (0, GameObject.FindGameObjectWithTag("Dipsy").GetComponent<Dipsy>().sfxShoot.Length);
			GameObject.FindGameObjectWithTag("Dipsy").GetComponent<Dipsy>().sfxShoot[i].Play ();
			b.DestroyBullet();
		} else if (other.tag == "IdeaQueue") {
			IdeaQueueReaction();
		} else if(other.tag == "LaserBeam"){
			LaserHit();
		}
	}
}
