// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | bytran@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// The following examples were used to write this class
// https://doc.qt.io/qt-5.10/qtwebsockets-index.html
// https://www.varvet.com/blog/using-websockets-in-native-ios-and-android-apps/
// https://doc.qt.io/qt-5.6/qtwebsockets-simplechat-example.html - most useful
// https://doc.qt.io/qt-5.10/qtwebsockets-echoclient-example.html
// https://doc.qt.io/qt-5.10/qtwebsockets-echoserver-example.html
// https://stackoverflow.com/questions/13835989/get-local-ip-address-in-qt
// https://stackoverflow.com/questions/5572263/get-current-qnetworkinterface-active-and-connected-to-the-internet
// http://www.websocket.org/echo.html
// https://stackoverflow.com/questions/25327039/can-websockets-work-on-mobile-phones
//
// The MIT License (MIT)
// Copyright (c) 2016 Dzianis Pliutau
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



// Web sockets clss is used for remote control and data transfer between devices using the WebSockets protocol

// This class was developed mostly using the Qt Simple Chat example
// https://doc.qt.io/qt-5.6/qtwebsockets-simplechat-example.html dstributed under the BSD license
#ifndef WEBSOCKETS_H
#define WEBSOCKETS_H

#include <QObject>
#include <QtWebSockets/QWebSocketServer>
#include <QtWebSockets/QWebSocket>
#include <QString>
#include <QUrl>
#include <QNetworkInterface>

class WebSockets : public QObject
{
    Q_OBJECT
public:
    explicit WebSockets(quint16 port, QObject *parent = nullptr);
    ~WebSockets();


    // Method to send a message to a remote device
    void sendMessage(const QString &message);
    void sendMessage(const QByteArray &message);

    // Methods to enable or disable web sockets protocol
    void enableWebSockets(bool state);
    bool getEnableWebSockets();

    // Methods to get and set the port numbers
    quint32 getPortNumber();
    void setPortNumber(quint16 newPortNumber);

    // Method is used to get the IP address assigned to the device
    QString getSelfDeviceIpAddresses();
    QString getConnectedDeviceIpAddress();

signals:

    // Emitted when the connection is closed
    void closed();

    // Signal is emitted when a command is received over the websockets protocol
    void commandReceived(const QString &message);

    // Signal is emitted when the connected state is changed
    void connectedStateChanged();

private slots:

    void onNewConnection();
    void processTextMessage(const QString &message);
    void processBinaryMessage(QByteArray message);
    void socketDisconnected();

public slots:

private:

    QWebSocketServer *webSocketServer;   // Web socket server to run within bytran
    QList<QWebSocket *> clients;    // Web socket clients to be connected to bytran
    quint16 portNumber;   // Port number to use for connection
};

#endif // WEBSOCKETS_H
