using UnityEngine;

public class Movement : MonoBehaviour
{
    [SerializeField] private float m_speed = 5.0f;
    [SerializeField] private float m_rotateSpeed = 2.0f;
    
    private Player m_player = default;
    private Vector3 m_movement = Vector3.zero;
    private Quaternion m_lookRotation = Quaternion.identity;
    private Vector3 m_movementInput => InputManager.Movement;

    private bool m_hasInputMovement => m_movementInput != Vector3.zero;
    
    public void Initialise(Player _player)
    {
        m_player = _player;
    }

    private void Update()
    {
        if (m_hasInputMovement)
        {
            m_movement = m_player.Camera.transform.TransformDirection(m_movementInput);
            m_movement.y = 0.0f;
            var lookRotation = Quaternion.LookRotation(m_movement, Vector3.up);
            m_lookRotation = Quaternion.RotateTowards(m_lookRotation, lookRotation, m_rotateSpeed);

            m_player.Animator.SetBool(AnimationHash.Walking, true);
        }
        else
        {
            m_player.Animator.SetBool(AnimationHash.Walking, false);
        }
    }

    private void FixedUpdate()
    {
        if (m_player)
        {
            if (m_hasInputMovement)
            {
                var movement = m_movement.normalized * (m_speed * Time.deltaTime);
                m_player.Rigidbody.MovePosition(m_player.transform.position + movement);
                m_player.Rigidbody.MoveRotation(m_lookRotation);
            }
        }
    }
}
