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

[RequireComponent(typeof(CinemachineFreeLook))]
public class TargetRecentering : MonoBehaviour
{
    [SerializeField] [Range(0, 1)] private float _inputMagnitude = 0.1f;
    
    [SerializeField] private InputActionReference _moveInput;
    
    private CinemachineFreeLook _freeLook;

    private void Start()
    {
        _freeLook = GetComponent<CinemachineFreeLook>();
    }

    private void Update()
    {
        // if there is any horizontal movement input...
        var input = _moveInput.action.ReadValue<Vector2>();
        var result = Mathf.Abs(input.x) > Mathf.Abs(input.y) && input.magnitude > _inputMagnitude;
        
        // ...enable target recentering in order to turn the camera with the player
        _freeLook.m_RecenterToTargetHeading.m_enabled = result;
    }
}
