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

public class CameraController : 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;

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

    private void Start()
    {
        m_Cam = Camera.main;
    }

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

            // Teclado

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

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

            // Zoom
            m_Cam.fieldOfView = Mathf.Clamp(m_Cam.fieldOfView - Input.mouseScrollDelta.y*2, minZoom, maxZoom);

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