#include "CameraCalibrationWindow.h"
#include "ProcessorThread.h"
#include <QMessageBox>
#include <QGraphicsPixmapItem>
#include <QLabel>
#include <QCloseEvent>
#include <QInputDialog>
#include <QDialog>
#include <QDebug>

#include <string>
#include <sstream>
#include <iostream>

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

CameraCalibrationWindow::CameraCalibrationWindow(ProcessorThread* thread, QWidget *parent, Qt::WindowFlags flags)
	: ProcessorWindow(thread, parent, flags)
{
	ui.setupUi(this);
	QGraphicsScene *scene = new QGraphicsScene(this);
	ui.graphicsView->setScene(scene);

	ui.propertyEditor->addObject(pt->processorObject);
	ui.propertyEditor->addObject(pt->processorObject->proc);

	connect(ui.applyButton, SIGNAL( clicked() ), this, SLOT( applyLists() ));
	connect(ui.acquireButton, SIGNAL( clicked() ), this, SLOT( acquire() ));
	connect(ui.calibrateButton, SIGNAL( clicked() ), this, SLOT( calibrate() ));
}

CameraCalibrationWindow::~CameraCalibrationWindow()
{

}

void CameraCalibrationWindow::frameIn(int /*id*/, cv::Mat* frame) {
	QImage img(frame->cols, frame->rows, QImage::Format_RGB888);
	memcpy(img.bits(), frame->data, frame->cols*frame->rows*3);
    img = img.rgbSwapped();
	//img.loadFromData(frame->ptr(),width*height*4);
	/*for(int i=0; i<width; ++i)
	{
		for(int j=0; j<height; ++j)
		{
			img.setPixel(i,j,qRgb(frame->data[j*width+i], frame->data[j*width+i], frame->data[j*width+i]));
		}
	}*/
	QPixmap item = QPixmap::fromImage(img);
	//item.loadFromData(frame->ptr(),width*height*3);
	//memcpy(item.data_ptr().data(), frame->ptr(), width*height*3);
	
	ui.graphicsView->scene()->clear();
	ui.graphicsView->scene()->addPixmap(item);
	ui.graphicsView->scene()->setSceneRect(0, 0, frame->cols, frame->rows);
	ui.graphicsView->fitInView(ui.graphicsView->scene()->sceneRect(), Qt::AspectRatioMode::KeepAspectRatio);
	ui.graphicsView->show();

	delete frame;
}

void CameraCalibrationWindow::updateLists(QList<CameraPreview*> cameraList, QList<ProcessorWindow*> processorList)
{
	ui.listWidget->clear();
	QListWidgetItem* item;

	for(int i=0; i<cameraList.size(); ++i)
	{
		item = new QListWidgetItem();
		item->setText(cameraList[i]->windowTitle());
		item->setData(QListWidgetItem::UserType+1, cameraList[i]->ct->cameraObject->cameraType);
		item->setData(QListWidgetItem::UserType+2, cameraList[i]->ct->cameraObject->cameraID);
		ui.listWidget->addItem(item);
	}

    
	pCameraList = cameraList;

	
	QList<CameraObject*> camList;
	QList<ProcessorObject*> procList;
	emit connectInputs(camList, procList);

}

void CameraCalibrationWindow::applyLists()
{
	QMessageBox msgBox;
	QList<CameraObject*> camList;
	QList<ProcessorObject*> procList;

	QList<QListWidgetItem*> selected = ui.listWidget->selectedItems();

	for(int i=0; i<ui.listWidget->count(); ++i)
	{
		ui.listWidget->item(i)->setBackgroundColor(QColor(255,255,255));
	}
	
	if(selected.size() != 1)
	{
		msgBox.setText("Select device!");
		msgBox.exec();
		return;
	}

    qDebug() << pCameraList.size();
    
	for(int i=0; i<pCameraList.size(); ++i)
	{
		for(int j=0; j<selected.size(); ++j)
		{
			if(selected[j]->data(QListWidgetItem::UserType+1).toInt() == (pCameraList)[i]->ct->cameraObject->cameraType && selected[j]->data(QListWidgetItem::UserType+2).toInt() == (pCameraList)[i]->ct->cameraObject->cameraID)
			{
				selected[j]->setBackgroundColor(QColor(0,200,0));
				camList.push_back((pCameraList)[i]->ct->cameraObject);
			}
		}
	}
	emit connectInputs(camList, procList);
}

void CameraCalibrationWindow::acquire()
{
	emit signalSpecial(QString("acquire"));
}

void CameraCalibrationWindow::calibrate()
{
	emit signalSpecial(QString("calibrate"));
}
