 
#include <fstream>
#include <iostream>
#include "globaldef.h"
#include "grace_tags.h"
#include "messages_manager.h"

#ifdef _USE_NAMESPACES
    namespace Else { 
#endif // _USE_NAMESPACES

void dumpHelp();
void checkParams(Parameters&);
int main(int argc, char* argv[]);

typedef vector<conform_string> StringsCollection;
StringsCollection inputs;
HtmlResultats global;

void dumpHelp()
{
    cerr << "Description:  (Evaluation part)\n"
            "  Read all result given in input, and write (stdout) the global result.\n"
            "Usage: [<evaluatred> -> ] patch_eval {<evaluated>} > all.res\n"
            "  <evaluated>       : an evaluation file.\n";
    cerr << "\n";
    exit (0);
}

void checkParams(Parameters& params)
{
    if ( !params.empty() && ( !(strcmp(params.front(), "?"))
        ||
           !(strcmp(params.front(), "-?"))
        || 
           !(strcmp(params.front(), "-h"))
        ||
           !(strcmp(params.front(), "-help"))
        ||
           !(strcmp(params.front(), "--help"))
       )) dumpHelp();

	for (Parameters::const_iterator currentArg = params.begin(); currentArg != params.end(); ++currentArg) {
        inputs.push_back(conform_string(*currentArg));
    }
}

// Temp: kappa test with martin.
void
printFinal(ostream& os, const HtmlResultats& allresult, const char*const head)
{
	os << head << "total case_nb (=err + ok + sil + noneval): " << allresult.nbCases() << '\n'
	   << head << "case_nb (=err + ok + sil): " << (allresult.errs() + allresult.oks() + allresult.silences()) << '\n'
	   << head << "noneval: " << allresult.unevaluable()+allresult.wrongAlign() << " (" << allresult.unevaluable() << '+'
	                   << allresult.wrongAlign() << ")\n"
	   << head << "noneval_ALIGN: " << allresult.wrongAlign() << '\n'
	   << head << "ok: " << allresult.oks() << '\n'
	   << head << "error: " << allresult.errs() << '\n'
	   << head << "sil: " << allresult.silences() << '\n';
	os.setf(ios::fixed, ios::floatfield);
	os << head << "silok_prb: " << setprecision(2) << allresult.averageSilOk() << '\n'
	   << head << "silerr_prb: " << setprecision(2) << allresult.averageSilErr() << '\n';
	os.setf(0, ios::floatfield);	   
	os << head << "sil_ok_case_nb: " << allresult.silencesOk() << '\n'
	   << head << "sil_err_case_nb: " << allresult.silencesErr() << '\n'
	   << head << "sil_sil_case_nb: " << (allresult.silences() - (allresult.silencesOk() + allresult.silencesErr())) << '\n';
	os.setf(ios::fixed, ios::floatfield);
	os << head << "score( P, D ): (" << setprecision(6) << allresult.precision() << " , "  << allresult.decision() << ")\n"
	   << head << "intervalle[Pmin, Pmoy, Pmax]: [" << allresult.minPrecision() << " , "
	                                                << allresult.averagePrecision() << " , "
	                                                << allresult.maxPrecision() << " ]\n";
	os.setf(0, ios::floatfield);	   
}	                                        


void
computing(istream& in) {
	HtmlResultats resultats;
	while (HtmlField::skipToTag(in, &resultats)) global.mergeWith(resultats);
}

int
main(int argc, char* argv[]) {
    Parameters params(argc, argv);
    checkParams(params);

	if (inputs.empty()) computing(cin);
	else for (StringsCollection::const_iterator i = inputs.begin(); i != inputs.end(); ++i)
	{
		ifstream in(i->c_str(), ios::in);
		if (in) computing(in);
		else msg.message(string("Input file '")+*i+string("' not found !"), STANDARD_MSG);
		in.close();
	}
	global.computeScore();
	printFinal(cout, global, "");
}



