#include "clusterflowmain.h"
#include "loadimage.h"
#include "qmessagebox.h"
#include "g12buffer.h"

ClusterFlowMain::ClusterFlowMain(QWidget *parent) : QDialog(parent)
{
	ui.setupUi(this);
	//fileDialog.setParent(this);
	//fileDialog.setModal(true);

	connect(ui.loadButton1, SIGNAL(clicked()), this, SLOT(activateLoadDialog1()));
	connect(ui.loadButton2, SIGNAL(clicked()), this, SLOT(activateLoadDialog2()));

	connect(ui.load1Button, SIGNAL(released()), this, SLOT(reloadBuffer1()));
	connect(ui.load2Button, SIGNAL(released()), this, SLOT(reloadBuffer2()));

	connect(this, SIGNAL(changeBuffer1(G12Buffer *)), &dialog1, SLOT(notifyBufferChangeSlot(G12Buffer *)));
	connect(this, SIGNAL(changeBuffer2(G12Buffer *)), &dialog2, SLOT(notifyBufferChangeSlot(G12Buffer *)));

	connect(ui.inputDetails1, SIGNAL(released()), &dialog1, SLOT(show()));
	connect(ui.inputDetails2, SIGNAL(released()), &dialog2, SLOT(show()));

	connect(ui.signaturesDetails1, SIGNAL(released()), &sigDialog1, SLOT(show()));
	connect(ui.signaturesDetails2, SIGNAL(released()), &sigDialog2, SLOT(show()));


	connect(&dialog1, SIGNAL(notifyBufferChange(G12Buffer *)), &sigDialog1, SLOT(notifyBufferChangeSlot(G12Buffer *)));
	connect(&dialog2, SIGNAL(notifyBufferChange(G12Buffer *)), &sigDialog2, SLOT(notifyBufferChangeSlot(G12Buffer *)));
	connect(&dialog1, SIGNAL(notifyBufferChange(G12Buffer *)), &flowDialog, SLOT(notifyBufferChangeSlot1(G12Buffer *)));
	connect(&dialog2, SIGNAL(notifyBufferChange(G12Buffer *)), &flowDialog, SLOT(notifyBufferChangeSlot2(G12Buffer *)));


	connect(ui.flowButton, SIGNAL(pressed()), &flowDialog, SLOT(show()));

	connect(&sigDialog1, SIGNAL(notifyCoratgesChange(CortageList *, G12Buffer *)), &flowDialog, SLOT(signatures1Changed(CortageList *)));
	connect(&sigDialog2, SIGNAL(notifyCoratgesChange(CortageList *, G12Buffer *)), &flowDialog, SLOT(signatures2Changed(CortageList *)));

	connect(&flowDialog, SIGNAL(needCoratgeListUpdate()), &sigDialog1 , SLOT(notifyRecalculate()));
	connect(&flowDialog, SIGNAL(needCoratgeListUpdate()), &sigDialog2 , SLOT(notifyRecalculate()));

	buffer1 = NULL;
	buffer2 = NULL;

	connect(ui.lockImagesButton, SIGNAL(released()), this, SLOT(onImageLock()));
	connect(ui.lockSignaturesButton, SIGNAL(released()), this, SLOT(onSignatureLock()));


	sigLock = true;
	imgLock = true;

	if (!sigLock)
	{
		disconnect (&sigDialog1,SIGNAL(parametersChanged(SignatureParameters)),&sigDialog2, SLOT(parametersChangedSlot(SignatureParameters)));
		disconnect (&sigDialog2,SIGNAL(parametersChanged(SignatureParameters)),&sigDialog1, SLOT(parametersChangedSlot(SignatureParameters)));
	} else {
		connect (&sigDialog1,SIGNAL(parametersChanged(SignatureParameters)),&sigDialog2, SLOT(parametersChangedSlot(SignatureParameters)));
		connect (&sigDialog2,SIGNAL(parametersChanged(SignatureParameters)),&sigDialog1, SLOT(parametersChangedSlot(SignatureParameters)));
	}

	if (!imgLock)
	{
		disconnect (&dialog1,SIGNAL(parametersChanged(GainParameters)),&dialog2, SLOT(parametersChangedSlot(GainParameters)));
		disconnect (&dialog2,SIGNAL(parametersChanged(GainParameters)),&dialog1, SLOT(parametersChangedSlot(GainParameters)));
	} else {
		connect (&dialog1,SIGNAL(parametersChanged(GainParameters)),&dialog2, SLOT(parametersChangedSlot(GainParameters)));
		connect (&dialog2,SIGNAL(parametersChanged(GainParameters)),&dialog1, SLOT(parametersChangedSlot(GainParameters)));
	}

	lockIcon = new QIcon(QString::fromUtf8(":/new/prefix1/lock.png"));
	unlockIcon = new QIcon(QString::fromUtf8(":/new/prefix1/unlock.png"));

	ui.lockImagesButton->setIcon(imgLock ? *lockIcon : *unlockIcon);
	ui.lockSignaturesButton->setIcon(sigLock ? *lockIcon : *unlockIcon);
}

void ClusterFlowMain::onSignatureLock()
{
	if (sigLock)
	{
		disconnect (&sigDialog1,SIGNAL(parametersChanged(SignatureParameters)),&sigDialog2, SLOT(parametersChangedSlot(SignatureParameters)));
		disconnect (&sigDialog2,SIGNAL(parametersChanged(SignatureParameters)),&sigDialog1, SLOT(parametersChangedSlot(SignatureParameters)));
	} else {
		connect (&sigDialog1,SIGNAL(parametersChanged(SignatureParameters)),&sigDialog2, SLOT(parametersChangedSlot(SignatureParameters)));
		connect (&sigDialog2,SIGNAL(parametersChanged(SignatureParameters)),&sigDialog1, SLOT(parametersChangedSlot(SignatureParameters)));
	}

	sigLock = !sigLock;
	ui.lockSignaturesButton->setIcon(sigLock ? *lockIcon : *unlockIcon);

}

void ClusterFlowMain::onImageLock()
{
	if (imgLock)
	{
		disconnect (&dialog1,SIGNAL(parametersChanged(GainParameters)),&dialog2, SLOT(parametersChangedSlot(GainParameters)));
		disconnect (&dialog2,SIGNAL(parametersChanged(GainParameters)),&dialog1, SLOT(parametersChangedSlot(GainParameters)));
	} else {
		connect (&dialog1,SIGNAL(parametersChanged(GainParameters)),&dialog2, SLOT(parametersChangedSlot(GainParameters)));
		connect (&dialog2,SIGNAL(parametersChanged(GainParameters)),&dialog1, SLOT(parametersChangedSlot(GainParameters)));
	}

	imgLock = !imgLock;
	ui.lockImagesButton->setIcon(imgLock ? *lockIcon : *unlockIcon);

}

ClusterFlowMain::~ClusterFlowMain()
{
	delete lockIcon;
	delete unlockIcon;
}


void ClusterFlowMain::reloadBuffer1()
{
	// Store the old buffer untill everybody will be notified about the change
	G12Buffer *old = buffer1;
	buffer1 = g12BufferCreateFromFile((char *)(ui.inputName1->text().toAscii().constData()));
	if (buffer1 == NULL)
	{
		QMessageBox::information(this, "Problem opening the file", QString("Could not open the file: %1").arg(ui.inputName1->text()),  "OK");
		buffer1 = old;
	}
	else
	{
		emit changeBuffer1(buffer1);
		if (old != NULL)
				g12BufferDelete(old);
	}


}

void ClusterFlowMain::reloadBuffer2()
{
	// Store the old buffer untill everybody will be notified about the change
	G12Buffer *old = buffer2;
	buffer2 = g12BufferCreateFromFile((char *)(ui.inputName2->text().toAscii().constData()));
	if (buffer2 == NULL)
	{
		QMessageBox::information(this, "Problem opening the file", QString("Could not open the file: %1").arg(ui.inputName2->text()),  "OK");
		buffer2 = old;
	}
	else
	{
		emit changeBuffer2(buffer2);
		if (old != NULL)
				g12BufferDelete(old);
	}
}

void ClusterFlowMain::activateLoadDialog1()
{
	activateLoadDialog(0);
}

void ClusterFlowMain::activateLoadDialog2()
{
	activateLoadDialog(1);
}


void ClusterFlowMain::activateLoadDialog(int num)
{
   QString s = QFileDialog::getOpenFileName(
		this,
		"Choose an input file",
		".",
#ifdef TIFF_SUPPORTED
		"Images (*.tiff *.ppm *.pgm *.raw)"
#else
		"Images (*.ppm *.pgm *.raw)"
#endif

	   );
    if (s.length() == 0)
    	return;
    if (num == 0)
    {
    	ui.inputName1->setText(s);
    	emit reloadBuffer1();
    }
    else
    {
    	ui.inputName2->setText(s);
    	emit reloadBuffer2();
    }
}




void ClusterFlowMain::setInputs(char *first, char *second)
{
	  ui.inputName1->setText(QString::fromUtf8("data/img.raw"));
	  ui.inputName2->setText(QString::fromUtf8("data/img.raw"));
}
