#pragma once

#include "text_view_dialog.hh"
#include "../TradeCore/message.hh"
#include <QObject>
#include <QLocalSocket>
#include <memory>

class ServerWidget;

/**
 * @brief Connection class encapsulates socket obtained from server
 *
 * Main purposes are parsing and serializing messages, registering
 * connections and handling invalid incoming messages.
 */
class Connection : public QObject
{
    Q_OBJECT

public:
    Connection(QLocalSocket* connection, ServerWidget* server);
    ~Connection();
    bool isRegistered() const;
    int clientId() const;
    void writeMessage(const Message& message);
    bool hasSocket();
    void disconnectSocket();
    QDateTime created() const;
    int receivedMessages() const;
    int sentMessages() const;
    void showData();

signals:
    void connectionRegistered(int clientId, const Registration&);
    void messageReceived(const Message&);
    void socketDisconnected();

private slots:
    void readMessage();
    void onSocketDisconnected();

private:
    Connection();
    Connection(const Connection& connection);
    Connection& operator=(const Connection&);

private:
    QLocalSocket* connection_;
    ServerWidget* server_;
    int clientId_;
    QDateTime created_;
    int receivedCount_;
    int sentCount_;
    std::unique_ptr<TextViewDialog> dataView_;
};
