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

public class CameraController : MonoBehaviour
{
    [SerializeField]
    private CameraSettings _settings;

    [SerializeField]
    private PlayerInputHandler _inputHandler;

    private Vector2 _cameraRotation;

    void FixedUpdate()
    {
        float deltaTime = Time.fixedDeltaTime;

        _cameraRotation += _inputHandler.InputState.Look * ( deltaTime * _settings.LookSpeed );
        _cameraRotation.y = Mathf.Clamp( _cameraRotation.y, _settings.LookYMin, _settings.LookYMax );
        _inputHandler.SetCameraRotation(_cameraRotation.x * -180.0f);
    }

    private void LateUpdate()
    {
        if ( _cameraRotation.x > 1.0f )
        {
            _cameraRotation.x -= 2.0f;
        }
        else if ( _cameraRotation.x < -1.0f )
        {
            _cameraRotation.x += 2.0f;
        }

        Quaternion rot = Quaternion.Euler( _cameraRotation.y * 180.0f, _cameraRotation.x * 180.0f, 0f );
        Vector3 pos = rot * ( Vector3.back * _settings.LookDistance );

        transform.localPosition = pos;
        transform.rotation = rot;
    }
}