using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour
{
	static CameraController instance = null;

	[SerializeField] Vector2 rotationSpeedFactor = new Vector2 (0.2f, 0.2f);

	[SerializeField]public float minDistance = 3f;
	[SerializeField]public float maxDistance = 30f;
	[SerializeField]public float cameraMinYAngle = 30f;
	[SerializeField]public float cameraMaxYAngle = 90f;
	float distance = 10f;
	float needDistance = 10f;

	
	Vector3 needPosition = Vector3.zero;

	Vector2 cameraAngles = new Vector2 (0, 75);
	Vector2 needCameraAngles = new Vector2 (0, 75);
	Vector2 startTouch = Vector2.zero;
	Transform selfTransform = null;

	Camera controlCamera = null;
	
	public static void SetNeedDistance(float distance)
	{
		if (instance != null)
		{
			instance.needDistance = distance;
		}
	}

	public static void SetBorders(float distance, float minDistance, float maxDistance, float cameraMinYAngle, float cameraMaxYAngle)
	{
		if (instance != null)
		{
			instance.distance = distance;
			instance.minDistance = minDistance;
			instance.maxDistance = maxDistance;
			instance.needDistance = Mathf.Min(instance.needDistance, maxDistance);
			instance.needDistance = Mathf.Max(instance.needDistance, minDistance);
			instance.cameraMinYAngle = cameraMinYAngle;
			instance.cameraMaxYAngle = cameraMaxYAngle;
			instance.cameraAngles.y = Mathf.Max(instance.cameraAngles.y, cameraMinYAngle);
			instance.cameraAngles.y = Mathf.Min(instance.cameraAngles.y, cameraMaxYAngle);
		}
	}

	public static void SetPosition(Vector3 position, bool useAnimation = true)
	{
		if (instance != null)
		{
			instance.needPosition = position;
			if (!useAnimation)
			{
				instance.selfTransform.position = position;
			}
		}
	}

	void Awake()
	{
		instance = this;
		selfTransform = transform;
		controlCamera = GetComponentInChildren<Camera> ();
	}

	void Start ()
	{
		selfTransform.localEulerAngles = new Vector3 (cameraAngles.y, cameraAngles.x);
	}
	
	void Update ()
	{
		selfTransform.position = selfTransform.position * 0.9f + needPosition * 0.1f;
		if (Input.GetMouseButtonDown (0))
		{
			startTouch = Input.mousePosition;
		}
		if (Input.GetMouseButton (0))
		{
			Vector2 delta = (Vector2)Input.mousePosition - startTouch;
			startTouch = Input.mousePosition;
			cameraAngles.x += delta.x * rotationSpeedFactor.x;
			cameraAngles.y += delta.y * rotationSpeedFactor.y;
			cameraAngles.y = Mathf.Max(cameraAngles.y, cameraMinYAngle);
			cameraAngles.y = Mathf.Min(cameraAngles.y, cameraMaxYAngle);
		}
		needCameraAngles = needCameraAngles * 0.9f + cameraAngles * 0.1f;
		selfTransform.localEulerAngles = new Vector3 (needCameraAngles.y, needCameraAngles.x);

		float scroll = Input.GetAxis ("Mouse ScrollWheel");
		if (scroll < 0)
		{
			needDistance *= 1.2f;
			needDistance = Mathf.Min(needDistance, maxDistance);
		}
		if (scroll > 0)
		{
			needDistance *= 0.8f;
			needDistance = Mathf.Max(needDistance, minDistance);
		}
		if (needDistance != distance && controlCamera != null)
		{
			distance = distance * 0.9f + needDistance * 0.1f;
			controlCamera.transform.localPosition = new Vector3(0, 0, -distance);
		}
	}
}
