#include "sigdistributionwidget.h"
#include "global.h"
#include "stdlib.h"

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


	//ui.tableWidget->setSortingEnabled(true);

	//QObject *header = (QObject *)(ui.tableWidget->horizontalHeader());
	//connect(header, SIGNAL(sectionClicked(int)), this, SLOT(onHeaderClicked(int)));
    //disconnect(header, SIGNAL(sectionPressed(int)), ui.tableWidget, SLOT(selectColumn(int)));
	cortageList = NULL;
	connect(ui.tableWidget, SIGNAL(itemDoubleClicked (QTableWidgetItem *)), this, SLOT(onSignatureClick(QTableWidgetItem *)));

}

void SigDistributionWidget::onSignatureClick(QTableWidgetItem *item)
{
	if (item->type() != QTableWidgetItem::UserType + 1)
		return;

	QSignatureTableWidgetItem *sigItem = (QSignatureTableWidgetItem *)item;
	signature_t sig = sigItem->sig;
	emit updatePoints(sig);

	signature_t shift = sig;
	QTableWidgetItem *bitItem;
	for (int b = 0; b < SIGNATURE_PATTERNS_NUMBER; b++)
	{
		int bit = shift % TENARY_SYSTEM_BASE;
		bitItem = ui.bitTableWidget->item(b,0);
		bitItem->setSelected(bit == 0);
		bitItem = ui.bitTableWidget->item(b,1);
		bitItem->setSelected(bit == 1);
		bitItem = ui.bitTableWidget->item(b,2);
		bitItem->setSelected(bit == 2);
		shift /= TENARY_SYSTEM_BASE;
	}
}




int SigDistributionWidget::sortBySig(const void *a, const void *b)
{
    SignatureInfo **ca = (SignatureInfo **)(a); // casting pointer types
    SignatureInfo **cb = (SignatureInfo **)(b);
    return (*ca)->sig - (*cb)->sig;
	/* integer comparison: returns negative if b > a
	and positive if a > b */
}

int SigDistributionWidget::sortByNum(const void *a, const void *b)
{
    SignatureInfo **ca = (SignatureInfo **)a; // casting pointer types
    SignatureInfo **cb = (SignatureInfo **)b;
    return (*ca)->count - (*cb)->count;
	/* integer comparison: returns negative if b > a
	and positive if a > b */
}

void SigDistributionWidget::regenerateTables()
{
	if (cortageList == NULL)
		return;
	maxcount = coratgeListDifferentSignatures(cortageList);

	ui.tableWidget->clear();
	ui.tableWidget->setColumnCount(2);
	ui.tableWidget->setRowCount(maxcount);
	ui.tableWidget->setSortingEnabled(false);
	QTableWidgetItem *titleItem1 = new QTableWidgetItem(tr("Signature"));
	QTableWidgetItem *titleItem2 = new QTableWidgetItem(tr("Count"));

	ui.tableWidget->setHorizontalHeaderItem(0, titleItem1);
	ui.tableWidget->setHorizontalHeaderItem(1, titleItem2);
	ui.tableWidget->setColumnWidth(0, 120);
	ui.tableWidget->setColumnWidth(1, 50);

	int i;
	for (i = 0; i < SIGNATURE_PATTERNS_NUMBER; i++)
	{
		tenaryBits[i][2] = tenaryBits[i][1] = tenaryBits[i][0] = 0;
	}

	int count = 0;
	int same = 1;
	signature_t sig = cortageList->cortages[0].signature;
	for (i = 1; i < cortageList->active; i++)
	{
		if (cortageList->cortages[i].signature == sig && i != cortageList->active - 1)
		{
			same++;
			continue;
		}

		/* Fill the left table */
		QSignatureTableWidgetItem *Item1 = new QSignatureTableWidgetItem(sig);
		QNumTableWidgetItem *Item2 = new QNumTableWidgetItem(same);
		Item1->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
		Item2->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
		ui.tableWidget->setItem(count, 0, Item1);
		ui.tableWidget->setItem(count, 1, Item2);

		/* Count bits statistics */
		signature_t shift = sig;
		for (int b = 0; b < SIGNATURE_PATTERNS_NUMBER; b++)
		{
			int bit = shift % TENARY_SYSTEM_BASE;
			tenaryBits[b][bit] += same;
			shift /= TENARY_SYSTEM_BASE;
		}

		count++;
		same = 1;
		sig = cortageList->cortages[i].signature;
	}
#ifdef ASSERTS
	if (count != maxcount)
		DOASSERT("Internal Trouble");
#endif

	ui.tableWidget->setSortingEnabled(true);

	QTableWidgetItem *Item;
	ui.bitTableWidget->clear();
	ui.bitTableWidget->setColumnCount(3);
	ui.bitTableWidget->setRowCount(SIGNATURE_PATTERNS_NUMBER);
	ui.bitTableWidget->setSortingEnabled(false);

	Item = new QTableWidgetItem(tr("0"));
	ui.bitTableWidget->setHorizontalHeaderItem(0, Item);
	Item = new QTableWidgetItem(tr("1"));
	ui.bitTableWidget->setHorizontalHeaderItem(1, Item);
	Item = new QTableWidgetItem(tr("2"));
	ui.bitTableWidget->setHorizontalHeaderItem(2, Item);

	ui.bitTableWidget->setColumnWidth(0, 50);
	ui.bitTableWidget->setColumnWidth(1, 50);
	ui.bitTableWidget->setColumnWidth(2, 50);


	for (int b = 0; b < SIGNATURE_PATTERNS_NUMBER; b++)
	{
		Item = new QTableWidgetItem(QString("%1").arg(tenaryBits[b][0]));
		Item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
		ui.bitTableWidget->setItem(b, 0, Item);
		Item = new QTableWidgetItem(QString("%1").arg(tenaryBits[b][1]));
		Item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
		ui.bitTableWidget->setItem(b, 1, Item);
		Item = new QTableWidgetItem(QString("%1").arg(tenaryBits[b][2]));
		Item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
		ui.bitTableWidget->setItem(b, 2, Item);
	}

}

void SigDistributionWidget::signatureListChanged(CortageList *CL)
{
	if (CL == NULL)
		return;
	cortageList = CL;
	if (!this->isHidden())
		regenerateTables();
}

void SigDistributionWidget::onHeaderClicked(int section)
{
	//changedSorting(section);
}


SigDistributionWidget::~SigDistributionWidget()
{

}
