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

public class CameraController : MonoBehaviour {

	public Transform Actor;
	public GameObject Canvas;
	public GameObject Hud;

	private Vector3 _firstPosition;
	private Vector3 _firstRotation;

	private float _timer;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if (Actor.gameObject.activeSelf == false)
			return;

		if (_timer >= 0f) {
			_timer -= Time.deltaTime;
			Vector3 pos = transform.position;
			pos.y = Mathf.Lerp (_firstPosition.y, 2.5f, 1f - _timer / 0.7f);
			transform.position = pos;
			transform.eulerAngles = Vector3.Lerp (_firstRotation, new Vector3 (40f, 0f, 0f), 1f - _timer / 0.7f);
		} else if (_timer < 0f && Hud.activeInHierarchy == false)
			Hud.SetActive (true);
		
		Vector3 viewPort = Camera.main.WorldToViewportPoint (Actor.position);
		Vector3 position = transform.position;

		if (viewPort.x < 0.4f)
		//if (Mathf.Lerp (transform.position.x, _actor.position.x, Time.deltaTime) > -18f)
			position.x = Mathf.Lerp (transform.position.x, Actor.position.x, Time.deltaTime);
		if (viewPort.x > 0.6f)
		//if (Mathf.Lerp (transform.position.x, _actor.position.x, Time.deltaTime) < 18f)
			position.x = Mathf.Lerp (transform.position.x, Actor.position.x, Time.deltaTime);

		if (viewPort.z - 2.8f < 0.4f)
		//if (Mathf.Lerp (transform.position.z, _actor.position.z, Time.deltaTime) > -11f)
			position.z = Mathf.Lerp (transform.position.z, Actor.position.z - 3f, Time.deltaTime);
		if (viewPort.z - 2.8f > 0.6f)
		//if (Mathf.Lerp (transform.position.z, _actor.position.z, Time.deltaTime) < 18f)
			position.z = Mathf.Lerp (transform.position.z, Actor.position.z-3f, Time.deltaTime);

		transform.position = position;
	}

	public void StartGame () {
		Actor.gameObject.SetActive (true);
		Actor.transform.position = new Vector3 (19f, 0.15f, -4.75f);
		Actor.GetComponent<TapMechanix> ().Start();
		for (int i = 0; i < Canvas.transform.childCount; i++)
			Canvas.transform.GetChild (i).gameObject.SetActive (false);

		_timer = 0.9f;

		_firstPosition = transform.position;
		_firstRotation = transform.eulerAngles;
	}
}
