// 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
#include "mollistviewreducedmodel.h"
#include <QMessageBox>

MolListViewReduced::MolListViewReduced(const QString &molName, const QString &color)
     : m_molName(molName), m_color(color)
{
}

QString MolListViewReduced::molName() const
{
    return m_molName;
}

QString MolListViewReduced::color() const
{ return m_color; }
void MolListViewReduced::setColor(const QString &color)
{
    if (color != m_color)
    {
        m_color = color;
    }
}

MolListViewReducedModel::MolListViewReducedModel(QObject *parent, BytranCore *inCore) : QAbstractListModel(parent)
{
    // Passing in the core module
    core = inCore;
    updateDisplayed();
}

// Method to update the list of displayed molecules
void MolListViewReducedModel::updateDisplayed()
{
    int i;  // Loop variable
    QString molName;
    QString color;


    removeAllElements();
    for (i = 0; i < core->getParams()->getMol()->count(); i++)
    {
        if (core->getParams()->getMolAt(i)->getSelected() == true)
        {
            // Displaying the molecular infromation for each molecular record (not all parameters are displayed) as saved into the internal data structure
            molName = core->getParams()->getMolAt(i)->getChemForm();
            color = core->getParams()->getMolAt(i)->getColor();
            this->addElement(MolListViewReduced(molName, color));
        }
    }
}

void MolListViewReducedModel::addElement(const MolListViewReduced &molListView)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_molListView << molListView;
    endInsertRows();
}

void MolListViewReducedModel::removeAllElements()
{
    beginRemoveRows(QModelIndex(), 0, rowCount());
    m_molListView.clear();
    endRemoveRows();
}

int MolListViewReducedModel::rowCount(const QModelIndex & parent) const {
    Q_UNUSED(parent);
    return m_molListView.count();
}

QVariant MolListViewReducedModel::data(const QModelIndex & index, int role) const {
    if ((index.row() < 0) || (index.row() >= m_molListView.count()))
        return QVariant();

    const MolListViewReduced &molListView = m_molListView[index.row()];
    if (role == molNameRole)
        return molListView.molName();    
    else if (role == colorRole)
        return molListView.color();
    return QVariant();
}

// Method to set the data when the values in the model are modified
bool MolListViewReducedModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
    switch (role)
    {
        case colorRole:
            m_molListView[index.row()].setColor(value.toString());
            break;
    }
    emit dataChanged(index, index);
    return true;
}

//![0]
QHash<int, QByteArray> MolListViewReducedModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[molNameRole] = "molName";
    roles[colorRole] = "color";
    return roles;
}
//![0]

// Need to update the model and synch data etc.
