#include "devices_list.h"
#include "SH_network_types.h"
#include "SH_platform.h"
#include "ui_devices_list.h"
#include <QMetaObject>
#include <piqt.h>
#include <piiodevice.h>


enum ColumnnClient {
	ccClassname,
	ccPath,
	ccMode,
	ccSendBytes,
	ccRecBytes,
	ccSendSpeed,
	ccRecSpeed,
};


DevicesList::DevicesList(QWidget * parent): QTreeWidget(parent){
	Q_INIT_RESOURCE(ui_tools);
	ui = new Ui::DevicesList();
	ui->setupUi(this);
	auto dcnl = PIIODevice::availableClasses();
	for (const auto & c: dcnl)
		dev_icons[PI2QString(c)] = QIcon(QString(":/icons/dev-%1.png").arg(PI2QString(c)));
	dmm[PIIODevice::ReadOnly] = "ReadOnly";
	dmm[PIIODevice::WriteOnly] = "WriteOnly";
	dmm[PIIODevice::ReadWrite] = "ReadWrite";
}


DevicesList::~DevicesList() {
}


void DevicesList::set(const PIVector<ServerDeviceInfo> & devices) {
	for (int i = topLevelItemCount() - 1; i >= devices.size_s(); --i)
		delete takeTopLevelItem(i);
	for (int i = topLevelItemCount(); i < devices.size_s(); ++i)
		addTopLevelItem(new QTreeWidgetItem());
	for (int i = 0; i < topLevelItemCount(); ++i) {
		QTreeWidgetItem * ti = topLevelItem(i);
		const ServerDeviceInfo & d(devices[i]);
		ti->setText(ccClassname, d.classname);
		ti->setIcon(ccClassname, dev_icons.value(d.classname, QIcon()));
		ti->setText(ccPath, d.path);
		ti->setText(ccMode, dmm.value(d.mode, "?"));
		ti->setText(ccSendBytes, PI2QString(PIString::readableSize(d.sended_bytes)));
		ti->setText(ccRecBytes , PI2QString(PIString::readableSize(d.received_bytes)));
		ti->setText(ccSendSpeed, d.send_speed);
		ti->setText(ccRecSpeed , d.receive_speed);
	}
	update();
}
