using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Mass : MonoBehaviour
{
		public Rigidbody controller;
		public float speed;
		public float jumpSpeed;
		private bool isGrounded, isJumping;
		public BoxCollider collider;
		private Animator animator;
		
		public static int maxEnergy = 150;
		public static int minEnergy = 0;
		public static int energyJump = 5;
		public static int bonusEnergy = 15;
		public static int startingEnergy = 50;
		public static float massScale;
		public static float deltaMass;
		private static int energy;
		public static GameObject weight;
		public Text HeightTutText;
		private static float startingMass;
		private static Vector3 startingScale;
	
	
		public static int Energy {
				get{ return energy;}
				set {
						if (value > maxEnergy) {
								return;
						}
						energy = value;
						if (weight == null) {
								weight = GameObject.FindGameObjectWithTag ("Player");
						}
			
						//TODO set mass set scale set position
						float scaleFactor = (float)energy / (float)startingEnergy;
						weight.rigidbody.mass = startingMass * scaleFactor > 0.25f ? startingMass * scaleFactor : 0.25f;
						Vector3 nextScale =
				(weight.transform.parent == null) ?
					startingScale * scaleFactor :
					scaleFactor * gamemanager.ComponentsRatio (startingScale, gamemanager.CALCULATEREALSCALE (weight.transform.parent));
						weight.transform.localPosition += new Vector3 (0f, (nextScale.y - weight.transform.localScale.y) / 2f, 0);
						weight.transform.localScale = nextScale;
						
						if (value <= 0) {
								GameObject.FindGameObjectWithTag ("Caronte").GetComponent<TheKeeper> ().actualLevel = Application.loadedLevelName;
								GameObject.FindGameObjectWithTag ("Caronte").GetComponent<TheKeeper> ().displayText = "You ran out of mass!".ToUpper ();

								Application.LoadLevel ("giocochiuso");
						}
				}
		}
		// Use this for initialization
		void Start ()
		{
				//animator = this.GetComponent<Animator> ();
				energy = startingEnergy;
				startingMass = this.gameObject.rigidbody.mass;
				startingScale = this.gameObject.transform.localScale;
				Physics.gravity = new Vector3 (0f, -98f, 0f);
				weight = GameObject.FindGameObjectWithTag ("Player");
		
				HeightTutText = null;
				
				
		}
	
		// Update is called once per frame
		void Update ()
		{
				//animator.SetBool ("isJumping", !isGrounded);
				if (Input.GetAxisRaw ("Horizontal") != 0f) {
						controller.AddForce (Input.GetAxisRaw ("Horizontal") * this.transform.right * speed);
						
				}
				//animator.SetBool ("isWalking", false);
				if (Input.GetButtonDown ("Jump") && isGrounded && !isJumping) {
						//animator.SetBool ("isJumping", true);
						isGrounded = false;
						isJumping = true;
						controller.AddForce (this.transform.up * jumpSpeed);
				}
				//controller.Move (-this.transform.right * speed * Time.deltaTime);
			
		
		}
		void OnCollisionEnter ()
		{
				isJumping = false;
		}
		void OnCollisionStay (Collision collision)
		{
				
				foreach (ContactPoint contact in collision.contacts) {
			
						if (contact.point.y <= transform.position.y) {
								isGrounded = true;
								if (collision.gameObject.tag == "Platform") {
										this.transform.parent = collision.gameObject.transform;
								}
								return;
						}		
				}
				isGrounded = false;
		}
		void OnCollisionExit (Collision collision)
		{
				if (collision.gameObject.transform.Equals (this.transform.parent)) {
						
						this.transform.parent = null;
				}
		
		}
		void OnTriggerEnter (Collider other)
		{
				if (HeightTutText != null) {
						HeightTutText.GetComponent<Animator> ().SetBool ("isActive", true);
				}
				if (other.gameObject.CompareTag ("MassBonus")) {
						Energy += bonusEnergy;
			
						Destroy (other.gameObject);
				}
				// TODO accedere e sbloccare il livello successivo 
				if (other.gameObject.CompareTag ("Fine")) {
						GameObject.FindGameObjectWithTag ("Caronte").GetComponent<TheKeeper> ().actualLevel = Application.loadedLevelName;
						GameObject.FindGameObjectWithTag ("Caronte").GetComponent<TheKeeper> ().displayText = "SCORE: " + (int)((Timer.SecondsRemaining + Mass.Energy + Luce.Energy) * 563);
						Application.LoadLevel ("giocochiuso"); 
				}
				
		}
}
