/*---------------------------------------------------------------------------------------------------------------------
- File      : grapher.cc                                                                  Project ELSE, EPFL - DI/LIA -
-                                                                       Evaluation in Language and Speech Engineering -
- Author    : Raphaël Rossi     Creation date : 25/08/1998                                                            -
- Eulogist  : -                 Approval date : -                  Version: 1.3                                       -
-                                                                                                                     -
- Descript. : Construct graph file with result of evaluations.                                                        -
-                                                                                                                     -
- Requested : -                                                                                                       -
-                                                                                                                     -
- Gaps      : o) N'admet en entree qu'une evaluation par fichier.                                                     -
-                                                                                                                     -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Rev. date | Reviser               | Revise's description                                                            -
- - - - - - + - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- 04/10/1999| Seydoux Florian       | Adaptation to the new input file format                                         -
---------------------------------------------------------------------------------------------------------------------*/

 
#include <fstream>
#include <iostream>
#include "globaldef.h"
#include "grace_tags.h"
#include "ps_output.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;

bool           points(false);
conform_string boxes_filename("boxes.ps");
conform_string triangles_filename("triangles.ps");
conform_string boxes_graph_title("Boxes comparison");
conform_string triangles_graph_title("Scores comparison");
conform_string subTitle;
StringsCollection inputs;

void dumpHelp()
{
    cerr << "Description:  (Evaluation part)\n"
            "  Produce postscript file with resultats boxes or triangles representation.\n"
            "Usage: grapher {switches} {<evaluated>}\n"
            "  <evaluated>       : an evaluation file.\n"
            "Switches:\n"
            "  -b boxes_file     : set the file name of the boxes postscript (default: boxes.ps)\n"
            "  -s \" text \"       :  specify the graph subtitle\n"
            "  -t triangles_file : set the file name of the triangle postscript (default: scores.ps)\n"
            "  -p                : to produce point instead of triangles\n";
    cerr << "\n";
    exit (0);
}

void checkParams(Parameters& params)
{
    if ((params.size() < 1)
        ||
           !(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) {
        if (!strcmp(*currentArg, "-b")) { if (++currentArg != params.end()) boxes_filename = *currentArg; continue; }
        if (!strcmp(*currentArg, "-t")) { if (++currentArg != params.end()) triangles_filename = *currentArg; continue; }
        if (!strcmp(*currentArg, "-s")) { if (++currentArg != params.end()) subTitle = *currentArg; continue; }
        if (!strcmp(*currentArg, "-p")) { points = true; continue; }
        inputs.push_back(conform_string(*currentArg));
    }
    if (!subTitle.empty()) 
    {
    	subTitle.erase(subTitle.end()-1);
    	subTitle.erase(subTitle.begin());
    }
}


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

	// number of evaluated system
	int system_number = inputs.size();

	PsOutput graph_triangles(triangles_filename,triangles_graph_title, subTitle);
  	graph_triangles.triangles_print(system_number);

	PsOutput graph_boxes(boxes_filename,boxes_graph_title,subTitle, 70.0);
	graph_boxes.boxes_print(system_number);

	// define a number for each system
  	Natural system_no(0);
  	Real system_color(0.0);
 
	for (StringsCollection::const_iterator i = inputs.begin(); i != inputs.end(); ++i)
	{
		ifstream in(i->c_str(), ios::in);
		if (!in) msg.seriousError(string("Grapher"), string("Input file '")+*i+string("' not found !"));
		HtmlSystem system;
		HtmlResultats result;
		HtmlField::skipToTag(in, &system);
		HtmlField::skipToTag(in, &result);

	    if (points) // add the point to the triangles file
      		graph_triangles.add_point(result.precision(), 
                                	  result.decision(), 
                                	  result.nbCases(),
                                	  system.sys_name());
	    else // add the system triangle to the graph in the triangles file    
      		graph_triangles.add_triangle(result.precision(), 
                                	     result.decision(),
                                	     result.minPrecision(), 
                                   		 result.maxPrecision(),
                                   	     result.nbCases(),
                                   	     system_color,
                                         system.sys_name());

	    // add the system box to the graph in the boxes file
	    graph_boxes.add_box(result.subMinPrecision(), 
                            result.subAveragePrecision(),
                            result.averagePrecision(),
                            result.supAveragePrecision(), 
                            result.supMaxPrecision(),
                            result.nbCases(),
                            1-((system_no+1)/(Real)(system_number+1)),
                        	system.sys_name());
        
        in.close();
	    system_no++;
	    system_color=0.8*(system_no/(float) system_number);
	}
}



