using UnityEngine;
using System.Collections;

public class ActiveCamera : MonoBehaviour {

	#region Private Variables
	private Vector3 _origin;
	private Camera _camera;
	private Vector3 _targetLoc;

	private Color _originalColor;
	private Color _targetColor = Color.black;

	private float _originalSize;
	private float _roomSize = 12.5f;
	private float _targetSize;

	private float _duration = 5f;
	private float _startTime;
	private bool _transitioning = false;
	#endregion


	#region Events
	void Awake () {

		_camera = GetComponent <Camera> ();

		_targetLoc = _origin = transform.position;
		_originalColor = _camera.backgroundColor;
		_originalSize = _camera.orthographicSize;

	}

	void Update () {
		
		if (_transitioning) {

			float t = (Time.time - _startTime) / _duration;
			if (t >= 1f) _transitioning = false;

			transform.position = Vector3.Lerp (transform.position, _targetLoc, t);
			_camera.backgroundColor = Color.Lerp (_camera.backgroundColor, _targetColor, t);
			_camera.orthographicSize = Mathf.Lerp (_camera.orthographicSize, _targetSize, t);

		}
		
	}
	#endregion


	#region Methods
	public void Switch () {

		GameManager.canMove = false;

		if (!_transitioning) {
			_targetLoc = GameObject.FindGameObjectWithTag ("CameraView").transform.position;
			_startTime = Time.time;
			_targetColor = Color.black;
			_targetSize = _roomSize;
			_transitioning = true;
		}

	}

	public void Reset () {

		GameManager.canMove = true;

		if (!_transitioning) {
			_targetLoc = _origin;
			_startTime = Time.time;
			_targetColor = _originalColor;
			_targetSize = _originalSize;
			_transitioning = true;
		}

	}
	#endregion
}
