/**
 * Skript je pouzit pro kontrolu ovladani kamery
 */
using UnityEngine;
using System.Collections;
/**
 * Author:			Karel Zavrel
 * 
 * Thesis:			An interactive application for development of creativity
 * Application:		Little Builder
 */
public class TouchCameraControlScript : MonoBehaviour {
	/**
	 * promenne:
	 * 	- prazdny nadrazeny objekt hlavni kamery, je pouzit jako rotacni bod kamery, ktera se toci okolo modelu
	 * 	- pocatecni natoceni nadrazeneho objektu kamery
	 * 	- pocatecni pozice nadrazeneho objektu kamery
	 * 	- hlavni kamera
	 * 	- rychlost otaceni kamery okolo modelu
	 * 	- rychlost pohybu kamery po scene
	 */
	private GameObject emptyObjForCam;
	private Quaternion startingEmptyObjRotation;
	private Vector3 startingEmptyObjPosition;
	private Camera mainCamera;
	private float rotationSpeed = 3.0f, movementSpeed = 5.0f;
	/**
	 * funkce pro inicializaci promennych
	 */
	void Start () {
		/**
		 * nalezeni nadrazeneho objektu kamery a nastaveni pocatecni pozice a natoceni
		 */
		emptyObjForCam = GameObject.Find ("EmptyObject_CameraSphere");
		startingEmptyObjRotation = emptyObjForCam.transform.rotation;
		startingEmptyObjPosition = emptyObjForCam.transform.position;
		/**
		 * nalezeni hlavni kamery
		 */
		mainCamera = GameObject.Find ("Main Camera").GetComponent<Camera> ();
	}
	/**
	 * funkce volana jednou v kazdem framu
	 */
	void Update () {
		/**
		 * otaceni kamerou neni povoleno, kdyz uzivatel pohybuje kostickou po scene
		 */
		if (InnerStoringScript.getIsRotationPermitted ()) {
			controlRotation ();
		}
		/**
		 * priblizovani a oddalovani a presun kamery neni povoleno, kdyz uzivatel pohybuje kostickou po scene
		 */
		if (InnerStoringScript.getIsScalePermitted ()) {
			controlZoom ();
			controlMovement ();
		}
		/**
		 * nastaveni pocatecni pozice a natoceni kamery
		 */
		if (Input.touchCount == 4) {
			emptyObjForCam.transform.rotation = Quaternion.Lerp(emptyObjForCam.transform.rotation, startingEmptyObjRotation, rotationSpeed * Time.deltaTime);
			emptyObjForCam.transform.position = Vector3.Lerp (emptyObjForCam.transform.position, startingEmptyObjPosition, movementSpeed * Time.deltaTime);
		}
	}
	/**
	 * funkce ridici otaceni kamery okolo modelu
	 */
	private void controlRotation(){
		if ((Input.touchCount == 1) && (Input.GetTouch (0).phase == TouchPhase.Moved)) {
			emptyObjForCam.transform.RotateAround(emptyObjForCam.transform.up, Mathf.Deg2Rad * (rotationSpeed * Input.GetAxis("Mouse X")));
			emptyObjForCam.transform.RotateAround(emptyObjForCam.transform.right, Mathf.Deg2Rad * (-rotationSpeed * Input.GetAxis("Mouse Y")));
		}
	}
	/**
	 * funkce ridici priblizovani a oddalovani kamery
	 */
	private void controlZoom(){
		if ((Input.touchCount == 2) && (Input.GetTouch (0).phase == TouchPhase.Moved)) {
			zoomModel(Input.GetTouch(0), Input.GetTouch(1));
		}
	}
	/**
	 * funkce pro priblizeni ci oddaleni kamery v zavislosti na zmene vzdalenosti mezi dvema prsty
	 * 	- pribizovani je reprezentovano zmenou meritka nadrazeneho objektu kamery
	 */
	private void zoomModel(Touch firstT, Touch secondT){
		float previousRange = ((firstT.position - firstT.deltaPosition) - (secondT.position - secondT.deltaPosition)).magnitude;
		float range = (firstT.position - secondT.position).magnitude;
		float difference = (previousRange - range) / 100.0f;// * 2.0f;
		Vector3 previousScale = emptyObjForCam.transform.localScale;
		if(((emptyObjForCam.transform.localScale.x > 0.1f) && (difference < 0.0f)) || ((emptyObjForCam.transform.localScale.x < 4.0f) && (difference > 0.0f))){
			previousScale += new Vector3 (difference, difference, difference);
		}
		emptyObjForCam.transform.localScale = previousScale;
	}
	/**
	 * funkce pro rizeni pohybu kamery ve scene
	 */
	private void controlMovement(){
		if (Input.touchCount == 3 && (Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (1).phase == TouchPhase.Moved && Input.GetTouch (2).phase == TouchPhase.Moved)) {
			Vector3 position = emptyObjForCam.transform.position;
			position.x = position.x + Input.GetAxis ("Mouse X") * -1.0f;
			position.y = position.y + Input.GetAxis ("Mouse Y") * -1.0f;
			if (position.x <= 15.0f && position.x >= -15.0f && position.y <= 15.0f && position.y >= -15.0f) {
				emptyObjForCam.transform.position = Vector3.Lerp (emptyObjForCam.transform.position, position, movementSpeed * Time.deltaTime);
			}
		}
	}
}