// 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 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.



// Object to communicate with the QML program interface for remote control components
#ifndef REMOTECONTROLQML_H
#define REMOTECONTROLQML_H

#include <QObject>
#include <QLocale>
#include "remotecontrol.h"

class RemoteControlQml : public QObject
{
    Q_OBJECT

    Q_PROPERTY(int communicationMode READ communicationMode WRITE setCommunicationMode NOTIFY communicationModeChanged)
    Q_PROPERTY(bool broadcastManualInput READ broadcastManualInput WRITE setBroadcastManualInput NOTIFY broadcastManualInputChanged)
    Q_PROPERTY(QString portNumber READ portNumber WRITE setPortNumber NOTIFY portNumberChanged)
    Q_PROPERTY(QString bytranDeviceIpAddress READ bytranDeviceIpAddress NOTIFY bytranDeviceIpAddressChanged)
    Q_PROPERTY(QString remoteDeviceIpAddress READ remoteDeviceIpAddress NOTIFY remoteDeviceIpAddressChanged)
    Q_PROPERTY(quint8 selectedBluetoothDeviceIndex READ selectedBluetoothDeviceIndex WRITE setSelectedBluetoothDeviceIndex NOTIFY selectedBluetoothDeviceIndexChanged)
    Q_PROPERTY(QString connectButtonText READ connectButtonText NOTIFY connectButtonTextChanged)
    Q_PROPERTY(QString connectedString READ connectedString NOTIFY connectedStringChanged)

    public:

    Q_INVOKABLE void openUrl(QString webAddress);
    Q_INVOKABLE void discoverDevices();
    Q_INVOKABLE void connectBluetooth();
    Q_INVOKABLE void broadcastCommand(QString command, int index);
    Q_INVOKABLE void sendMessage(QString message);

    explicit RemoteControlQml(QObject *parent = nullptr, RemoteControl *inRemoteControl = 0);

    int communicationMode() const;
    void setCommunicationMode(const int communicationMode);

    bool broadcastManualInput() const;
    void setBroadcastManualInput(const bool broadcastManualInput);

    QString portNumber() const;
    void setPortNumber(const QString portNumber);

    quint8 selectedBluetoothDeviceIndex() const;
    void setSelectedBluetoothDeviceIndex(const quint8 &selectedBluetoothDeviceIndex);

    QString bytranDeviceIpAddress() const;
    QString remoteDeviceIpAddress() const;

    // Text of the bluetooth low energy connect button
    QString connectButtonText() const;

    // Text to indicate the UUID value or the connected state (if connected)
    QString connectedString() const;

signals:

    void communicationModeChanged();
    void broadcastManualInputChanged();
    void portNumberChanged();
    void bytranDeviceIpAddressChanged();
    void remoteDeviceIpAddressChanged();
    void selectedBluetoothDeviceIndexChanged();
    void connectButtonTextChanged();
    void connectedStringChanged();

public slots:

    // Signal is emitted when the connected state is changed (regardless of the connection type, i.e. WebSockets, Bluetooth, Serial Port)
    void setConnectedState();

    // Slots are called when a remote client is connected or disconnected from the bluetooth server
    void setConnectedToBluetoothServer(const QString &name);
    void setDisconnectedFromBluetoothServer(const QString &name);

private:

    RemoteControl *remoteControl;   // The remote control object
    int m_communicationMode;        // Variable indicates the communication mode selected (0 - Disabled, 1 - Bluetooth, 2 - serial port).
    bool m_broadcastManualInput;    // If this is set to true, the manual input in the device will be broadcast over bluetooth or RS-232
    QString m_portNumber;   // Port number (for the WebSockets connection over the Internet)
    QString m_bytranDeviceIpAddress;   // String with the IP address(es) of the bytran device
    QString m_remoteDeviceIpAddress;   // String with the IP address of the remote device (when connected)
    quint8 m_selectedBluetoothDeviceIndex;  // Selected service item index
    QString m_connectButtonText;    // Connect/Disconnect button text
    QString m_connectedString;      // String with the bluetooth service UUID or the connected state value
    QLocale loc;  // Object used for sting to number conversion

};

#endif // REMOTECONTROLQML_H
