using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;

public class InputManager : MonoBehaviour
{
    [SerializeField] 
    [Range(0f, 4f)]
    private float inputBufferTime = .25f;
        
    public Vector2 moveInput { get; private set; } 
    public Vector2 aimInput { get; private set; }
    
    public bool jumpPerformed{ get; set; } 
    public bool attackPerformed { get; set; } 
    public bool interactPerformed { get; set; }

    private InputMap _inputMap;
    
    private void OnEnable()
    {
        _inputMap = new InputMap();
        _inputMap.Enable();
        
        _inputMap.Player.Move.Enable();
        _inputMap.Player.Look.Enable();
        _inputMap.Player.Interact.Enable();
        
        _inputMap.Player.Jump.Enable();
        _inputMap.Player.Jump.performed += OnJump;

        _inputMap.Player.Attack.Enable();
        _inputMap.Player.Attack.performed += OnAttack;
    }
    
    private void OnDisable()
    { 
        _inputMap.Disable();
        
        _inputMap.Player.Move.Disable();
        _inputMap.Player.Look.Disable();
        _inputMap.Player.Interact.Disable();

        _inputMap.Player.Jump.Disable();
        _inputMap.Player.Jump.performed -= OnJump;

        _inputMap.Player.Attack.Disable();
        _inputMap.Player.Attack.performed -= OnAttack;
    }
    
    private Coroutine _jumpCoroutine = null;
    private void OnJump(InputAction.CallbackContext context)
    {
        jumpPerformed = true;
        if (_jumpCoroutine != null)
        {
            StopCoroutine(_jumpCoroutine);
        }
        _jumpCoroutine = StartCoroutine(ResetButtonInput(newValue =>
        {
            jumpPerformed = newValue;
            _jumpCoroutine = null;
        }));
    }
    
    private Coroutine _attackCoroutine = null;
    private void OnAttack(InputAction.CallbackContext context)
    {
        attackPerformed = true;
        
        if (_attackCoroutine != null)
        {
            StopCoroutine(_attackCoroutine);
        }
        _attackCoroutine = StartCoroutine(ResetButtonInput(newValue =>
        {
            attackPerformed = newValue;
            _attackCoroutine = null;
        }));
    }
    
    private Coroutine _interactCoroutine = null;
    private void OnInteract(InputAction.CallbackContext context)
    {
        interactPerformed = true;
        
        if (_interactCoroutine != null)
        {
            StopCoroutine(_interactCoroutine);
        }
        _interactCoroutine = StartCoroutine(ResetButtonInput(newValue =>
        {
            interactPerformed = newValue;
            _interactCoroutine = null;
        }));
    }
    
    private IEnumerator ResetButtonInput(System.Action<bool> setReset)
    {
        yield return new WaitForSeconds(inputBufferTime);
        setReset(false);
        print("Jump deactivated"+Time.time);
    }
    
    private void Update()
    {
        //interactPerformed = _inputMap.Player.Interact.ReadValue<float>() >= 0.9f;
        interactPerformed = _inputMap.Player.Interact.IsPressed();
        moveInput = _inputMap.Player.Move.ReadValue<Vector2>();
        aimInput = _inputMap.Player.Look.ReadValue<Vector2>();
    }
}
