#include "viflowdialog.h"
#include "qpainter.h"
#include "g12Image.h"

#include <math.h>
#include <qmessagebox.h>



ViFlowDialog::ViFlowDialog(QWidget *parent)
    : QDialog(parent)
{
	ui.setupUi(this);

	/* Main UI*/
	connect(ui.widget, SIGNAL(askParentRepaint(QPaintEvent *, QWidget *)), this, SLOT(repaintChild(QPaintEvent *, QWidget *)));
	connect(ui.widget, SIGNAL(notifyParentMouseMove(QMouseEvent* )), this, SLOT(mouseChild(QMouseEvent *)));

	connect(ui.pointsButton, SIGNAL(released()), ui.widget, SLOT(update()));
	connect(ui.arrowsButton, SIGNAL(released()), ui.widget, SLOT(update()));
	connect(ui.noneButton, SIGNAL(released()), this, SLOT(update()));

	connect(ui.leftBufferButton, SIGNAL(released()),  ui.widget, SLOT(update()));
	connect(ui.rightBufferButton, SIGNAL(released()),  ui.widget, SLOT(update()));

	ui.arrowsButton->setChecked(true);

//	ui.recalculateButton->setEnabled(false);

	ui.zoneSizeBox->setMinimum(0);
	ui.zoneSizeBox->setMaximum(10);
	ui.zoneSizeBox->setValue(4);

	ui.maxDeviationBox->setMinimum(0);
	ui.maxDeviationBox->setMaximum(2000);
	ui.maxDeviationBox->setValue(9);

	ui.goodSamplesBox->setMinimum(0);
	ui.goodSamplesBox->setMaximum(80);
	ui.goodSamplesBox->setValue(2);

	ui.maxSigBox->setMinimum(0);
	ui.maxSigBox->setMaximum(20);
	ui.maxSigBox->setValue(7);

	flow = NULL;
	clist1 = NULL;
	clist2 = NULL;
	buf1 = NULL;
	buf2 = NULL;

	ui.statisticsLabel->setText("No Flow");
	ui.shouldFilterBox->setChecked(true);
	connect(ui.recalculateButton, SIGNAL(pressed()), this, SLOT(notifyRecalculate()));

}


void ViFlowDialog::signatures1Changed(CortageList *clist1)
{
	if (this->buf1 != NULL)
		this->clist1 = clist1;
	//this->buf1 = buf;
//	ui.recalculateButton->setEnabled(true);
}

void ViFlowDialog::signatures2Changed(CortageList *clist2)
{
	if (this->buf2 != NULL)
		this->clist2 = clist2;
//	this->buf2 = buf;
//	ui.recalculateButton->setEnabled(true);
}

void ViFlowDialog::notifyBufferChangeSlot1(G12Buffer *buf)
{
	this->buf1 = buf;
	this->flow = NULL;
	ui.statisticsLabel->setText("No Flow");
	ui.widget->update();
//	ui.recalculateButton->setEnabled(true);
}

void ViFlowDialog::notifyBufferChangeSlot2(G12Buffer *buf)
{
	this->buf2 = buf;
	this->flow = NULL;
	ui.statisticsLabel->setText("No Flow");
	ui.widget->update();
//	ui.recalculateButton->setEnabled(true);
}

    /* Process the child repainting */
void ViFlowDialog::repaintChild(QPaintEvent *event, QWidget *who)
{
	QPainter painter(who);
	if (ui.leftBufferButton->isChecked() && buf1 != NULL)
	{
		g12Image image(buf1);
		painter.drawImage(QPoint(0,0), image);
	}

	if (ui.rightBufferButton->isChecked() && buf2 != NULL)
	{
		g12Image image(buf2);
		painter.drawImage(QPoint(0,0), image);
	}

	if (buf1 == NULL)
		painter.drawText(0,0, who->width()/2, who->height(), Qt::AlignHCenter | Qt::AlignVCenter, QString("NO LEFT IMAGE"));
	if (buf2 == NULL)
		painter.drawText(who->width()/2, 0, who->width()/2, who->height(), Qt::AlignHCenter | Qt::AlignVCenter, QString("NO RIGHT IMAGE"));


	if (flow == NULL)
		return;
	if (ui.arrowsButton->isChecked())
	{
		int i,j;
		for (i = 6; i < flow->h - 6; i++ )
		{
			for (j = 6; j < flow->w - 6; j++ )
			{
				if (FELEM(flow, j,i).x == FLOW_UNKNOWN_X || FELEM(flow, j,i).y == FLOW_UNKNOWN_Y)
					continue;
				vector2Ds16 point = FELEM(flow, j,i);
				int color = sqrt((double)point.x * point.x + point.y * point.y) * 20;
				if (color > 255)
					color = 255;
				painter.setPen(QColor(0, color, 255));
				painter.setBrush(QColor(0, color, 255));
				painter.drawLine(j,i,j + point.x, i + point.y);

			}
		}
	}
	if (ui.pointsButton->isChecked())
	{
		int i,j;
		for (i = 6; i < flow->h - 6; i++ )
		{
			for (j = 6; j < flow->w - 6; j++ )
			{
				if (FELEM(flow, j,i).x == FLOW_UNKNOWN_X || FELEM(flow, j,i).y == FLOW_UNKNOWN_Y)
					continue;
				vector2Ds16 point = FELEM(flow, j,i);
				int color = sqrt((double)point.x * point.x + point.y * point.y) * 20;
				if (color > 255)
					color = 255;
				//color = (color << 16) | (0xFF);
				painter.setBrush(QColor(0, color, 255));
				painter.setPen(QColor(0, color, 255));
				painter.drawPoint(j,i);
			}
		}
	}


}

void ViFlowDialog::mouseChild(QMouseEvent *event)
{

}

void ViFlowDialog::notifyRecalculate(void)
{
	emit needCoratgeListUpdate();

//	ui.recalculateButton->setEnabled(false);

	if (clist1 == NULL || clist2 == NULL)
		return;

	int zoneSize = ui.zoneSizeBox->value();
	int hitsThres = ui.maxDeviationBox->value();
	int hitsLimit = ui.goodSamplesBox->value();
	int cutOut = ui.maxSigBox->value();
	int shouldFilter = ui.shouldFilterBox->isChecked();

	if (clist1->h != clist2->h || clist1->w != clist2->w)
	{
		QMessageBox::information(this, "Files have different sizes",
				QString("File sizes differ [%1x%2] and [%3x%4]")
					.arg(clist1->w).arg(clist1->h)
					.arg(clist2->w).arg(clist2->h),  "OK");
		return;
	}

	FlowBuffer *newFlow = matchCortagesWize(clist1, clist2, cutOut);
	if (shouldFilter)
	{
		FlowBuffer *filteredFlow = filterFlowSimple(newFlow, zoneSize, hitsThres, hitsLimit);
		flowBufferDelete(newFlow);
		newFlow = filteredFlow;
	}

	if (flow != NULL)
		flowBufferDelete(flow);
	flow = newFlow;
	int density = flowCountDensity(flow);
	double persent = (double)density * 100.0 / (flow->h * flow->w);
	ui.statisticsLabel->setText(QString("%1 %").arg(persent));
	ui.widget->update();
}

ViFlowDialog::~ViFlowDialog()
{

}
