//
// Created by Krad on 2021/11/23.
//

#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include "network/netcfgtablemodel.h"
#include <QDebug>

NetCfgTableModel::NetCfgTableModel(QObject* parent) : QAbstractTableModel(parent) {

}
/*!
	Sets the \a role data for the item at \a index to \a value.
	Returns \c{true} if successful; otherwise returns \c{false}.
	The dataChanged() signal should be emitted if the data was successfully
	set.
	The base class implementation returns \c{false}. This function and data() must
	be reimplemented for editable models.
	\sa Qt::ItemDataRole, data(), itemData()
*/

//bool NetCfgTableModel::setData(const QModelIndex& index, const QVariant& value, int role){}

QList<QStringList>  NetCfgTableModel::getData()
{
	return mydata;
}
bool NetCfgTableModel::loadData(const QList<QStringList>& list)
{
	this->mydata = list;

	QModelIndex tl = this->index(0, 0);
	QModelIndex br = this->index(list.size() - 1, list.begin()->size() - 1);
	emit dataChanged(tl, br);
	return true;
}
bool NetCfgTableModel::addRow(const QStringList& value)
{
	int index = rowCount(QModelIndex());
	QAbstractTableModel::beginInsertRows(QModelIndex(), index, index);
	mydata.insert(index, value);
	QAbstractTableModel::endInsertRows();
	return true;

}

bool NetCfgTableModel::insertRow(int index, const QStringList& value)
{
	QAbstractTableModel::beginInsertRows(QModelIndex(), index, index);
	mydata.insert(index, value);
	QAbstractTableModel::endInsertRows();
	return true;
}
bool NetCfgTableModel::removeRow(int index)
{
	QAbstractTableModel::beginRemoveRows(QModelIndex(), index, index);
	mydata.removeAt(index);
	QAbstractTableModel::endRemoveRows();
	return true;
}

QStringList NetCfgTableModel::rowData(QModelIndex index)
{
	if (!index.isValid()) return QStringList();
	if (index.row() < mydata.size())
	{
		return mydata[index.row()];
	}
	return QStringList();
}

QVariant NetCfgTableModel::data(const QModelIndex& index, int role) const {
	if (!index.isValid()) return QVariant();
	if (role == Qt::TextAlignmentRole) {
		return Qt::AlignCenter;
	}
	if (role == Qt::DisplayRole || role == Qt::EditRole)
	{
		return mydata[index.row()][index.column()];
	}
	return QVariant();
}

int NetCfgTableModel::rowCount(const QModelIndex& parent) const {
	if (!mydata.isEmpty()) return mydata.count();
	return 0;
}

int NetCfgTableModel::columnCount(const QModelIndex& parent) const {
	if (!mydata.isEmpty()) return mydata[0].count();
	return 0;
}

QVariant NetCfgTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
	if (role != Qt::DisplayRole)
		return QVariant();
	if (orientation == Qt::Horizontal)
		return headerStrings.at(section);
	return QVariant();
}
