// 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 list model is used to display a list of available ports on a device
#include "serialportslistmodel.h"

SerialPortsListView::SerialPortsListView(const QString &portName)
     : m_portName(portName)
{
}

QString SerialPortsListView::portName() const { return m_portName; }
void SerialPortsListView::setPortName(const QString &portName)
{
    if (portName != m_portName)
    {
        m_portName = portName;
    }
}

SerialPortsListModel::SerialPortsListModel(QObject *parent, BytranCore *inCore) : QAbstractListModel(parent)
{
    // Transfering a pointer to the core component to an internal variable
    this->core = inCore;
    updateDisplayed();
}

// Method to update the list of displayed molecules
void SerialPortsListModel::updateDisplayed()
{
    int i;  // Loop variable
    QString serialPortName;  // Serial port name

    removeAllElements();
    for (i = 0; i < core->getRemoteControl()->getSerialPort()->getPortNames().count(); i++)
    {
        // Displaying the molecular infromation for each molecular record (not all parameters are displayed) as saved into the internal data structure
        serialPortName = core->getRemoteControl()->getSerialPort()->getPortNames().at(i);
        this->addElement(SerialPortsListView(serialPortName));
    }
    if (core->getRemoteControl()->getSerialPort()->getPortNames().count() <= 0) { this->addElement(SerialPortsListView("Unavailable")); }
}

void SerialPortsListModel::addElement(const SerialPortsListView &serialPortsListView)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_serialPortsListView << serialPortsListView;
    endInsertRows();
}

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

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

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

    const SerialPortsListView &serialPortsListView = m_serialPortsListView[index.row()];
    if (role == serialPortRole)
        return serialPortsListView.portName();
    return QVariant();
}

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

//![0]
QHash<int, QByteArray> SerialPortsListModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[serialPortRole] = "portName";
    return roles;
}
//![0]
