using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {
    public Transform target;
    public Transform zoomTarget;

    private float speed = 0.7f;
    private float walkSpeed = 0.7f;
    private float zoomSpeed = 0.01f;
    private Vector3 origin;

    private float zooming = 0.0f;

    public bool zoomIn = false;


    

	// Use this for initialization
	void Start () {
        origin = transform.position;
        zoomTarget = target;

	}

    public void startZoom() {
        speed = zoomSpeed;
        zoomIn = true;
    }

    public void startZoom(Transform zt) {
        zoomTarget = zt;
        speed = zoomSpeed;
        zoomIn = true;
    }

    public void zoomOut() {
        zooming = 3.0f;
        zoomIn = false;
        zoomTarget = target;
    }
	
	// Update is called once per frame
	void Update () {
        Vector3 camPos;

        if (zooming > 0) {
            zooming -= Time.deltaTime;

            if (zooming < 2f) {
                speed = Mathf.Lerp(speed, walkSpeed, 0.02f);
            }
            
            if (zooming <= 0) {
                zoomIn = false;
                speed = walkSpeed;
                zoomTarget = target;
            }
        } 


        if (zoomIn) {
            camPos = new Vector3(zoomTarget.position.x, zoomTarget.position.y + 4f, zoomTarget.position.z - 3);
            transform.position = Vector3.Lerp(transform.position, camPos, zoomSpeed);
        } else {
            camPos = new Vector3(target.position.x, origin.y, origin.z);
        }
        
        transform.position = Vector3.Lerp(transform.position, camPos, speed);
        transform.LookAt(zoomTarget);
	}
}
