using System;
using UnityEngine;
using UnityEngine.InputSystem;

public class InputManager : MonoBehaviour, GameInput.IPlayerActions
{
    public static InputManager Instance { get; private set; }

    private GameInput _input;

    public event Action<Vector2> OnMove_Event;
    public event Action OnJump_Event;
    public event Action OnPunch_Event;
    public event Action OnKick_Event;
    public event Action OnDash_Event;
    public event Action OnRoll_Event;
    public event Action OnBackDash_Event;

    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
            return;
        }
        Instance = this;

        _input = new GameInput();
        _input.Player.SetCallbacks(this);
    }

    private void OnEnable()  => _input?.Player.Enable();

    private void OnDisable() => _input?.Player.Disable();

    private void OnDestroy() => _input?.Dispose();
    
    public void OnMove(InputAction.CallbackContext ctx)
    {
        OnMove_Event?.Invoke(ctx.ReadValue<Vector2>());
    }

    public void OnJump(InputAction.CallbackContext ctx)
    {
        if (ctx.phase == InputActionPhase.Started)
            OnJump_Event?.Invoke();
    }

    public void OnPunch(InputAction.CallbackContext ctx)
    {
        if (ctx.phase == InputActionPhase.Started)
            OnPunch_Event?.Invoke();

    }

    public void OnKick(InputAction.CallbackContext ctx)
    {
        if (ctx.phase == InputActionPhase.Started)
            OnKick_Event?.Invoke();
    }

    public void OnDash(InputAction.CallbackContext ctx)
    {
        if (ctx.phase == InputActionPhase.Started)
            OnDash_Event?.Invoke();
    }

    public void OnRoll(InputAction.CallbackContext ctx)
    {
        if (ctx.phase == InputActionPhase.Started)
            OnRoll_Event?.Invoke();
    }

    public void OnBackDash(InputAction.CallbackContext ctx)
    {
        if (ctx.phase == InputActionPhase.Started)
            OnBackDash_Event?.Invoke();
    }

}
