using UnityEngine;
using System.Collections;

public class HeadsUpDisplay : MonoBehaviour {

	//Distance that crosshair raycast will extend
	public float rayDistance;

	//GameObject container for interactables
	private GameObject interactable;

	//Crosshair texture
	private Texture2D crosshair;

	//Shaders for swapping between
	private Shader highlight;

	//Flag to show interact text
	private bool interactFlag = false;

	public GUIStyle textStyle;

	private Material[] container;

	public bool highlighting = false;
	public bool inCutscene = false;
	
	public Controls controls;
	private string interact;
	
	private GameObject player;

	void Start ()
	{	
		player = GameObject.Find ("Player");
		//Initialize shaders and crosshair
		highlight = Shader.Find("Reflective/Diffuse");
		crosshair = new Texture2D(1, 1);
		crosshair.SetPixel (0, 0, Color.white);
		crosshair.Apply();
		
		controls = transform.root.GetComponent<Controls>();
		interact = controls.square;
		//Begin checking for interactables
		StartCoroutine (InteractCheck ());
	}
	
	//Checks if aimed object is interactable
	private IEnumerator InteractCheck ()
	{
		while (true)
		{
			//Initialize raycast from middle of viewport (crosshair)
			Ray ray = Camera.main.ViewportPointToRay (new Vector3 (0.5f, 0.5f, 0));
			RaycastHit hit;

			//Check for raycast collision
			if (Physics.Raycast (ray, out hit, rayDistance))
			{
				Debug.Log(hit.transform.name);
				//Check if collision is interactable
				if (hit.transform.gameObject.tag == "Interactable" && player.GetComponent<States>().mouselookFlag)
				{
					//Change interact text flag
					interactFlag = true;

					int loop = 0;

					if (interactable != hit.transform.gameObject && highlighting)
					{
						if (interactable != null)
							interactable.GetComponentInChildren<MeshRenderer>().materials = container;
						container = new Material[hit.transform.gameObject.GetComponentInChildren<MeshRenderer>().materials.Length];
						foreach (Material element in hit.transform.gameObject.GetComponentInChildren<MeshRenderer>().materials)
						{
							container[loop++] = new Material(element);
						}
					}

					//Initialize interactable gameObject container
					interactable = hit.transform.gameObject;

					//Change interactable shader to highlighted
					if (highlighting)
					{
						foreach (Material element in interactable.GetComponentInChildren<MeshRenderer>().materials)
						{
							element.shader = highlight;
						}
					}

					//Check for Mouseclick interaction
					if (Input.GetButtonDown (interact))
					{
						//Send interaction
						interactable.SendMessage("Use", null);
					}
				} else {
					if (interactable && highlighting)
					{
			//Return interactable shader and flag to normal
						interactable.GetComponentInChildren<MeshRenderer>().materials = container;
					}
					interactFlag = false;
				}
			} else {
				if (interactable && highlighting)
				{
					interactable.GetComponentInChildren<MeshRenderer>().materials = container;
				}
				interactFlag = false;
			}
			/************************************/
			yield return null;
		}
	}

	void OnGUI ()
	{	
		//Draw crosshair
		if (!inCutscene)
			GUI.DrawTexture(new Rect(Screen.width / 2f - 1, Screen.height / 2f - 1, 2, 2), crosshair);

		if (interactable != null)
		{
			if (interactFlag && interactable.GetComponent<InteractText>() != null)
			{
				string interactText = interactable.GetComponent<InteractText>().interactText;
				DrawOutline(new Rect(Screen.width / 2f - 100, Screen.height / 2f + 100, 200, 20), interactText, textStyle, Color.black, Color.white);
			}
		}
	}

	void DrawOutline (Rect location, string text, GUIStyle style, Color outColour, Color inColour)
 	{
 		GUIStyle origStyle = style;
 		style.normal.textColor = outColour;
	    location.x--;
	    GUI.Label(location, text, style);
	    location.x +=2;
	    GUI.Label(location, text, style);
	    location.x--;
	    location.y--;
	    GUI.Label(location, text, style);
	    location.y +=2;
	    GUI.Label(location, text, style);
	    location.y--;
	    style.normal.textColor = inColour;
	    GUI.Label(location, text, style);
	    style = origStyle;
 	}
}