#include "TrayListViewItemModel.h"

#include <vector>
#include <utility>
#include <QDebug>

TrayListViewItemModel::TrayListViewItemModel(QObject* parent) : QAbstractItemModel(parent)
{
}

void TrayListViewItemModel::addDevice(const QString& iconPath, const QVariantList& list)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    devices.push_back(std::make_pair(QIcon::fromTheme(iconPath), list));
    endInsertRows();
}

void TrayListViewItemModel::removeDevice(const QString& portName, const QString& devName)
{
//    if (row >= 0 && row < rowCount())
//    {
//        beginRemoveRows(QModelIndex(), row, row);
//        devices.erase(devices.begin() + row);
//        endRemoveRows();
//    }

    std::erase_if(devices, [portName, devName] (const auto& devPair) {
        const auto& [currentIconName, variantList] = devPair;
        return variantList.at(0) == portName && variantList.at(1) == devName;
    });
}

QModelIndex TrayListViewItemModel::index(int row, int column, const QModelIndex& parent) const
{
    return createIndex(row, column);
}

QModelIndex TrayListViewItemModel::parent(const QModelIndex&) const
{
    return QModelIndex();
}

int TrayListViewItemModel::rowCount(const QModelIndex& parent) const
{
    if (!parent.isValid())
        return devices.size();
    return 0;
}

int TrayListViewItemModel::columnCount(const QModelIndex&) const
{
    return 1;
}

QVariant TrayListViewItemModel::data(const QModelIndex& index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (role == Qt::DisplayRole)
        return devices[index.row()].second.at(1);
    else if (role == Qt::DecorationRole && index.column() == 0)
    {
        return devices[index.row()].first;
    }

    return QVariant();
}
