#pragma strict
//@script RequireComponent(AudioSource)

private var guiShow : boolean = false;
private var guiShowInfo : boolean = false;
private var guiShowObject : boolean = false;
private var guiShowHitQuantic : boolean = false;
private var guiShowBreakGlass : boolean = false;

private var guiShowBottleDrink : boolean = false;
private var guiShowBottle1 : boolean = false;
private var guiShowBottle2 : boolean = false;
private var guiShowBottle3 : boolean = false;
var bottlesound : GameObject;

private var isOpen : Array = new Array(false,false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false); // seven doors

private var protectiveGlass : GameObject;
var protectiveGlassSound : GameObject;

var info1 : GameObject;
var info2 : GameObject;
var info3 : GameObject;
var info4 : GameObject;
var info5 : GameObject;


//var info1impact : AudioClip;

var rayLength = 10;
var rayLengthInfo = 10;

//get the hammer to be destroyed
private var otherhammer : GameObject;
//private var otherPlayer : GameObject;

//hide cursor
function Start () {
	if (PlayerPrefs.GetInt("PlayerBrakeGlass") == 1)
	{
				protectiveGlass = GameObject.FindWithTag("Glass");
				Destroy(protectiveGlass);
	}
Screen.showCursor = false;
}



function Update()
{
	var hit : RaycastHit;
	var tag : String;
	var DoorIndex : int;
	var fwd = transform.TransformDirection(Vector3.forward);
	var DoorIndexEnd : int;


//	Debug.Log(Physics.Raycast(transform.position, fwd, hit, rayLength));

	if(Physics.Raycast(transform.position, fwd, hit, rayLength))
	{
		tag = hit.collider.gameObject.tag;
		DoorIndex = tag.IndexOf("Door");
		if(DoorIndex > -1)
		{
			DoorIndexEnd = (DoorIndex + 4); //length of "Door"
			updateDoor(
				hit.collider.gameObject,
				int.Parse(
					tag.Substring(
						DoorIndexEnd,
						(tag.Length - DoorIndexEnd)
					)
				),
				hit);
		} else {
			guiShow = false;
		}
	}
	else
	{
		guiShow = false;
	}
				if (!(Application.loadedLevelName == "World0"))
			{
	//*************
	if(Physics.Raycast(transform.position, fwd, hit, rayLength))
	{	print (PlayerPrefs.GetInt("PlayerHammer"));
		if(hit.collider.gameObject.tag == "Hammer"||hit.collider.gameObject.tag == "Hammer_Empty")
		{	
			guiShowObject = true;
			if (Application.loadedLevelName == "World5")
			{	
				otherhammer = GameObject.FindWithTag("Hammer");
				Destroy(otherhammer);
				//PlayerPrefs.SetInt ("PlayerHammer", 1);
				guiShowObject = false;
				guiShowHitQuantic = true;
			}
			
			/*else if(Input.GetKeyDown("e") && isOpen == true)
			{
				door.audio.Play("DoorClose");
				isOpen = false;
				guiShow = false;
			}*/
		}

	}
			else
		{
			guiShowObject = false;
			guiShowHitQuantic = false;
		}
			//else { guiShow = true;
				
			 //}	
	//*************
	if(Physics.Raycast(transform.position, fwd, hit, rayLength))
	{
		if(hit.collider.gameObject.tag == "Glass"&PlayerPrefs.GetInt("PlayerHammer") == 1)
		{
			guiShowBreakGlass = true;
			//if(Input.GetKeyDown("e") && isOpen == false)
			if(Input.GetKeyDown("e"))
			{
				//audio.Play(info1impact);
				
				//isOpen = true;
				guiShowBreakGlass = false;
				protectiveGlass = GameObject.FindWithTag("Glass");
				protectiveGlassSound.audio.Play();
				Destroy(protectiveGlass);
				PlayerPrefs.SetInt ("PlayerBrakeGlass", 1);
			}
			
			/*else if(Input.GetKeyDown("e") && isOpen == true)
			{
				door.audio.Play("DoorClose");
				isOpen = false;
				guiShow = false;
			}*/
		}
	}	
	else
	{
		guiShowBreakGlass = false;
		//audio.Stop(info1impact);
		info1.audio.Stop();
	}
	//*************
	if(Physics.Raycast(transform.position, fwd, hit, rayLength))
	{
		if(hit.collider.gameObject.tag == "World1")
		{
			guiShowInfo = true;
			//if(Input.GetKeyDown("e") && isOpen == false)
			if(Input.GetKeyDown("e"))
			{
				//audio.Play(info1impact);
				info1.audio.Play();
				//isOpen = true;
				guiShowInfo = false;
				Application.LoadLevel ("World1");
			}
			
			/*else if(Input.GetKeyDown("e") && isOpen == true)
			{
				door.audio.Play("DoorClose");
				isOpen = false;
				guiShow = false;
			}*/
		}
	}	
	else
	{
		guiShowInfo = false;
		//audio.Stop(info1impact);
		info1.audio.Stop();
	}
	//*************
	if(Physics.Raycast(transform.position, fwd, hit, rayLength))
	{
		if(hit.collider.gameObject.tag == "World2")
		{
			guiShowInfo = true;
			//if(Input.GetKeyDown("e") && isOpen == false)
			if(Input.GetKeyDown("e"))
			{
				//audio.Play(info1impact);
				info2.audio.Play();
				//isOpen = true;
				guiShowInfo = false;
				Application.LoadLevel ("World2");
			}
			
			/*else if(Input.GetKeyDown("e") && isOpen == true)
			{
				door.audio.Play("DoorClose");
				isOpen = false;
				guiShow = false;
			}*/
		}
	}	
	else
	{
		guiShowInfo = false;
		//audio.Stop(info1impact);
		info2.audio.Stop();
	}
		//*************
	if(Physics.Raycast(transform.position, fwd, hit, rayLength))
	{
		if(hit.collider.gameObject.tag == "World3")
		{
			guiShowInfo = true;
			//if(Input.GetKeyDown("e") && isOpen == false)
			if(Input.GetKeyDown("e"))
			{
				//audio.Play(info1impact);
				info3.audio.Play();
				//isOpen = true;
				guiShowInfo = false;
				Application.LoadLevel ("World3");
			}
			
			/*else if(Input.GetKeyDown("e") && isOpen == true)
			{
				door.audio.Play("DoorClose");
				isOpen = false;
				guiShow = false;
			}*/
		}
	}	
	else
	{
		guiShowInfo = false;
		//audio.Stop(info1impact);
		info2.audio.Stop();
	}
		//*************
	if(Physics.Raycast(transform.position, fwd, hit, rayLength))
	{
		if(hit.collider.gameObject.tag == "World4")
		{
			guiShowInfo = true;
			//if(Input.GetKeyDown("e") && isOpen == false)
			if(Input.GetKeyDown("e"))
			{
				//audio.Play(info1impact);
				info4.audio.Play();
				//isOpen = true;
				guiShowInfo = false;
				Application.LoadLevel ("World4");
			}
			
			/*else if(Input.GetKeyDown("e") && isOpen == true)
			{
				door.audio.Play("DoorClose");
				isOpen = false;
				guiShow = false;
			}*/
		}
	}	
	else
	{
		guiShowInfo = false;
		//audio.Stop(info1impact);
		info2.audio.Stop();
	}
		//*************
	if(Physics.Raycast(transform.position, fwd, hit, rayLength))
	{
		if(hit.collider.gameObject.tag == "World5")
		{
			guiShowInfo = true;
			//if(Input.GetKeyDown("e") && isOpen == false)
			if(Input.GetKeyDown("e"))
			{
				//audio.Play(info1impact);
				info5.audio.Play();
				//isOpen = true;
				guiShowInfo = false;
				Application.LoadLevel ("World5");
			}
			
			/*else if(Input.GetKeyDown("e") && isOpen == true)
			{
				door.audio.Play("DoorClose");
				isOpen = false;
				guiShow = false;
			}*/
		}
	}	
	else
	{
		guiShowInfo = false;
		//audio.Stop(info1impact);
		info2.audio.Stop();
	}

	//*************
	if(Physics.Raycast(transform.position, fwd, hit, rayLength))
	{
		if(hit.collider.gameObject.tag == "Bottle1")
		{
			guiShowBottle1 = true;
			//if(Input.GetKeyDown("e") && isOpen == false)
			if(Input.GetKeyDown("e"))
			{
				//audio.Play(info1impact);
				bottlesound.audio.Play();
				PlayerPrefs.SetInt ("PlayerSweet", 0);
				//isOpen = true;
				guiShowBottle1 = false;
				//Application.LoadLevel ("World4");
			}
			
			/*else if(Input.GetKeyDown("e") && isOpen == true)
			{
				door.audio.Play("DoorClose");
				isOpen = false;
				guiShow = false;
			}*/
		}
	}	
	else
	{
		guiShowBottle1 = false;
		//audio.Stop(info1impact);
		info2.audio.Stop();
	}
		//*************
	if(Physics.Raycast(transform.position, fwd, hit, rayLength))
	{
		if(hit.collider.gameObject.tag == "Bottle2")
		{
			guiShowBottle2 = true;
			//if(Input.GetKeyDown("e") && isOpen == false)
			if(Input.GetKeyDown("e"))
			{
				//audio.Play(info1impact);
				bottlesound.audio.Play();
				PlayerPrefs.SetInt ("PlayerSweet", 1);
				//isOpen = true;
				guiShowBottle2 = false;
				//Application.LoadLevel ("World4");
			}
			
			/*else if(Input.GetKeyDown("e") && isOpen == true)
			{
				door.audio.Play("DoorClose");
				isOpen = false;
				guiShow = false;
			}*/
		}
	}	
	else
	{
		guiShowBottle2 = false;
		//audio.Stop(info1impact);
		info2.audio.Stop();
	}
		//*************
	if(Physics.Raycast(transform.position, fwd, hit, rayLength))
	{
		if(hit.collider.gameObject.tag == "Bottle3")
		{
			guiShowBottle3 = true;
			//if(Input.GetKeyDown("e") && isOpen == false)
			if(Input.GetKeyDown("e"))
			{
				//audio.Play(info1impact);
				bottlesound.audio.Play();
				PlayerPrefs.SetInt ("PlayerSweet", 0);
				//isOpen = true;
				guiShowBottle1 = false;
				//Application.LoadLevel ("World4");
			}
			
			/*else if(Input.GetKeyDown("e") && isOpen == true)
			{
				door.audio.Play("DoorClose");
				isOpen = false;
				guiShow = false;
			}*/
		}
	}	
	else
	{
		guiShowBottle3 = false;
		//audio.Stop(info1impact);
		info2.audio.Stop();
	}
		//se termina iful de la World0
	}
}

function updateDoor(door : GameObject, index : int, hit : RaycastHit)
{
	guiShow = true;
		
	if(Input.GetKeyDown("e"))
	{
		if (index != 4) {
			toggleDoor(door, index);
		} else {
			var alarmHandler : AlarmHandler = GetComponent(AlarmHandler);
			if (!alarmHandler.doorLocked) {
				toggleDoor(door, index);
			}
		}
	}
}

function toggleDoor(door : GameObject, index : int)
{
	var animationName : String;
	
	if(isOpen[index] == false)
	{
		animationName = String.Concat("Door",index.ToString(),"Open");
		isOpen[index] = true;
		guiShow = false;
	} else {
		animationName = String.Concat("Door",index.ToString(),"Close");
		isOpen[index] = false;
		guiShow = false;
	}
	
	door.animation.Play(animationName);
}

function OnGUI()
{
	//if(guiShow == true && isOpen == false)
	if(guiShowBottle1 == true)
	{
		GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 500, 30), "Mmmm, it looks bitter. Drink (E)");
	}
	if(guiShowBreakGlass == true)
	{
		GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 30), "Break glass (E)");
	}
	
	if(guiShowBottle2 == true)
	{
		GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 500, 30), "Mmmm, it looks sweet. Drink (E)");
	}
	if(guiShowBottle3 == true)
	{
		GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 500, 30), "Mmmm, it looks sour. Drink (E)");
	}
	if(guiShowObject == true)
	{
		GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Get Object");
	}
		if(guiShowHitQuantic == true)
	{
		GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 500, 50), "Hmm, it's just disappeared! I must find it in another world.");
	}
	
	if(guiShow == true)
	{
		GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Use Door (E)");
	}
		if(guiShowInfo == true)
	{
		GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Enter World (E)");
	}
}