using InControl;
using UnityEngine;

public class CharacterInput : MonoBehaviour
{
    [SerializeField] private float dashCooldown = 5;
    [SerializeField] private float mashTimer = 0.1f;
    [SerializeField, ReadOnly] private CatController player;
    [SerializeField, ReadOnly] private bool allowInput;
    [SerializeField, ReadOnly] private Vector2 movementDirection;
	[SerializeField, ReadOnly] private bool isDashing;

    [SerializeField, ReadOnly] float dashTimer;

    private InputWrapper input;

    public bool mashMode = false;

    public CatController Player { get { return player; } set { player = value; } }
    public bool AllowInput { get { return allowInput; } }
	public bool IsDashing { get { return isDashing; } }
    public Vector2 MovementDirection { get { return movementDirection; } }

    private bool buttonIsPressed;

    public int mashID;

    public void Tick()
    {
        // if (player.Health.IsRespawning)
        // {
        //     ResetInput();
        //     return;
        // }

        if (input.Triangle() && !mashMode)
        {
            GameStart.Instance.Restart();
        }

        if(input.Cross() && mashMode)
        {
            SoundManager.Instance.Play(SoundManager.SoundType.Bump);
            ButtonMashUI.Instance.ButtonDown(mashID);
        }
        if (input.CrossUp() && mashMode)
        {
            ButtonMashUI.Instance.ButtonUp(mashID);
        }

        if (allowInput)
        {
            movementDirection = input.Direction();

            if(dashTimer > 0) dashTimer -= Time.deltaTime;
            if(input.Cross() && dashTimer <= 0)
            {
                if (mashMode)
                {
                    //Debug.Log("Mash");
                    ButtonMashUI.Instance.ButtonDown(mashID);
                }
                else
                {
                    player.Physics.StartDash(mashTimer);
                    dashTimer = dashCooldown;
                }
            }

            // if (input.CrossUp() && dashTimer <= 0 && mashMode)
            // {
            //     ButtonMashUI.Instance.ButtonUp(mashID);
            // }
        }
    }

    public void BindController(InputDevice device)
    {
        if (device != null)
        {
            input = new ControllerWrapper(device);
        }
    }

    public void SetPermissions(bool allowMovement)
    {
        this.allowInput = allowMovement;
    }

    public void ResetInput()
    {
        movementDirection = Vector2.zero;
    }
}