#include "cameralistwidget.h"
#include <QDebug>
#include <QHeaderView>
#include "../cameras.h"

CameraListWidget::CameraListWidget(QWidget* parent): QTableWidget(parent)
{
	setColumnCount(1);
	setSelectionBehavior(QAbstractItemView::SelectRows);
	setSelectionMode(QAbstractItemView::MultiSelection);
	setHorizontalHeaderItem(0, new QTableWidgetItem("Camera"));
	setRowCount(desc_.size());
	horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
	setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void CameraListWidget::setConfigured(std::vector<bool> configured)
{
	setColumnCount(2);
	setHorizontalHeaderItem(1, new QTableWidgetItem("Configured"));
	for(size_t i = 0; i < desc_.size() && i < configured.size(); ++i)
	{
		setItem(static_cast<int>(i), 1, new QTableWidgetItem(configured[i] ? "Yes" : "No"));
		qDebug()<<"Set item "<<i<<1;
	}
}

void CameraListWidget::setCameras(const std::vector<cam::Camera::Description>& desc)
{
	desc_ = desc;
	qDebug()<<"cameras: "<<desc_.size();
	setRowCount(static_cast<int>(desc_.size()));
	for(size_t i = 0; i < desc_.size(); ++i)
		setItem(static_cast<int>(i), 0, new QTableWidgetItem((desc_[i].getVendor() + " " + desc_[i].getModel()).c_str()));
}

std::vector<cam::Camera::Description> CameraListWidget::getSelectedDescriptions()
{
	 QList<QModelIndex> selected = selectedIndexes();
	std::vector<cam::Camera::Description> cameras;
	for(auto& selection : selected)
	{
		if(selection.column() == 0)
			cameras.push_back(desc_[selection.row()]);
	}
	return cameras;
}
