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

public class CameraControllerCinemachine : MonoBehaviour
{
    public float speed = 3f;

    public float maxStartX = 1720f;
    public float minStartX = 200f;
    public float maxStartY = 880f;
    public float minStartY = 200f;


    public float maxZoom = 5;
    public float minZoom = 3;

    Camera m_Cam;
    CinemachineVirtualCamera vCam;

    public float panSpeed = 20f;
    private bool doPan = false;
    private Vector3 panOrigin;
    private Vector3 panDifference;
    private Vector3 lastCamSite;

    private void Start()
    {
        vCam = GetComponent<CinemachineVirtualCamera>();
    }

    void Update()
    {
        if (Manager.Instance.StillPlaying())
        {

            // Teclado

            if (Input.GetKey(KeyCode.W))
            {
                transform.position += new Vector3(0f, speed * Time.unscaledDeltaTime, 0f);
            }
            if (Input.GetKey(KeyCode.S))
            {
                transform.position -= new Vector3(0f, speed * Time.unscaledDeltaTime, 0f);
            }
            if (Input.GetKey(KeyCode.A))
            {
                transform.position -= new Vector3(speed * Time.unscaledDeltaTime, 0f, 0f);
            }
            if (Input.GetKey(KeyCode.D))
            {
                transform.position += new Vector3(speed * Time.unscaledDeltaTime, 0f, 0f);
            }

            // Agarrando
            if (Input.GetMouseButton(2))
            {
                panDifference = Input.mousePosition;
                if (!doPan)
                {
                    print("doing");
                    doPan = true;
                    panOrigin = Input.mousePosition;
                    lastCamSite = transform.position;
                }
            }
            else
            {
                doPan = false;
            }
            if (doPan)
            {
                Vector3 newPos = (panOrigin - panDifference) * panSpeed * vCam.m_Lens.FieldOfView;
                Vector3 final = lastCamSite + newPos;
                final.z = transform.position.z;
                transform.position = lastCamSite + newPos;
            }

            // Zoom
            vCam.m_Lens.FieldOfView = Mathf.Clamp(vCam.m_Lens.FieldOfView - Input.mouseScrollDelta.y * 2, minZoom, maxZoom);

            //Limits
            float x = Mathf.Clamp(transform.position.x, minStartX, maxStartX);
            float y = Mathf.Clamp(transform.position.y, minStartY, maxStartY);
            float z = transform.position.z;
            transform.position = new Vector3(x, y, z);
        }
    }
}
