using UnityEngine;
using System.Collections;


public class Wave : MonoBehaviour
{

	 float location;
	public float height;


	 float initialDistance;
	 bool lerp;

	//public bool dontDoAnything;

	[Range (-1, 1)]
	public int direction;

	public float speed;



	float easedHeight;
	float originalH;

	BoxCollider2D boxCollider2DComp;
	 GameObject otherWave;

	void Start ()
	{
		location = transform.position.x;
		height = transform.localScale.y;
		easedHeight = transform.localScale.y;
        transform.localScale = new Vector3(direction * easedHeight, easedHeight, 1);

    }

	void Awake ()
	{
		boxCollider2DComp = GetComponent<BoxCollider2D> ();
    }

	void Update ()
	{


		if (transform.localScale.y <= 0) {
			DestroyThis ();
		}


		location = location + speed * direction * Time.deltaTime;
		transform.position = new Vector3 (location, transform.position.y, transform.position.z);

		if (lerp == false) {
			float targetX = height;
			float dx = targetX - easedHeight;
			easedHeight += dx * 2 * Time.deltaTime;

			transform.localScale = new Vector3 (direction * easedHeight, easedHeight , 1);
		}

//		if (lerp == true) {
//			float progress = 0;
//		
//				 progress = 1 - ((Mathf.Abs(transform.position.x - otherWave.transform.position.x)) / initialDistance);
//			
//
//			
//			if (progress < 0.9f) {
//				//transform.localScale = new Vector3 (1, Mathf.Lerp (height, 0, progress), 1);
//                transform.localScale = new Vector3(1, Mathf.Lerp(originalH, 0, progress), 1);
//            } else  {
//				Destroy (gameObject);
//			}
//
//		}
	}






	void OnTriggerEnter2D (Collider2D other)
	{
		if (other.gameObject.tag == gameObject.tag) {

			//if (!dontDoAnything)
			//{
			otherWave = other.gameObject;
			Wave otherWaveScript = other.GetComponent<Wave> ();
			float otherH = otherWaveScript.height;

			int relaDir = direction / otherWaveScript.direction;
			//if (direction == otherWaveScript.direction) {

			//}

			print (height + name + otherH + otherWave.name);



			if (otherWaveScript.height > height) {
				//   otherWaveScript.dontDoAnything = true;
				ShitBro (otherWaveScript, relaDir);
                
			} else if (otherWaveScript.height == height) {

				if (relaDir < 0) {
					print ("ahahhhhhhhhhhh");
					//  boxCollider2DComp.enabled = false;
					initialDistance = Vector3.Distance (transform.position, otherWave.transform.position);
					originalH = height;
					LerpToZero ();

					// height = 0;
					//  that.dontDoAnything = false;
					print ("bye bro");
				} else {
					if (speed < otherWaveScript.speed) {
						//print ("
						ShitBro (otherWaveScript, relaDir);
					}
				}
					
			}

		}
	}

	public void LerpToZero ()
	{
		lerp = true;

		iTween.ScaleTo (gameObject, iTween.Hash (
			"y", 0,
			"speed", 10,
			"oncomplete", "DestroyThis"

		));
	}


	public void DestroyThis ()
	{
		Destroy (gameObject);

	}


	public void ImSorryBro (float that, int rD)
	{
		height = height + (that * rD);
	}

	public void ShitBro (Wave anotherWave, int rD)
	{

		anotherWave.ImSorryBro (GetComponent<Wave> ().height, rD);
		boxCollider2DComp.enabled = false;
		initialDistance = Vector3.Distance (transform.position, otherWave.transform.position);
		originalH = height;
		LerpToZero ();
		height = 0;
		//  that.dontDoAnything = false;
		print ("bye bro");
	}
}
