using UnityEngine;
using System.Collections;

public class ProjectileCollision : MonoBehaviour {

	public int DamageDealt=1;
	public float StunTime=0.1f;
	public float Knockback=0.5f;

	public Sprite ConvertedSprite;
	public Transform ExplosionPrefab;
	// TODO add code hook to projectile collision
	
	private GameController gc;	
	private GameObject cameraGO;
	private bool juicing = false;
	private SpriteRenderer rend;

	private bool converted=false;
	private bool bounced=false;
	private float dontDestroyTil=0;
	private bool QueuedConvert = false;

	// Use this for initialization
	void Start () {
		GameObject go = GameObject.Find ("GameController");
		gc = go.GetComponent<GameController>();
		cameraGO = GameObject.Find ("Main Camera");
		rend = gameObject.GetComponent<SpriteRenderer>();
	}

	void OnCollisionEnter2D(Collision2D coll) {
		if (coll.gameObject.tag == "Player")
		{
			if(!converted)
			{
				PlayerStatus ps = coll.gameObject.GetComponent<PlayerStatus>();
				ps.TakeDamage(DamageDealt);

				gc.ReportPlayerHit();

				Transform d = (Transform)GameObject.Instantiate (gc.PlayerDamagePrefab);
				d.position = transform.position;//coll.contacts[0].point;
				TrailTarget trail = d.GetComponent<TrailTarget>();
				trail.Target = coll.transform;


				if(gc.PlayerDamageClip != null)
					iTween.Stab(gc.gameObject, iTween.Hash ("audiosource", gc.DamageAudio, "audioclip", gc.PlayerDamageClip, "volume", 1.1f*gc.Volume));

				System.Threading.Thread.Sleep(80);

				//if(cameraGO != null)
				//	iTween.ShakePosition(cameraGO, new Vector3(0.1f, 0.1f, 0), 0.05f);
			}

			Explode();
		}

		else if (coll.gameObject.tag == "Monster")
		{
			MonsterStatus ms = coll.gameObject.GetComponent<MonsterStatus>();
			ms.TakeDamage(DamageDealt);
			Vector2 stunvec = (Vector2)(coll.transform.position - transform.position).normalized * 0.5f + (rigidbody2D.velocity.normalized * 0.5f);
			ms.Stun(StunTime, stunvec * Knockback);

			if(converted)
			{
				PopperDrop pop = coll.gameObject.GetComponent<PopperDrop>();
				if(pop != null)
					pop.PopConverted();
			}
			Explode();
		}

		else if(coll.gameObject.tag == "Mirror")
		{			
			ConvertProjectile();
			
			if(!juicing)
			{
				juicing = true;
				
				if(cameraGO != null)
					iTween.ShakePosition(cameraGO, new Vector3(0.1f, 0.1f, 0), 0.05f);
				
				iTween.ScaleFrom (gameObject, iTween.Hash("scale", new Vector3(1.75f, 1.2f, 0f), "time", 0.6f, "oncomplete", "DoneJuicing"));
			}
			
			SwingArmJuice j = coll.gameObject.GetComponent<SwingArmJuice>();
			if(j!=null)
				j.RunJuice((Vector3)coll.contacts[0].point);

			//Debug.Log ("Shot bouncing off Mirror");
		}
		else if(coll.gameObject.tag == "Wall" && converted && !bounced)
		{
			// converted bullets get one free bounce off walls
			bounced = true;
			dontDestroyTil = Time.fixedTime + 0.05f;
			Debug.Log ("Projectile bouncing off wall");
		}
		else if(Time.fixedTime > dontDestroyTil)
		{
			Explode();
		}
		else
			Debug.Log("Ignoring fall-thru Projectile Collision with " + coll.gameObject.name + " bounced = " + bounced);
	}

	void FixedUpdate()
	{
		if(QueuedConvert)
			DoConvertProjectile();
	}

	private void DoConvertProjectile()
	{
		QueuedConvert = false;
		converted = true;
		gameObject.layer =  LayerMask.NameToLayer("PlayerProjectile");
		if(ConvertedSprite != null && rend != null)
		{
			rend.sprite = ConvertedSprite;
		}

		MoveForward mf = gameObject.GetComponent<MoveForward>();
		mf.Speed = 10f + gc.Player.rigidbody2D.velocity.magnitude;
	}

	public void ConvertProjectile()
	{
		QueuedConvert = true;
	}

	private void Explode()
	{
		if(ExplosionPrefab != null)
		{
			Transform exp = (Transform) GameObject.Instantiate(ExplosionPrefab);
			exp.position = transform.position;
		}
		
		GameObject.Destroy (gameObject);
	}

	void DoneJuicing()
	{
		juicing = false;
	}

}
