using System;
using Unicorn.System;
using UnityEngine;
using UnityEngine.InputSystem;

namespace Unicorn.Player
{
    public class PlayerMovement : MonoBehaviour
    {
        public float moveSpeed;
        public float jumpForce;
    
        private Rigidbody2D _rb;
        private UnicornInputSystem _unicorn;
    
        private void OnDisable()
        {
            _unicorn.Player.Jump.performed -= Jump;
        }
        void Start()
        {
            _rb =  GetComponent<Rigidbody2D>();
            _unicorn = UnicornInput.Unicorn;
            _unicorn.Player.Jump.performed += Jump;
        }



        private void Jump(InputAction.CallbackContext obj)
        {
            _rb.linearVelocityY = jumpForce;
        }

        private void FixedUpdate()
        {
            var readValue = _unicorn.Player.Horizontal.ReadValue<float>();
            _rb.linearVelocityX = readValue * moveSpeed;
            if (_unicorn.Player.Down.IsPressed() && _rb.linearVelocityY < jumpForce/1.5)
                _rb.linearVelocityY = Math.Min(0, _rb.linearVelocityY);
        }
    }
}
