using UnityEngine;
using System.Collections;

public class MinibotScript : MonoBehaviour
{
	public bool DirIsRight;
	public int bottype;
	
	float proper_x;
	float proper_y;
	Quaternion proper_rotation;
	public int hp = 2;
	bool colorhasbeenset = false;
	MeshRenderer MR_REF;
	BoxCollider BC_REF;
	ParticleSystem PS_REF;
	public GizmoScript GS_REF;
	bool gone = false;
	public bool shining;
	
	const int HEART = 0;
	const int RED = 1;
	const int BLU = 2;
	const int GRN = 3;

	// Use this for initialization
	void Start ()
	{
		PS_REF = (ParticleSystem)GetComponent ("ParticleSystem");
		MR_REF = (MeshRenderer)GetComponent("MeshRenderer");
		BC_REF = (BoxCollider)GetComponent("BoxCollider");
		proper_y = transform.position.y;
		proper_rotation = transform.rotation;
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(!gone)
		{
			float speed = (1.0f * (float)GS_REF.difficulty) + 2.5f;
			if(DirIsRight)
				rigidbody.velocity = new Vector3(speed, 0.0f, 0.0f);
			else
				rigidbody.velocity = new Vector3(-speed, 0.0f, 0.0f);

			proper_x = transform.position.x;
			if(proper_x > 6.1f)
				proper_x = 6.1f;
			if(proper_x < -6.1f)
				proper_x = -6.1f;

			transform.position = new Vector3 (proper_x,
			                                  proper_y,
			                                  0.0f);
			transform.rotation = proper_rotation;

			if(DirIsRight && shining && !PS_REF.isPlaying)
			{
				PS_REF.Play ();
			}

			if(hp <= 0)
			{
				MR_REF.renderer.enabled=false;
				BC_REF.enabled = false;
				if(DirIsRight)
				{
					PS_REF.Stop ();
					GameObject.Destroy (this.gameObject,0.5f);
				}
				else
				{
					PS_REF.Play ();
					GameObject.Destroy (this.gameObject,2.0f);
				}

				gone = true;
			}

			if(!colorhasbeenset)
			{
				switch(bottype)
				{
				case RED:
					MR_REF.material.SetColor("_Color", new Color(0.84f, 0.2f, 0.84f));
					break;
				case BLU:
					MR_REF.material.SetColor("_Color", new Color(0.2f, 0.84f, 0.84f));
					break;
				case GRN:
					MR_REF.material.SetColor("_Color", new Color(0.84f, 0.84f, 0.2f));
					break;
				}
				PS_REF.startColor = MR_REF.material.color;
				colorhasbeenset = true;
			}
		}

	}

	void OnCollisionStay(Collision col)
	{

		if (col.gameObject.name == "Minibot(Clone)" || col.gameObject.name == "Minibot2(Clone)") 
		{
			MinibotScript mbs_ref = (MinibotScript)col.gameObject.GetComponent("MinibotScript");

			if(mbs_ref.DirIsRight != DirIsRight)
			{
				if(bottype == mbs_ref.bottype)
				{
					if(DirIsRight)
					{
						GS_REF.Heal(shining);
						GS_REF.Score(100, true);
					}
				}
				else
				{
					// breaks combo
					GS_REF.Score(100, false);
				}

				hp = 0;
			}
		}
		else if(col.gameObject.name == "P1Heart" || col.gameObject.name == "P2Heart")
		{
			HeartScript heart_ref = (HeartScript)col.gameObject.GetComponent("HeartScript");
			if(DirIsRight != heart_ref.DirIsRight)
			{
				hp = 0;
				GS_REF.Damage();
				//no points, just breaks combo
				GS_REF.Score(0, false);
			}
		}			
		else
		{
			//collided with a friend in the same lane
		}


	}

}
