using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public struct BufferedInput
{
    public BufferedInput( float bufferTime )
    {
        _bufferTime = bufferTime;
        _value = false;
        _timeSincePress = Mathf.Infinity;
        _timeSinceRelease = Mathf.Infinity;
    }
    
    private float _bufferTime;
    private bool _value;
    private float _timeSinceRelease;
    private float _timeSincePress;

    public void Set( bool newValue )
    {
        if ( newValue && !_value )
        {
            _timeSincePress = 0f;
        }
        else if ( !newValue && _value )
        {
            _timeSinceRelease = 0f;
        }

        _value = newValue;
    }

    public void Update( float deltaTime )
    {
        _timeSincePress += deltaTime;
        _timeSinceRelease += deltaTime;
    }

    public bool GetValue()
    {
        return _value || _timeSinceRelease <= _bufferTime;
    }

    public bool GetRawValue()
    {
        return _value;
    }

    public bool GetDown( bool consume = true )
    {
        bool value = _timeSincePress <= _bufferTime;
        
        if ( consume && value )
            _timeSincePress = Mathf.Infinity;
        
        return value;
    }

    public bool GetRelease( bool consume = true )
    {
        bool value = _timeSinceRelease <= _bufferTime;
        
        if ( consume && value )
            _timeSinceRelease = Mathf.Infinity;

        return value;
    }
}
