#include "inputoutput.h"
#include "apriori.h"
#include "timerSystem.h"
#define STATS_FILENAME "stats.csv"

int main()
{
	TimerSystem timer;
	Trie largeItemsets;
	ArrayInfo2D transactionSizeInfo;
	bool **transactions = NULL;

	string filename = "";
	bool keepRunning = true;
	int minimumSupport = 0;
	double time, parallelTime;
	float speedUp;
	float efficacity;
	float realEfficacity;
	string numTransactions, numItems, maxThreads;
	CreateData(STATS_FILENAME);
	while (keepRunning)
	{
		
		filename = getFileName();
		//	filename = "datasets/T5.N0.1K.D1K.txt";
		//filename = "datasets/T10I4D100K.dat";
		//filename = "datasets/chess.dat";
		//filename = "datasets/test.txt";

		cout << "number Transactions: ";
		getline(cin, numTransactions);

		cout << "number Items: ";
		getline(cin, numItems);

		cout << "max Threads: ";
		getline(cin, maxThreads);

		minimumSupport = getMinimumSupport(stoi(numTransactions));

		largeItemsets.clearTrie();

		if (!loadData(transactions, transactionSizeInfo, filename, numTransactions, numItems))
			continue;

		//minimumSupport = getMinimumSupport(transactionSizeInfo.sizeI);

		//minimumSupport = 5;

		timer.startClock();
		runApriori(const_cast<const bool**>(transactions), transactionSizeInfo, minimumSupport, largeItemsets);
		time = timer.getTime();

		cleanupTransactions(transactions, transactionSizeInfo);
		
		for (int nbThreads = 2; nbThreads < stoi(maxThreads); nbThreads++){
			largeItemsets.clearTrie();
			if (!loadData(transactions, transactionSizeInfo, filename, numTransactions, numItems))
				continue;
			timer.startClock();
			runParallelApriori(const_cast<const bool**>(transactions), transactionSizeInfo, minimumSupport, largeItemsets, nbThreads);
			parallelTime = timer.getTime();
			speedUp = time / parallelTime;
			efficacity = speedUp / nbThreads;
			realEfficacity = speedUp / 4;
			WriteData(STATS_FILENAME, nbThreads, stoi(numTransactions), stoi(numItems), minimumSupport, parallelTime, speedUp, efficacity, realEfficacity);
			cleanupTransactions(transactions, transactionSizeInfo);
		}
		//outputResults(time, largeItemsets);
		//WriteOutputResults(time, largeItemsets);
		

		//cleanupTransactions(transactions, transactionSizeInfo);
		keepRunning = getContinue();
	}

	return 0;
}