// 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 is a "QAbstractItemModel" class used to display and change data in the QML molecules list view
#ifndef MOLLISTVIEWMODEL_H
#define MOLLISTVIEWMODEL_H

#include <QObject>
#include <QLocale>
#include <QAbstractListModel>
#include "bytrancore.h"

class MolListView
{
public:

    MolListView(const QString &index, const bool &checked, const QString &molName, const QString &pressValue, const QString &color);

    QString index() const;
    bool checked() const;
    QString molName() const;
    QString pressValue() const;
    QString color() const;

    void setChecked(const bool &checked);
    void setPressValue(const QString &pressValue);
    void setColor(const QString &color);

private:

    QString m_index;
    bool m_checked;
    QString m_molName;
    QString m_pressValue;
    QString m_color;

};

class MolListViewModel : public QAbstractListModel
{
    Q_OBJECT

    Q_PROPERTY(int units READ units WRITE setUnits NOTIFY unitsChanged)
    Q_PROPERTY(QString selectedColor READ selectedColor WRITE setSelectedColor NOTIFY selectedColorChanged)
    Q_PROPERTY(int activeIndex READ activeIndex WRITE setActiveIndex NOTIFY activeIndexChanged)
    Q_PROPERTY(bool allSelected READ allSelected WRITE setAllSelected NOTIFY allSelectedChanged)
    Q_PROPERTY(bool calcNotSelectedMols READ calcNotSelectedMols WRITE setCalcNotSelectedMols NOTIFY calcNotSelectedMolsChanged)

public:

    // Method to update the partial pressures displayed in the molecules window
    Q_INVOKABLE void updatePartPress();
    Q_INVOKABLE void updateActiveColor();
    Q_INVOKABLE void selectAll(bool selected);

    enum MolListViewRoles {
        indexRole = Qt::UserRole + 1,
        checkedRole,
        molNameRole,
        pressValueRole,
        colorRole
    };

    MolListViewModel(QObject *parent = 0, BytranCore *inCore = 0);

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

    int units() const;
    void setUnits(const int &units);

    QString selectedColor() const;
    void setSelectedColor(const QString &selectedColor);

    int activeIndex() const;
    void setActiveIndex(const int activeIndex);

    bool allSelected() const;
    void setAllSelected(const bool allSelected);

    bool calcNotSelectedMols() const;
    void setCalcNotSelectedMols(const bool calcNotSelectedMols);

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

signals:

    // Signals to notify of select all changed and units changed states
    void unitsChanged();
    void selectedColorChanged();
    void activeIndexChanged();
    void allSelectedChanged();
    void calcNotSelectedMolsChanged();

    // Signal is emitted in order to update the ploted spectra (after the checked states are modified)
    void updateSpectra(bool updateScale, bool updateTotal);

private slots:

    void updateH2OValue();
    void updateAllPartPressValues();
    void updateAllCheckedStates();

protected:

    QHash<int, QByteArray> roleNames() const;

private:

    QList<MolListView> m_molListView;

    QString m_selectedColor;  // String with the HEX representation of the selected color
    int m_activeIndex;   // Index of the currently active molecule in the list
    bool m_allSelected;  // Variable holds the state of if all molecules are selected or not
    int m_units;   // Units of concentration to be used to specify the partial pressures
    bool m_calcNotSelectedMols;  // Variable indicating if not selected molecules are to be included in calculations (yes by default)

    // Core object with the calculation logic etc.
    BytranCore *core;

    // Locale specific string to number and inverse conversions
    QLocale loc;

};

#endif // MOLLISTVIEWMODEL_H
