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

public class HUDbehaviors : MonoBehaviour {

	public List <GameObject> towers = new List <GameObject> (); //Array to store placed towers
	public GameObject tower; //tower source object prefab
	public GameObject building; //The building queued to be placed

	//private Transform towerHolder;

	void OnGUI()
	{
		Vector3 p = new Vector3();
		Camera  c = Camera.main;
		Event   e = Event.current;
		Vector2 mousePos = new Vector2();

		// Get the mouse position from Event.
		// Note that the y position from Event is inverted.
		mousePos.x = e.mousePosition.x;
		mousePos.y = c.pixelHeight - e.mousePosition.y;

		p = c.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, c.nearClipPlane));

		GUILayout.BeginArea(new Rect(20, 20, 250, 120));
		GUILayout.Label("Screen pixels: " + c.pixelWidth + ":" + c.pixelHeight);
		GUILayout.Label("Mouse position: " + mousePos);
		GUILayout.Label("World position: " + p.ToString("F3"));
		GUILayout.EndArea();
	}

	// Use this for initialization
	void Start () {
		//towerHolder = new GameObject ("Board").transform;
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButtonDown (0)) {

			Vector3 coords3D = new Vector3 ();
			Camera c = Camera.main;
			Vector2 mousePos = new Vector2();

			// Get the mouse position from Event.
			// Note that the y position from Event is inverted.
			mousePos.x = Input.mousePosition.x;
			mousePos.y = c.pixelHeight - Input.mousePosition.y;

			coords3D = c.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 10f));
			coords3D.z = coords3D.z * -1f;

			placeTower (coords3D );
		}
	}

	public void selectTower() {
		building = tower;
	}

	public void placeTower(Vector3 position) {
		GameObject newTower;

		newTower = Instantiate (tower, position, Quaternion.identity) as GameObject;
		//newTower.transform.SetParent (towerHolder);

		towers.Add(newTower);
	}
		
}
