// 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 is used to relay the atmospheric model information betweeen the C++ data objects and the QML interface
#ifndef ATMOSPHERELISTVIEWMODEL_H
#define ATMOSPHERELISTVIEWMODEL_H

#include <QObject>
#include <QVector>
#include <QAbstractListModel>
#include "bytrancore.h"
#include "auxiliary.h"

class AtmosphereListView
{
public:

    explicit AtmosphereListView(const int &index, const QString &itemName);

    QString itemName() const;
    int index() const;

private:

    int m_index;  // Index of the item displayed
    QString m_itemName;  // Item name (such as the molecule chemical formula, temperature, pressure or density)

};

class AtmosphereListViewModel : public QAbstractListModel
{
    Q_OBJECT

    Q_PROPERTY(int selectedItem READ selectedItem WRITE setSelectedItem NOTIFY selectedItemChanged)
    Q_PROPERTY(quint8 modelNum READ modelNum WRITE setModelNum NOTIFY modelNumChanged)
    Q_PROPERTY(Spectrum spectrum READ spectrum NOTIFY spectrumChanged)
    Q_PROPERTY(double minX READ minX WRITE setMinX NOTIFY minXChanged)
    Q_PROPERTY(double maxX READ maxX WRITE setMaxX NOTIFY maxXChanged)
    Q_PROPERTY(double minY READ minY WRITE setMinY NOTIFY minYChanged)
    Q_PROPERTY(double maxY READ maxY WRITE setMaxY NOTIFY maxYChanged)
    Q_PROPERTY(QString xAxisTitle READ xAxisTitle WRITE setXAxisTitle NOTIFY xAxisTitleChanged)
    Q_PROPERTY(int currSpecIndex READ currSpecIndex WRITE setCurrSpecIndex NOTIFY currSpecIndexChanged)
    Q_PROPERTY(QString lineColor READ lineColor NOTIFY lineColorChanged)

public:

    // Method is used to assign new values
    Q_INVOKABLE void assignNewValues();

    // Method to update the plot according to the currently selected plot index
    Q_INVOKABLE void plotSpectrum();

    enum MolListViewRoles {
        itemNameRole = Qt::UserRole + 1,
    };

    AtmosphereListViewModel(QObject *parent = nullptr, BytranCore *inCore = nullptr);

    void addElement(const AtmosphereListView &atmListView);
    int rowCount(const QModelIndex & parent = QModelIndex()) const;
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;

    int selectedItem() const;
    void setSelectedItem(const int &selectedItem);

    quint8 modelNum() const;
    void setModelNum(const quint8 &modelNum);

    Spectrum spectrum() const;

    double minX() const;
    void setMinX(const double &minX);
    double maxX() const;
    void setMaxX(const double &maxX);
    double minY() const;
    void setMinY(const double &minY);
    double maxY() const;
    void setMaxY(const double &maxY);

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

    // Methods to get and set the XAxis title
    QString xAxisTitle();
    void setXAxisTitle(const QString &xAxisTitle);

    // Methods to get and set the current spectrum index
    int currSpecIndex() const;
    void setCurrSpecIndex(const int currSpecIndex);

    // Color to use for plotting the spectrum
    QString lineColor() const;

private slots:

    // Slot is used to update all values in the corresponding QML dialog
    void setUpdatedValues();

signals:

    // Signals to notify of select all changed and units changed states
    void selectedItemChanged();
    void modelNumChanged();
    void spectrumChanged();
    void minXChanged();
    void maxXChanged();
    void minYChanged();
    void maxYChanged();

    // Signal emitted when the X axis title has been changed
    void xAxisTitleChanged();

    // Signal is emitted when the current spectrum index is modified
    void currSpecIndexChanged();

    // Signal indicating that the color of the line to be used for plotting the spectrum has changed
    void lineColorChanged();

protected:

    QHash<int, QByteArray> roleNames() const;

private:

    // Method to update the spectrum displayed
    void updateSpectrum();

    BytranCore *core;
    QList<AtmosphereListView> m_atmListView;
    int m_units;  // Units of concentration to be used to specify the partial pressures
    int m_selectedItem;  // Variable indicating if all entities are selected in the list of molecules
    quint8 m_modelNum;  // Selected atmospheric model number
    Spectrum m_spectrum;
    double m_minX;
    double m_maxX;
    double m_minY;
    double m_maxY;

    // Horizontal axis title
    QString m_xAxisTitle;

    // Auxiliary object
    Auxiliary *aux;

    // Variable indicating the current spectrum index
    int m_currSpecIndex;

    // Color of the line to be used for plotting the spectrum
    QString m_lineColor;

};

#endif // ATMOSPHERELISTVIEWMODEL_H
