using System.Collections.Generic;
using UnityEngine;

public class InputBuffer : MonoBehaviour
{
    float bufferDuration = 0.4f; // Duration in seconds to keep inputs in the buffer
    private Queue<InputData> inputBuffer = new Queue<InputData>();

    public void AddInputToBuffer(string inputName)
    {
        if (!(inputBuffer.Count > 0))
        {
            InputData newInput = new InputData(inputName, Time.time);
            inputBuffer.Enqueue(newInput);
            Debug.Log($"Input {inputName} added to buffer at time {Time.time}");
        }
    }

    public void CleanUpBuffer()
    {
        while (inputBuffer.Count > 0)//&& inputBuffer.Peek().time < Time.time - bufferDuration
        {
            InputData expiredInput = inputBuffer.Dequeue();
            Debug.Log($"Input {expiredInput.inputName} removed from buffer (expired)");
        }
    }

    public string GetLatestInput()
    {
        if (inputBuffer.Count > 0)
        {
            return inputBuffer.Peek().inputName;
        }
        return null; // Return null if no input is in the buffer
    }

    private class InputData
    {
        public string inputName;
        public float time;

        public InputData(string inputName, float time)
        {
            this.inputName = inputName;
            this.time = time;
        }
    }
}
