using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent (typeof (LineRenderer))]
public class Laser : MonoBehaviour {

	private LineRenderer laser;
	private List<GameObject> lasers;

	void Awake () {
		laser = GetComponent<LineRenderer> ();
		laser.useWorldSpace = true;
		laser.SetPosition (0, Vector3.zero);
		laser.SetPosition (1, Vector3.zero);

		lasers = new List<GameObject> ();
	}

	public void UpdateLaser()
	{
		bool reflection;
		int maxBounce = 0;

		Vector3 lastCollision = this.transform.position;
		Vector3 lastDirection = this.transform.forward;

		foreach(GameObject e in lasers) {Destroy(e);}
		lasers.Clear ();

		do
		{
			reflection = false;
			maxBounce++;

			// Detect where the laser would hit the wall and draw it up till that point.
			RaycastHit hit;
			if (Physics.Raycast (lastCollision, lastDirection*300.0f, out hit))
			{
				// Is it a mirror?
				Mirror mirror = hit.collider.GetComponent<Mirror>();

				Vector3 start 	= lastCollision;
				Vector3 end		= Vector3.zero;

				// We hit something!
				GameObject temp = new GameObject("laser");
				temp.layer = 2; // Ignore Raycast

				LineRenderer newLaser = temp.AddComponent<LineRenderer>();

				newLaser.materials = laser.materials;
				newLaser.SetColors(Color.red, Color.red);
				newLaser.SetWidth(0.2f, 0.2f);
				if (mirror) end = mirror.gameObject.transform.position;
				else end = hit.point;

				newLaser.SetPosition(0, start);
				newLaser.SetPosition(1, end);
				lasers.Add (temp);

				bool horizontal = false;

				if ( Mathf.Abs(end.x - start.x) < 0.05f ) horizontal = true;

				if (mirror)
				{
					reflection = true;
					lastCollision = end;

					if (mirror.Face)
					{
						if (mirror.transform.position.x - start.x > 0.25f)			{lastDirection = Vector3.up;}
						else if (mirror.transform.position.x - start.x < -0.25f)	{lastDirection = Vector3.down;}
						else if (mirror.transform.position.y - start.y > 0.25f)		{lastDirection = Vector3.right;}
						else 														{lastDirection = Vector3.left;}
					}
					else
					{
						if (mirror.transform.position.x - start.x > 0.25f)			{lastDirection = Vector3.down;}
						else if (mirror.transform.position.x - start.x < -0.25f)	{lastDirection = Vector3.up;}
						else if (mirror.transform.position.y - start.y > 0.25f)		{lastDirection = Vector3.left;}
						else														{lastDirection = Vector3.right;}
					}

				}
				else // Check if the laser is NOT hitting a goal.
				{
					if (hit.collider.tag != "laserGoal")
					{
						GetComponent<Objective>().objective = true;
						print ("YAY!");
					}
					else
					{
						GetComponent<Objective>().objective = false;
						print ("NO!");
					}
				}

				// Now update the collisions.
				BoxCollider col = temp.AddComponent<BoxCollider>();
				float length 		= (end-start).magnitude;

				if (!horizontal)	col.size 	= new Vector3(length, 0.2f, 1.0f);
				else 				col.size 	= new Vector3(0.2f, length, 1.0f);
				col.center 	= start+(end-start)/2f;
			}
		} while (reflection == true && maxBounce < 20);
	}

	// Use this for initialization
	void Start ()
	{
		UpdateLaser ();
	}
	
	// Update is called once per frame
	void Update () {
	
		//Debug.DrawRay(this.transform.position, this.transform.forward*100.0f, Color.white); 

	}
}
