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

public class gamemanager : MonoBehaviour
{
		public GameObject lightOb;
		public GameObject massOb;
		public Camera maincamera;
		public Light luce;
		public GameObject mass;
		public GameObject muro;
		private SpriteRenderer sprite;
		private bool isClick;
		private Animator lightanimator, weightanimator;
		public Text LightLabel;
		public Text WeightLabel;
		private float rL, lL;
		
	
	
		private int energyTransferJump = 5;

		// Use this for initialization
		void Start ()
		{
				Screen.showCursor = false;
				sprite = mass.GetComponent<SpriteRenderer> ();
				lightanimator = lightOb.GetComponentInChildren<Animator> ();
				weightanimator = massOb.GetComponentInChildren<Animator> ();
				//Debug.Log ("init mass: " + Mass.Energy);
				
				Luce.luce = luce;
				Luce.Energy = 100;
				//Debug.Log ("init light: " + Luce.Energy);
				float offset = 10;
				rL = Camera.main.ScreenToWorldPoint (new Vector3 (Screen.width + offset, 0, 0)).x;
				lL = Camera.main.ScreenToWorldPoint (new Vector3 (-offset, 0, 0)).x;
		
				GameObject muroDx = (GameObject)Instantiate (muro, new Vector3 (rL, 0, mass.transform.position.z), Quaternion.identity);
				GameObject muroSx = (GameObject)Instantiate (muro, new Vector3 (lL, 0, mass.transform.position.z), Quaternion.identity);
		
				muroDx.transform.localScale = new Vector3 (1f, 10000f, 1f);
				muroSx.transform.localScale = new Vector3 (1f, 10000f, 1f);
		
				Debug.Log (mass.gameObject.transform.localScale.x);
				
		
			
		}
	
		// Update is called once per frame
		void Update ()
		{
				
				Ray ray = maincamera.ScreenPointToRay (Input.mousePosition);
				Vector3 newPos = new Vector3 (ray.origin.x, ray.origin.y, lightOb.transform.position.z);
				lightOb.transform.position = newPos;
				if (Input.GetButtonDown ("Fire1"))
						isClick = true;
						
				if (Input.GetButtonUp ("Fire1"))
						isClick = false;
				if (isClick) {
						lightanimator.SetBool ("isEmitting", true);
						luce.GetComponent<Light> ().enabled = true;
				} else {
						luce.GetComponent<Light> ().enabled = false;
						lightanimator.SetBool ("isEmitting", false);
				}
				/** to handle mouse wheel scroll **/ 
				
		
				if (Input.GetButtonDown ("Fire2") && !(Mass.Energy >= Mass.maxEnergy)) {
			
						if (Luce.Energy > Mass.energyJump) {
								Luce.Energy -= energyTransferJump;
								Mass.Energy += energyTransferJump;
							
						}
			
						
				}	
				
				if (Input.GetButtonDown ("Fire3") && !(Mass.Energy <= Mass.minEnergy)) {
						
						
						Luce.Energy += energyTransferJump;
						Mass.Energy -= energyTransferJump;
		
						
						
			
				}
		
				LightLabel.text = ((int)Luce.Energy).ToString ();
				WeightLabel.text = Mass.Energy.ToString ();
			
		}
		public static Vector3 CALCULATEREALSCALE (Transform transform)
		{
				if (transform.parent == null) {
						return transform.localScale;
				}
				return ComponentsRatio (CALCULATEREALSCALE (transform.parent), transform.localScale);
		}
	
		public static Vector3 ComponentsRatio (Vector3 a, Vector3 b)
		{
				return new Vector3 (a.x / b.x, a.y / b.y, a.z / b.z);
		}
}
