using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerInputController : MonoBehaviour
{
    public InputActionAsset InputActions;
    public bool InputEnabled { get; private set; } = true;

    private InputAction m_moveAction;
    private InputAction m_lookAction;
    private InputAction m_jumpAction;
    private InputAction m_throwAction;
    private InputAction m_shiftAction;
    private InputAction m_interactAction;
    private InputAction m_headInteractAction;
    

    public Vector2 MoveAmount { get; private set; }
    public Vector2 LookAmount { get; private set; }

    public bool JumpPressed { get; private set; }
    public bool ShiftPressed { get; private set; }
    public bool ThrowPressed { get; private set; }
    public bool InteractPressed { get; private set; }
    public bool HeadInteractionPressed { get; private set; }
   

    private void Awake()
    {
        var map = InputActions.FindActionMap("Player");

        m_moveAction = map.FindAction("Move");
        m_lookAction = map.FindAction("Look");
        m_jumpAction = map.FindAction("Jump");
        m_shiftAction = map.FindAction("Shift");
        m_throwAction = map.FindAction("Throw");
        m_interactAction = map.FindAction("Interact");
        m_headInteractAction = map.FindAction("HeadInteract");
        
    }

    private void OnEnable()
    {
        InputActions.FindActionMap("Player").Enable();
    }

    private void OnDisable()
    {
        InputActions.FindActionMap("Player").Disable();
    }

    private void Update()
    {
        if (!InputEnabled)
        {
            MoveAmount = Vector2.zero;
            LookAmount = Vector2.zero;
            ShiftPressed = false;
            JumpPressed = false;
            ThrowPressed = false;
            InteractPressed = false;
            HeadInteractionPressed = false;
            return;
        }

        MoveAmount = m_moveAction.ReadValue<Vector2>();
        LookAmount = m_lookAction.ReadValue<Vector2>();

        ShiftPressed = m_shiftAction.IsPressed();
        JumpPressed = m_jumpAction.WasPressedThisFrame();
        ThrowPressed = m_throwAction.WasPressedThisFrame();
        InteractPressed = m_interactAction.WasPressedThisFrame();
        HeadInteractionPressed = m_headInteractAction.WasPressedThisFrame();
    }

    public void SetInputEnabled(bool enabled)
    {
        InputEnabled = enabled;
    }
}