namespace UniversalNetFrame451.IO;

public class MessageQueue : BaseFilter, IDisposable
{

    private EventQueue<Data> _receiveQueue;
    private EventQueue<Data> _sendQueue;


    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;
    }

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

    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);
    }
}
public class MessageReceiveQueue : BaseFilter, IDisposable
{
    private EventQueue<Data> _receiveQueue;


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

        this._receiveQueue = new(ReceivedQueueHandler);

        return true;
    }

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

    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);
    }

}

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

    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);
    }
}