using System.Data.Odbc;

namespace TLVProtocal.SubLayers;

internal class SingleConnectLayer : BaseFilter
{
    private TcpConnection _currentConnection;
    public override void Connected(Connection connection)
    {
        if (connection is not TcpConnection tcpConnection)
            return;

        this._currentConnection ??= tcpConnection;
        base.Connected(connection);
    }

    public override void Disconnected(Connection connection)
    {
        this._currentConnection = null;
        base.Disconnected(connection);
    }

    public override bool Send(Data data)
    {
        if (this._currentConnection == null)
            return false;

        if (data.Connection is not TcpConnection tcpConnection)
            return false;

        if (this._currentConnection.RemoteEndPoint != tcpConnection.RemoteEndPoint)
            return false;

        return base.Send(data);
    }

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

        if (data.Connection is not TcpConnection tcpConnection)
            return false;

        if (this._currentConnection.RemoteEndPoint != tcpConnection.RemoteEndPoint)
            return false;

        return base.Receive(data);
    }
}
