#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "Main.hpp"
#include "GSATThreadMPI.hpp"
#include "color.h"

extern int world_rank;

/* --- Public --- */

GSATThreadMPI::GSATThreadMPI(int _nbThread, int argc, char** argv) {
	nbThread = _nbThread;
	gsat = std::vector<GSAT*>(_nbThread);
	threads = std::vector<std::thread>(_nbThread);
	for(int i = 0; i < _nbThread; i++) {
		gsat[i] = new GSAT();
		gsat[i]->setParameters(argc,argv);
  		gsat[i]->initialize();
	}
	srand(getpid());
}

GSATThreadMPI::~GSATThreadMPI() {
	for(int i = 0; i < nbThread; i++) {
		delete gsat[i];
	}
}

bool GSATThreadMPI::solve() {
	threads.clear();
	end = false;
	find = false;
	//Lance thread
	for(int i = 0; i < nbThread; i++) {
		threads[i] = std::thread(&GSATThreadMPI::solverThread, this, i);
	}
	//Lance thread de synchronisation MPI
	mpiSync = std::thread(mpiWait, this);
	//Attend resultat
	for(int i = 0; i < nbThread; i++){
		threads[i].join();
	}
	mpiSync.join();
	//Si c'est le proc qui à trouvé
	if(find) {
		sleep(1); //Laisse le temps aux autres de se couper
		//Affiche resultat
		printf("-------------------------------------------------------------------------------------------------------\n");
		printf(GREEN);
		printf("s %s\n",find?"SATISFIABLE":"NOT FOUND");
		printf(YELLOW);
        printf("c real time : %.4f seconds\n", result.calcTime);
        printf("c [processor:%d][thread:%2d][iteration:%5d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
			world_rank,
			result.threadId,
			result.nbIteration,
			result.heuristicFill,
			result.heuristicSolve,
			result.nbSatisfiedClausesFill,
			result.nbSatisfiedClausesSolve);
        printf(RESET);
        fflush(stdout);
	}
  	return find;
}

void GSATThreadMPI::isEnd() {
	end = true;
}

/* --- Private --- */

void GSATThreadMPI::solverThread(int id) {
	//Resolution
	double startTime = gsat[id]->realTime();
	bool solve = false;
	int cpt = 0;
	while(!end && !solve){
		//Pour eviter que les processeurs effectue tous les meme calculs
        int fill = rand() % 4 + 2;
        int h = rand() % 7;
        solve = gsat[id]->start(fill, h);
        if(solve) {
        	printf(CYAN);
        }
		printf("c [processor:%d][thread:%2d][iteration:%5d][fill:%d][heuristic:%d]Satisfied clauses (begin: %d)(end:%d)\n",
			world_rank,
			id,
		   	gsat[id]->getNbIterations(),
		   	gsat[id]->getHeuristicFill(),
		   	gsat[id]->getHeuristicSolve(),
		   	gsat[id]->getNbSatisfiedClausesFill(),
		   	gsat[id]->getNbSatisfiedClausesSolve());
		if(solve) {
			printf(RESET);
		}
		cpt++;
	}
	//Si 1er arreter
	if(!end && solve) {
		mpiNotify(world_rank);
		end = true;
		find = true;
		result.threadId = id;
		result.calcTime = gsat[id]->realTime() - startTime;
		result.nbIteration = gsat[id]->getNbIterations();
		result.heuristicFill = gsat[id]->getHeuristicFill();
		result.heuristicSolve = gsat[id]->getHeuristicSolve();
		result.nbSatisfiedClausesFill = gsat[id]->getNbSatisfiedClausesFill();
		result.nbSatisfiedClausesSolve = gsat[id]->getNbSatisfiedClausesSolve();
	}
}
