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

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

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

ObjectExtractionWindow::ObjectExtractionWindow(ProcessorThread* thread, QWidget *parent, Qt::WindowFlags flags)
	: ProcessorWindow(thread, parent, flags)
{
	ui.setupUi(this);
    
    QGraphicsScene *scene = new QGraphicsScene(this);
	ui.graphicsView_0->setScene(scene);
//    scene = new QGraphicsScene(this);
//    ui.graphicsView_1->setScene(scene);
//    scene = new QGraphicsScene(this);
//    ui.graphicsView_2->setScene(scene);
//    scene = new QGraphicsScene(this);
//    ui.graphicsView_3->setScene(scene);

	ui.propertyEditor->addObject(pt->processorObject);
	ui.propertyEditor->addObject(pt->processorObject->proc);
    
    connect(ui.graphicsView_0, SIGNAL(clicked(int, int)), this, SLOT(object_picked(int, int)));
	
	connect(ui.get_backgroundButton, SIGNAL(clicked()), this, SLOT(get_background()));
    connect(ui.save_backgroundButton, SIGNAL(clicked()), this, SLOT(save_background()));
    connect(ui.load_backgroundButton, SIGNAL(clicked()), this, SLOT(load_background()));
}

ObjectExtractionWindow::~ObjectExtractionWindow()
{

}

void ObjectExtractionWindow::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();
	QPixmap item = QPixmap::fromImage(img);
	//item.loadFromData(frame->ptr(),width*height*3);
	//memcpy(item.data_ptr().data(), frame->ptr(), width*height*3);
	
    switch(id) {
        case 0:
            ui.graphicsView_0->scene()->clear();
            ui.graphicsView_0->scene()->addPixmap(item);
            ui.graphicsView_0->scene()->setSceneRect(0, 0, frame->cols, frame->rows);
            ui.graphicsView_0->fitInView(ui.graphicsView_0->scene()->sceneRect(), Qt::AspectRatioMode::KeepAspectRatio);
            ui.graphicsView_0->show();
            break;
//        case 1:
//            ui.graphicsView_1->scene()->clear();
//            ui.graphicsView_1->scene()->addPixmap(item);
//            ui.graphicsView_1->scene()->setSceneRect(0, 0, frame->cols, frame->rows);
//            ui.graphicsView_1->fitInView(ui.graphicsView_1->scene()->sceneRect(), Qt::AspectRatioMode::KeepAspectRatio);
//            ui.graphicsView_1->show();
//            break;
    }
    
	

	delete frame;
}
