#ifndef APISMTCPCLIENT_H
#define APISMTCPCLIENT_H

#include <QObject>

#include <QTcpSocket>
#include <QAbstractSocket>
#include <QByteArray>
#include <QQueue>

class QTimer;

class ApismTcpClient : public QObject
{
    Q_OBJECT
public:
    explicit ApismTcpClient(const QString & hostname, const QString & port, QObject *parent = nullptr);
    bool isConnected();

    void connectToHost();
    void connectToHost(const QString & hostname, const QString & port);

    // socket is implicitely closed by APISM
    void closeConnection();

    void sendData(const QByteArray & message);

public slots:
    // socket interface
    void onSocketConnected();
    void onSocketDisconnected();
    void onSocketBytesWritten(qint64 bytes);
    void onSocketReadyRead();

signals:
    void receivedData(QByteArray response);

    void responseTimeout();

private:
    QTcpSocket *socket;

    QQueue<QByteArray> sendQueue;


    QString hostname;
    QString port;

    QTimer *responseTimeoutTimer;
    quint8 responseTimerTimeoutCounter;


    void private_sendData();


public slots:
    void onResponseTimeoutTimerTimeout();


};

#endif // APISMTCPCLIENT_H
