using UnityEngine;
using System.Collections;

public class MoveCamera : MonoBehaviour 
{
	private Camera current = null;
	
	private bool rotatingX = false;
	private bool rotatingY = false;
	private float radius;
	private float targetY;
	private float xAngle;
	private float yAngle;
	private float xStep = 2.0f;
	private float yStep = 1.0f;

	void Start()
	{
		if (current == null)
		{
			if (Camera.main != null)
			{
				current = Camera.main;
				radius  = 5.0f;
				targetY = 0.0f;
				xAngle  = 0.0f;
				yAngle  = -80.0f;
				TransformCamera();
			}
		}
	}
	
	void Update() 
	{
		if (current == null)
		{
			return;
		}
	
		ControlCamera();
		RotateCamera();
		UpdateCamera();
		TransformCamera();
	}

	void ControlCamera()
	{
		if (Input.GetMouseButton(0))
        {
            xAngle -= Input.GetAxis("Mouse X") * 4.0f;
            yAngle += Input.GetAxis("Mouse Y") * 4.0f;
        }		

 		if (Input.GetAxis("Mouse ScrollWheel") > 0)
		{
			radius -= 0.5f;
		}
		if (Input.GetAxis("Mouse ScrollWheel") < 0)
		{
			radius += 0.5f;
		}		
		
		if (Input.GetKey(KeyCode.S))
		{
			yAngle -= 0.99f;
		}
		else if (Input.GetKey(KeyCode.DownArrow))
		{
			yAngle -= 0.99f;
		}
		if (Input.GetKey(KeyCode.W))
		{
			yAngle += 0.99f;
		}
		else if (Input.GetKey(KeyCode.UpArrow))
		{
			yAngle += 0.99f;
		}
		if (Input.GetKey(KeyCode.A))
		{
			xAngle -= 5.0f;
		}
		else if (Input.GetKey(KeyCode.LeftArrow))
		{
			xAngle -= 5.0f;
		}
		if (Input.GetKey(KeyCode.D))
		{
			xAngle += 5.0f;
		}
		else if (Input.GetKey(KeyCode.RightArrow))
		{
			xAngle += 5.0f;
		}
		if (Input.GetKey(KeyCode.PageUp))
		{
			targetY += 0.02f;
			if (Input.GetKey(KeyCode.LeftShift))
			{
				yAngle = 0.0f;
			}
		}			
		if (Input.GetKey(KeyCode.PageDown))
		{
			targetY -= 0.02f;
			if (Input.GetKey(KeyCode.LeftShift))
			{
				yAngle = 0.0f;
			}
		}			
		if (Input.GetKey(KeyCode.KeypadPlus))
		{
			radius -= 0.1f;
		}
		else if (Input.GetKey(KeyCode.Plus))
		{
			radius -= 0.1f;
		}
		if (Input.GetKey(KeyCode.KeypadMinus))
		{
			radius += 0.1f;
		}			
		else if (Input.GetKey(KeyCode.Minus))
		{
			radius += 0.1f;
		}			
	}

	void RotateCamera()
	{
		if (Input.GetKeyDown(KeyCode.Backspace))
		{
			xAngle = xAngle > 180.0f ? xAngle - 180.0f : xAngle + 180.0f;
		}
		if (Input.GetKeyDown(KeyCode.Alpha0))
		{
			xAngle    = 0.0f;
			rotatingX = false;
			rotatingY = false;
		}
		if (Input.GetKeyDown(KeyCode.Period))
		{
			xAngle += 45.0f;
		}
		if (Input.GetKeyDown(KeyCode.Comma))
		{
			xAngle -= 45.0f;
		}
		if (Input.GetKeyDown(KeyCode.X))
		{
			rotatingX = !rotatingX;
			xStep = Input.GetKey(KeyCode.LeftShift) ? -2.0f : 2.0f;
		}
		else if (Input.GetKeyDown(KeyCode.Y))
		{
			rotatingY = !rotatingY;
			yStep = Input.GetKey(KeyCode.LeftShift) ? -0.5f : 0.5f;
		}
		if (rotatingX)
		{
			xAngle += xStep;
		}
		if (rotatingY)
		{
			yAngle += yStep;
			if (yAngle > -0.01f || yAngle < -90.0f)
			{
				yStep = -yStep;
			}
		}
	}
	
	void UpdateCamera()
	{
		if (current != null)
		{
			if (Input.GetKeyDown(KeyCode.F1))
			{
				yAngle    = -80f;
				xAngle    = 0.0f;
				rotatingX = false;
				rotatingY = false;
			}
			if (Input.GetKeyDown(KeyCode.F2))
			{
				yAngle    = -60.0f;
				xAngle    = 0.0f;
				rotatingX = false;
				rotatingY = false;
			}
			if (Input.GetKeyDown(KeyCode.F3))
			{
				yAngle    = -45.0f;
				xAngle    = 0.0f;
				rotatingX = false;
				rotatingY = false;
			}
			if (Input.GetKeyDown(KeyCode.F4))
			{
				yAngle    = -0.01f;
				xAngle    = 0.0f;
				rotatingX = false;
				rotatingY = false;
			}
			if (Input.GetKeyDown(KeyCode.F5))
			{
				yAngle    = -90.0f;
				xAngle    = 0.0f;
				rotatingX = false;
				rotatingY = false;
			}
			if (Input.GetKeyDown(KeyCode.F6))
			{
				yAngle    = -90.0f;
				xAngle    = 90.0f;
				rotatingX = false;
				rotatingY = false;
			}
			if (Input.GetKeyDown(KeyCode.F7))
			{
				yAngle    = -90.0f;
				xAngle    = 180.0f;
				rotatingX = false;
				rotatingY = false;
			}
			if (Input.GetKeyDown(KeyCode.F8))
			{
				yAngle    = -90.0f;
				xAngle    = 270.0f;
				rotatingX = false;
				rotatingY = false;
			}
			if (Input.GetKeyDown(KeyCode.F12))
			{
				radius    = 5.0f;
				targetY   = 0.0f;
				xAngle    = 0.0f;
				yAngle    = -80.0f;
				rotatingX = false;
				rotatingY = false;
			}
		}
	}

	void TransformCamera()
	{
		radius = radius < 0.01f ? 0.01f : radius;
		radius = radius > 10.0f ? 10.0f : radius;
		xAngle = xAngle < 0.0f ? xAngle + 360.0f : xAngle;
		xAngle = xAngle > 360.0f ? xAngle - 360.0f : xAngle;
		yAngle = yAngle < -179.99f ? -179.99f : yAngle;
		yAngle = yAngle >= -0.01f ? -0.01f : yAngle;
		targetY = targetY < 0.0f ? 0.0f : targetY;
		targetY = targetY > 2.0f ? 2.0f : targetY;
		
		Vector3 newPos;
		newPos.x = radius * Mathf.Sin(xAngle * Mathf.PI / 180.0f) * Mathf.Sin(Mathf.Abs(yAngle) * Mathf.PI / 180.0f);
		newPos.y = (targetY + radius) * (Mathf.Cos(yAngle * Mathf.PI / 180.0f));
		newPos.z = radius * Mathf.Cos(xAngle * Mathf.PI / 180.0f) * Mathf.Sin(yAngle * Mathf.PI / 180.0f);
		current.transform.position = newPos;
		current.transform.LookAt(new Vector3(0.0f, targetY, 0.0f));	
	}
	
	void OnGUI()
	{
		GUI.Label(new Rect(Screen.width - 140, 10, 80, 24), "X Rotation");
		GUI.Label(new Rect(Screen.width - 30, 10, 30, 24), xAngle.ToString("##0"));
		GUI.Label(new Rect(Screen.width - 140, 26, 80, 24), "Y Rotation");
		GUI.Label(new Rect(Screen.width - 30, 26, 30, 24), (yAngle + 90.0f).ToString("##0"));
		GUI.Label(new Rect(Screen.width - 140, 42, 80, 24), "Distance");
		GUI.Label(new Rect(Screen.width - 30, 42, 30, 24), radius.ToString("##0.#"));
		GUI.Label(new Rect(Screen.width - 140, 58, 80, 24), "Target Y");
		GUI.Label(new Rect(Screen.width - 30, 58, 30, 24), targetY.ToString("#0.#"));
	}
}
