using UnityEngine;
using System.Collections;

public class TestMoveInter : MonoBehaviour
{
	public float rayDistance = 4f;
	RaycastHit whatIHit;
	GameObject[] targetItem;
	public bool IHitBlue = false;
	public bool IHitRed = false;
	public bool IHitYellow = false;
	public string journal;
	GameObject guiarea;
	GameObject book;
	bool showjournal = false;
	bool hasPicture = true;
	GameObject pic;
	bool showPicture = false;

	// Update is called once per frame
	void Update ()
	{
		Debug.DrawRay (this.transform.position, this.transform.forward * rayDistance, Color.red);
			
		//Creates a raycast, the "out" parameter is to output to the variable created above
		if(Physics.Raycast (this.transform.position, this.transform.forward, out whatIHit, rayDistance))
		{
			if(Input.GetKeyDown (KeyCode.F))
			{
				if (whatIHit.collider.transform.name == "Box1")
				{
					IHitBlue = true;
					journal = journal + "I hit the Blue Box.\n";

					targetItem = GameObject.FindGameObjectsWithTag("Red");
					foreach (GameObject thing in targetItem)
					{
						thing.renderer.enabled = false;
						thing.collider.enabled = false;
					}
					targetItem = GameObject.FindGameObjectsWithTag ("Yellow");
					foreach (GameObject thing in targetItem)
					{
						thing.renderer.renderer.enabled = enabled;
						thing.collider.enabled = true;
					}
				}
				if (whatIHit.collider.transform.name == "Box2")
				{
					IHitRed = true;
					journal = journal + "I Hit the Red Box.\n";
					targetItem = GameObject.FindGameObjectsWithTag("Yellow");
					foreach (GameObject thing in targetItem)
					{
						thing.renderer.enabled = false;
						thing.collider.enabled = false;
					}
					targetItem = GameObject.FindGameObjectsWithTag ("Blue");
					foreach (GameObject thing in targetItem)
					{
						thing.renderer.renderer.enabled = enabled;
						thing.collider.enabled = enabled;
					}
				}
				
				if (whatIHit.collider.transform.name == "Box3")
				{
					IHitYellow = true;
					journal = journal + "I Hit the Yellow Box.\n";
					targetItem = GameObject.FindGameObjectsWithTag("Blue");
					foreach (GameObject thing in targetItem)
					{
						thing.renderer.enabled = false;
						thing.collider.enabled = false;
					}
					targetItem = GameObject.FindGameObjectsWithTag ("Red");
					foreach (GameObject thing in targetItem)
					{
						thing.renderer.renderer.enabled = enabled;
						thing.collider.enabled = enabled;
					}
				}
			}
		}
		if(Input.GetKeyDown (KeyCode.J))
		{
			if (showPicture == false)
			{
				if (showjournal == false)
				{
					showjournal = true;
					guiarea = GameObject.FindGameObjectWithTag ("Journal"); 
					guiarea.transform.guiText.text = journal;
					book = GameObject.FindGameObjectWithTag ("Book");
					book.renderer.enabled = enabled;
					CharacterController cc = GetComponent<CharacterController>();
					cc.enabled = false; 
					GameObject.Find ("Player").GetComponent<MouseLook>().enabled = false;
					GameObject.Find ("Main Camera").GetComponent<MouseLook>().enabled = false;
					GetComponent<FPSInputController>().enabled = false;
					GetComponent<CharacterMotor>().enabled = false;
					
				}
				else
				{
					guiarea = GameObject.FindGameObjectWithTag ("Journal");
					guiarea.transform.guiText.text = "";
					showjournal = false;
					book.renderer.enabled = false;
					CharacterController cc = GetComponent<CharacterController>();
					cc.enabled = true; 
					MouseLook mouseL = GetComponent<MouseLook>();
					mouseL.enabled = true;
					GameObject.Find ("Main Camera").GetComponent<MouseLook>().enabled = true;
					GetComponent<FPSInputController>().enabled = true;
					GetComponent<CharacterMotor>().enabled = true;
				}
			}

		}
		if (Input.GetKeyDown (KeyCode.P))
		{
			if (hasPicture == true)
			{
				if (showPicture == false)
				{
				CharacterController cc = GetComponent<CharacterController>();
				cc.enabled = false; 
				GameObject.Find ("Player").GetComponent<MouseLook>().enabled = false;
				GameObject.Find ("Main Camera").GetComponent<MouseLook>().enabled = false;
				GetComponent<FPSInputController>().enabled = false;
				GetComponent<CharacterMotor>().enabled = false;
				book = GameObject.FindGameObjectWithTag ("Book");
				book.renderer.enabled = false;
				pic = GameObject.FindGameObjectWithTag ("Picture");
				pic.renderer.enabled = true;
				showPicture = true;
				}
				else
				{
				CharacterController cc = GetComponent<CharacterController>();
				cc.enabled = true;
				GameObject.Find ("Player").GetComponent<MouseLook>().enabled = true;
				GameObject.Find ("Main Camera").GetComponent<MouseLook>().enabled = true;
				GetComponent<FPSInputController>().enabled = true;
				GetComponent<CharacterMotor>().enabled = true;
				pic = GameObject.FindGameObjectWithTag ("Picture");
				pic.renderer.enabled = false;
				showPicture = false;
				}
			}
		}
	}
}

