using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using Variables;
using NaughtyAttributes;

public class CharacterInputController : MonoBehaviour
{

    [SerializeField, Header("References")]
    private CharacterController2D m_characterController;

    [SerializeField]
    private Reference<bool> m_playerInput;

    [SerializeField]
    private Reference<bool> m_noInputAllowed;

    [SerializeField]
    private Reference<bool> m_isPlayerDead;

    [SerializeField]
    private Reference<bool> m_isVictory;

    [SerializeField]
    private Animator m_animator;

    [SerializeField, BoxGroup("Settings")]
    private float m_speed = 2.0f;

    [ShowNonSerializedField]
    private Vector2 m_movement;

    [ShowNonSerializedField]
    private bool m_jump;



    private void OnEnable()
    {
        m_isPlayerDead.OnValueChanged += OnDeath;
        m_isVictory.OnValueChanged += OnVictory;
    }

    public void OnMove(InputAction.CallbackContext context)
    {
        m_movement = context.ReadValue<Vector2>();
    }

    public void OnJump(InputAction.CallbackContext context)
    {
        m_jump = context.ReadValueAsButton();
    }

    private void Update()
    {

        if (!m_noInputAllowed)
        {
            m_characterController.Move(m_movement.x * m_speed, false, m_jump);
            m_playerInput.Value = m_movement.magnitude != 0 || m_jump;
            
        }
        else
        {
            m_characterController.Move(0, false, false);
            m_playerInput.Value = false;
        }
        m_animator.SetBool("isMoving", m_playerInput);
    }


    public void OnLandHappened()
    {
        m_animator.SetTrigger("Land");
    }

    public void OnJumpHappened()
    {
        m_animator.SetTrigger("Land");
    }
    public void OnDeath(bool value)
    {
        if (value)
            m_animator.SetTrigger("Death");
        else
            m_animator.SetTrigger("Respawn");
    }

    public void OnVictory(bool value)
    {
        if (value)
            m_animator.SetTrigger("Victory");
    }






}
