using UnityEngine;
using System.Collections;

public class SpinAndBounce : MonoBehaviour {


	public float BounceSpeed = 2;
	public float SpinSpeed = 3;
	//public float TimeToDestroy = 20;
	public int ItemType = 0;
	//public int DestroyPoints = 3;
	
	protected Transform myTransform;
	protected float posY = 0.0f;
	protected bool Up = true;
	protected GameObject Canon;
	//protected Fire FireScript;
	
	protected float nTopY;				// For the Start Bounce until land in terrain.
	protected bool isLanded = false;
	protected bool bGoingUp = true;
	
	protected float GroundPosition = 3f;
	
	// Use this for initialization
	void Start () {
		//GameObject Terrain = GameObject.FindWithTag ("Terrain");
		//if(Terrain != null)
		//	Physics.IgnoreCollision(this.collider, Terrain.collider);
		
		Canon = GameObject.Find ("First Person Controller");
		//FireScript = Canon.GetComponent(typeof(Fire)) as Fire;
		myTransform = transform;
		posY = myTransform.position.y;
		nTopY = posY + 5;
		
		// This would cast rays only against colliders in layer , the Terrain.
		//int layerMask = 1 << 6;
		RaycastHit hit = new RaycastHit();
		Vector3 PosStart = myTransform.position;
		PosStart.y -= 3.51f;
		//Vector3 down = myTransform.TransformDirection (-Vector3.up);
		if(Physics.Raycast(PosStart, -Vector3.up, out hit))
			GroundPosition = hit.point.y + 1;
		
		//this.rigidbody.AddRelativeForce(new Vector3(0.0f, 15.0f, 0f), ForceMode.VelocityChange);
		//Destroy (gameObject, TimeToDestroy);
	}
	
	void Awake () { 
		myTransform = transform;
	}
	
	void Bounce () { 
		float PosAux;
		float MinVelocity = 5f;
		float MaxVelocity = 20f;
		
		PosAux = nTopY - myTransform.position.y;  	// Control the max & Min Velocity of the bounce
		if(PosAux < MinVelocity)					// increasing or decresing the vel amount.
			PosAux = MinVelocity;
		else
			if(PosAux > MaxVelocity)
				PosAux = MaxVelocity;
				
		if(bGoingUp)
			if(myTransform.position.y < nTopY)
				myTransform.Translate(0, Time.deltaTime * BounceSpeed * PosAux, 0, Space.World);
			else
				bGoingUp = false;
		else
			if(myTransform.position.y > GroundPosition)
				myTransform.Translate(0, -Time.deltaTime * BounceSpeed * PosAux, 0, Space.World);
			else{
				if(myTransform.position.y < GroundPosition){
					Vector3 PositionAux = myTransform.position;
					PositionAux.y = GroundPosition;
					myTransform.position = PositionAux;
					posY = myTransform.position.y;
				}
				isLanded = true;
			}
	}	
	
	// Update is called once per frame
	void Update () {
		if(!isLanded)
			Bounce();
		else{
		myTransform.Rotate(Vector3.up, SpinSpeed, Space.World);
		
		if(Up)
			if(myTransform.position.y < posY + 0.5f)
				myTransform.Translate(0, Time.deltaTime * BounceSpeed, 0, Space.World);
			else
				Up = false;
		else
			if(myTransform.position.y > posY - 0.5f)
				myTransform.Translate(0, -Time.deltaTime * BounceSpeed, 0, Space.World);
			else
				Up = true;
		//myTransform.Rotate(Vector3.forward, 2, Space.World);
		}
	}
	
	/*void OnTriggerEnter (Collider other) {
		if(other.gameObject.tag == "Player"){
			if(ItemType >= 0 && ItemType < 4){
				Debug.Log("Reloading");
				Fire FireScript = Canon.GetComponent(typeof(Fire)) as Fire;
				GameObject Bullet = FireScript.Weapons[ItemType];
				WeaponStatus WeaponScript = Bullet.GetComponent(typeof(WeaponStatus)) as WeaponStatus;
				WeaponScript.CurrentAmmo = WeaponScript.TotalAmmo;
				if(FireScript.TypeFire == ItemType)
					FireScript.Ammo = WeaponScript.TotalAmmo;
				//aiScript.ChangeWeaponByNumber(ItemType);
			}
			else 
			if(ItemType == 4){  // Health
				GeneralVars.Health += 100;
				if(GeneralVars.Health > 100)
					GeneralVars.Health = 100;
				GameObject HealthObj = GameObject.Find("GUI Health");
				GUIText HealthText = HealthObj.guiText;
				HealthText.text = "Health : " + GeneralVars.Health.ToString();
			}
			Destroy (gameObject);
		}
	}*/
	
}
