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

public class cameraLook : MonoBehaviour {

	public float rotateSpeed = 90.0f;
	public float moveSpeed = 2.0f;
	public GameObject sphereMesh;
	public int layerMask_ClickSurface = 10;
	public float rayMaxDistance = 1000.0f;
//	public int Health = 5;

	void Start () {
		
	}

	void Update () {

		// Raycast Hit
		if (Input.GetButtonDown ("Fire1")) {
//			if (Health > 0 && Input.GetButtonDown ("Fire1")) {
				
			Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			RaycastHit hitObject;
			if (Physics.Raycast (ray, out hitObject, rayMaxDistance, 1 << layerMask_ClickSurface)) {
				GameObject SphereColliderInstance = Instantiate (sphereMesh, hitObject.point, hitObject.transform.rotation);
				SphereColliderInstance.tag = "Sphere Collider Expansion";
				SphereColliderExpansion sphereColliderExpansion = SphereColliderInstance.GetComponent<SphereColliderExpansion> ();
				sphereColliderExpansion.enabled = true;
			}
//			Health--;
		}

		// Translate Camera
		if (Input.GetKey (KeyCode.W))
			transform.Translate (moveSpeed * Vector3.forward * Time.deltaTime, Space.Self);
		if (Input.GetKey (KeyCode.S))
			transform.Translate (moveSpeed * Vector3.back * Time.deltaTime, Space.Self);
		if (Input.GetKey (KeyCode.A))
			transform.Translate (moveSpeed * Vector3.left * Time.deltaTime, Space.Self);
		if (Input.GetKey (KeyCode.D))
			transform.Translate (moveSpeed * Vector3.right * Time.deltaTime, Space.Self);

		if (transform.GetChild (0).GetComponent<Camera> ().orthographic) {
			if (Input.GetKey (KeyCode.Q))
				transform.GetChild (0).GetComponent<Camera> ().orthographicSize += -moveSpeed/5 * Time.deltaTime;
			if (Input.GetKey (KeyCode.E))
				transform.GetChild (0).GetComponent<Camera> ().orthographicSize += moveSpeed/5 * Time.deltaTime;
		}
		else {
			if (Input.GetKey (KeyCode.Q))
			transform.GetChild (0).Translate (-moveSpeed * transform.GetChild (0).transform.forward * Time.deltaTime, Space.World);
		if (Input.GetKey (KeyCode.E))
			transform.GetChild (0).Translate (moveSpeed * transform.GetChild (0).transform.forward * Time.deltaTime, Space.World);
	}


		// Rotate Camera
		if (Input.GetKey (KeyCode.Z))
				transform.Rotate (rotateSpeed*Vector3.up * Time.deltaTime,Space.World);
		if (Input.GetKey (KeyCode.C))
				transform.Rotate (rotateSpeed*Vector3.down * Time.deltaTime,Space.World);

		// Rotate Camera using Mouse
		if (Input.GetMouseButton (1)) {
			float rotationData = Input.GetAxis ("Mouse X") * rotateSpeed;
			transform.Rotate (0, rotationData * Time.deltaTime, 0, Space.World);
		}
		if (Input.GetMouseButtonDown (2)) {
			transform.GetChild (0).GetComponent<Camera> ().orthographic = !transform.GetChild (0).GetComponent<Camera> ().orthographic;
		}
	}
}
