namespace Universal.IO;

public class MessageQueue : BaseFilter, IDisposable
{
    private EventQueue<Data>? _receiveQueue;
    private EventQueue<Data>? _sendQueue;
    private bool disposedValue;

    public bool Initialize()
    {
        if (this._receiveQueue is not null)
            return false;

        if (this._sendQueue is not null)
            return false;

        this._receiveQueue = new(ReceivedQueueHandler);
        this._sendQueue = new(SendQueueHandler);

        return true;
    }

    public override bool Send(Data data)
    {
        if (_sendQueue is null)
            return false;

        this._sendQueue?.Enqueue(data);
        return true;
    }

    public override bool Receive(Data data)
    {
        if (this._receiveQueue is null)
            return false;

        this._receiveQueue?.Enqueue(data);
        return true;
    }

    private void SendQueueHandler(Data data)
    {
        base.Send(data);
    }
    private void ReceivedQueueHandler(Data data)
    {
        base.Receive(data);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                this._receiveQueue?.Dispose();
                this._sendQueue?.Dispose();
                this._receiveQueue = null;
                this._sendQueue = null;
            }

            disposedValue = true;
        }
    }

    public void Dispose()
    {
        Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }
}
public class MessageReceiveQueue : BaseFilter, IDisposable
{
    private EventQueue<Data>? _receiveQueue;
    private bool disposedValue;

    public bool Initialize()
    {
        if (this._receiveQueue is not null)
            return false;

        this._receiveQueue = new(ReceivedQueueHandler);

        return true;
    }

    public override bool Receive(Data data)
    {
        if (this._receiveQueue is null)
            return false;

        this._receiveQueue?.Enqueue(data);
        return true;
    }

    private void ReceivedQueueHandler(Data data)
    {
        base.Receive(data);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                this._receiveQueue?.Dispose();
                this._receiveQueue = null;
            }

            disposedValue = true;
        }
    }

    public void Dispose()
    {
        Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }
}

public class MessageSendQueue : BaseFilter, IDisposable
{
    private EventQueue<Data>? _sendQueue;
    private bool disposedValue;

    public bool Initialize()
    {
        if (this._sendQueue is not null)
            return false;

        this._sendQueue = new(SendQueueHandler);

        return true;
    }

    //void IDisposable.Dispose()
    //{
    //    this._sendQueue?.Dispose();
    //    this._sendQueue = null;
    //}


    public override bool Send(Data data)
    {
        if (_sendQueue is null)
            return false;

        this._sendQueue?.Enqueue(data);
        return true;
    }

    private void SendQueueHandler(Data data)
    {
        base.Send(data);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                this._sendQueue?.Dispose();
                this._sendQueue = null;
            }

            disposedValue = true;
        }
    }

    public void Dispose()
    {
        // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
        Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }
}