using UnityEngine;
using System.Collections;

public class CameraFollower : MonoBehaviour
{
	[SerializeField]
	float duration = 1.7f;
	public bool moveDown = false;

	public Renderer lastRendererInView;
    
	public void SetupCamera()
	{
		/*
		origin = this.transform.position;
		Vector3 a = controller.loadedPlayer[0].position;
		Vector3 b = controller.loadedPlayer[1].position;
		lookTarget = (b - a).normalized;
		lookTarget = a + (.5f * lookTarget);

		offset = transform.position - lookTarget;
		transform.LookAt(lookTarget);
		*/

		/* for (int i = 0; i < loadedPlayer.Count; i++)
		{
			
		}

		player = controller.loadedPlayer == null ? null : controller.loadedPlayer.gameObject;

		if (player != null)
        {
			//temp set the pos good
			transform.position.Set(player.transform.position.x, 4, 9);

			//Calculate and store the offset value by getting the distance between the player's position and camera's position.
			
		}*/
	}

    internal void SwitchTarget(Transform cameraTarget)
    {
		moveDown = false;
		lastRendererInView = GameObject.FindWithTag("lastRendererInView").GetComponent<Renderer>();
        StartCoroutine(LerpObjectToTarget(transform.position, cameraTarget.position, 1.7f));
    }

    // LateUpdate is called after Update each frame
    void Update()
    {
        if (moveDown)
        {
            // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
            transform.Translate((Vector3.down * 29f)* Time.deltaTime, Space.World);//lookTarget + offset;
		}
    }

	private IEnumerator LerpObjectToTarget(Vector3 start, Vector3 end, float time)
    {
        float timer = 0;
        while (timer < time)
        {
			//transform.LookAt(end);
            transform.position = Vector3.Lerp(start, end, timer / time);

            timer += Time.deltaTime;
            yield return null;
        }
    }

}