/* 
 * File:   newCSimpleTest.c
 * Author: blackpanther
 *
 * Created on October 31, 2010, 1:26 PM
 */

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

#include "configuration.h"

#include "logger.h"
#include "utilities.h"

#include "problema.h"
#include "method.h"
#include "result.h"

#include "model/testProblem.h"

#include "control/testMathsLibraryManager.h"
#include "control/testXMLParser.h"

#define launchTest(testname,testfunction) \
    logMessage(INFO,"TEST_STARTED",testname);                                           \
    if( testfunction )                                                                  \
    	logMessage(INFO,"TEST_SUCCESS","time=0 testProblem (OptimServer Test Suite)");  \
    else                                                                                \
    	logMessage(INFO,"TEST_FAILED","time=0 testProblem (OptimServer Test Suite)");   \
    logMessage(INFO,"TEST_FINISHED",testname);

int main(int argc, char** argv) {
	Problema *problem = NULL;
	Method   *method  = NULL;
	Result   *result  = NULL;

	int i;

    resetToDefaultLogger();
    setLoggerLevel(ALL);

    char   xmlProblemFile[MAX_STRING_INPUT] = {0};
    char   methodName[MAX_STRING_INPUT]     = {0};
	void   *args[MAX_ITEM] = {0};

	logMessage(INFO,"test suite","launch");

    //read data from input file
    scanf("%s",xmlProblemFile);
    logMessage(DEBUG1,"xml problem file fetched",xmlProblemFile);

    scanf("%s",methodName);
    logMessage(DEBUG1,"methodName fetched",methodName);

    scanf("%s",methodName);
    logMessage(DEBUG1,"methodName fetched",methodName);

//Problem
	problem = parseProblemFromFile(xmlProblemFile);

	if( !problem )
	{
		sprintf(buffer,"unable to parse problem from %s",xmlProblemFile);
		logMessage(SEVERE,"problem parsing",buffer);
		return -1;
	}

	logMessage(INFO,"problem",problem->display(problem));

////Method
//	method = loadMethod(methodName);
//
//	if( !method )
//	{
//		destroyProblema(problem);
//		sprintf(buffer,"unable to load %s method",methodName);
//		logMessage(SEVERE,"method loading",buffer);
//		return -2;
//	}
//
//	logMessage(INFO,"method",method->_name);

////Result
	result = parseResultFromFile(xmlResultFile);

	if( !result )
	{
		sprintf(buffer,"unable to parse result from %s",xmlResultFile);
		logMessage(SEVERE,"result parsing",buffer);
		return -1;
	}

	logMessage(INFO,"result",result->display(result));
//	double vector[problem->_dimension];
//	for(i=0;i<problem->_dimension;i++)
//		scanf("%lf",&vector[i]);
//
//	logMessage(DEBUG1,"vector fetched",printVector(vector,problem->_dimension));
//
//	char word[MAX_STRING_INPUT] = {0};
//	char *cursor = NULL;
//
//	double pas;
//	double epsilon;
//	int    patience;
//	double temperature;
//	double neighborStep;
//	int    levelLimit;
//	int    populationSize;
//	double crossProbability;
//	double mutationProbability;
//	double bounds_min, bounds_max;
//	double evaporationRate;
//	int    antNumber;
//
//	while( fgets(buffer,MAX_STRING_INPUT,stdin) )
//	{
//		strcpy(word,"pas");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			pas = atof( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"pas fetched",doubleToString(pas));
//		}
//
//		strcpy(word,"epsilon");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			epsilon = atof( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"epsilon fetched",doubleToString(epsilon));
//		}
//
//		strcpy(word,"patience");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			patience = atoi( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"patience fetched",integerToString(patience));
//		}
//
//		strcpy(word,"temperature");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			temperature = atof( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"temperature fetched",doubleToString(temperature));
//		}
//
//		strcpy(word,"neighborStep");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			neighborStep = atof( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"neighbor step fetched",doubleToString(neighborStep));
//		}
//
//		strcpy(word,"levelLimit");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			levelLimit = atoi( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"levelLimit fetched",integerToString(levelLimit));
//		}
//
//		strcpy(word,"populationSize");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			populationSize = atoi( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"populationSize fetched",integerToString(populationSize));
//		}
//
//		strcpy(word,"crossProbability");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			crossProbability = atof( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"crossProbability fetched",doubleToString(crossProbability));
//		}
//
//		strcpy(word,"mutationProbability");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			mutationProbability = atof( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"mutationProbability fetched",doubleToString(mutationProbability));
//		}
//
//		strcpy(word,"boundsMin");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			bounds_min = atof( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"boundsMin fetched",doubleToString(bounds_min));
//		}
//
//		strcpy(word,"boundsMax");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			bounds_max = atof( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"boundsMax fetched",doubleToString(bounds_max));
//		}
//
//		strcpy(word,"evaporationRate");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			evaporationRate = atof( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"evaporationRate fetched",doubleToString(evaporationRate));
//		}
//
//		strcpy(word,"antNumber");
//		cursor = strstr(buffer,word);
//
//		if( cursor )
//		{
//			logMessage(DEBUG2,"cursor analysis",(cursor+strlen(word)+1));
//			antNumber = atoi( (cursor+strlen(word)+1) );
//			logMessage(DEBUG1,"antNumber fetched",integerToString(antNumber));
//		}
//	}
//
//	args[INDEX_EPSILON]          = &epsilon;
//	args[INDEX_PATIENCE]         = &patience;
//	args[INDEX_STEP]             = &pas;
//	args[INDEX_TEMPERATURE]      = &temperature;
//	args[INDEX_NEIGHBOR_STEP]    = &neighborStep;
//	args[INDEX_LEVEL_LIMIT]      = &levelLimit;
//	args[INDEX_POPULATION_SIZE]  = &populationSize;
//	args[INDEX_CROSS_PROBA]      = &crossProbability;
//	args[INDEX_MUTATION_PROBA]   = &mutationProbability;
//	args[INDEX_BOUNDS_MIN]       = &bounds_min;
//	args[INDEX_BOUNDS_MAX]       = &bounds_max;
//	args[INDEX_EVAPORATION_RATE] = &evaporationRate;
//	args[INDEX_ANT_NUMBER]       = &antNumber;
//
//	logMessage(INFO,"resolve","launch method");
//
//	result = resolve(
//			problem,
//			method,
//			vector,
//			args);
//
//	if( !result )
//	{
//		sprintf(buffer,"unable to resolve %s with %s method",problem->functionRepresentation,method->_name);
//		destroyProblema(problem);
//		destroyMethod(method);
//		logMessage(SEVERE,"problem resolve",buffer);
//		return -3;
//	}
//
//    setLoggerLevel(INFO);
//
//    logMessage(INFO,"result",result->display(result));

//	logMessage(INFO,"print xml",writeProblemXML(problem));

    return (EXIT_SUCCESS);
}
