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



// This class is used to display the list of available bluetooth devices
#ifndef BLUETOOTHLISTVIEWMODEL_H
#define BLUETOOTHLISTVIEWMODEL_H

#include <QObject>
#include <QAbstractListModel>
#include <QBluetoothAddress>
#include "bluetoothdiscoverdevices.h"

class BluetoothListView
{
public:

    BluetoothListView(const QString &serviceName, const QString &color);

    QString deviceName() const;
    void setDeviceName(const QString &deviceName);

    QString color() const;
    void setColor(const QString &color);

private:

    QString m_deviceName;
    QString m_color;

};

class BluetoothListViewModel : public QAbstractListModel
{
    Q_OBJECT

public:

    // Method to update the partial pressures displayed in the molecules window
    Q_INVOKABLE void updateDisplayed();
    Q_INVOKABLE void populateList();

    enum MolListViewReducedRoles {
        deviceNameRole = Qt::UserRole + 1,
        colorRole
    };

    BluetoothListViewModel(QObject *parent = nullptr, BluetoothDiscoverDevices *inBluetoothDiscoverDevices = nullptr);

    void addElement(const BluetoothListView &bluetoothListView);
    void removeAllElements();
    // void clear();
    int rowCount(const QModelIndex & parent = QModelIndex()) const;
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;

    // Method to set the data when the values in the model are modified
    bool setData(const QModelIndex & index, const QVariant & value, int role);

private slots:

    void setDeviceDiscoveryStarted();   // Slot is called when the device discovery is started
    void setDeviceDiscoveryFinished();  // Slot is called when the device discovery has finished

    // Slot to update the list of connected devices
    void setDeviceConnected();
    void setDeviceDisconnected();

signals:


protected:

    QHash<int, QByteArray> roleNames() const;

private:

    BluetoothDiscoverDevices *bluetoothDiscoverDevices;
    QList<BluetoothListView> m_devicesListView;
    QString m_selectedColor;  // String with the HEX representation of the selected color
    int m_activeIndex;        // Index of the currently active device in the list
    QString informationStr;   // String to display information about the bluetooth discovery progress

};

#endif // BLUETOOTHLISTVIEWMODEL_H
