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

public class CameraController : MonoBehaviour
{
    [SerializeField] private CinemachineVirtualCamera cinemachineVirtualCamera;
    [SerializeField] private float moveSpeed = 10f;
    [SerializeField] private float rotationSpeed = 100f;
    [SerializeField] private float zoomSpeed = 0f;
    [SerializeField] private float zoomIncreaseAmount = 1f;
    private const float MIN_FOLLOW_Y_OFFSET = 2f;
    private const float MAX_FOLLOW_Y_OFFSET = 12f;

    private Vector3 targetFollowOffset;
    private CinemachineTransposer cinemachineTransposer;
    private void Start() {
        cinemachineTransposer = cinemachineVirtualCamera.GetCinemachineComponent<CinemachineTransposer>();
        targetFollowOffset = cinemachineTransposer.m_FollowOffset;
    }
    private void Update() {
        HandleMovement();
        HandleRotation();
        HandleZoom();
    }

    private void HandleMovement() {
        Vector3 inputMoveDirection = InputManager.Instance.GetCameraMoveVector();

        Vector3 moveVector = 
            transform.forward   *   inputMoveDirection.y +
            transform.right     *   inputMoveDirection.x;
        transform.position += moveVector * Time.deltaTime * moveSpeed;
    }

    private void HandleRotation() {
        Vector3 rotationVector = new Vector3(0,0,0); 
        rotationVector.y = InputManager.Instance.GetCameraRotateAmount();

        transform.eulerAngles += rotationVector * Time.deltaTime * rotationSpeed;
    }

    private void HandleZoom() {
        targetFollowOffset.y += InputManager.Instance.GetCameraZoomAmount() * zoomIncreaseAmount;

        targetFollowOffset.y = Mathf.Clamp(targetFollowOffset.y,MIN_FOLLOW_Y_OFFSET,MAX_FOLLOW_Y_OFFSET);
        cinemachineTransposer.m_FollowOffset = Vector3.Lerp(
                                                        cinemachineTransposer.m_FollowOffset,targetFollowOffset,
                                                        Time.deltaTime * zoomSpeed) ;
    }
}