// Dump the mapping table.

#include <fstream>
#include <iostream>
#include "globaldef.h"
#include "messages_manager.h"
#include "user_configuration.h"
#include "tags_mapping_table.h"

#ifdef _USE_NAMESPACES
    namespace Else { 
#endif // _USE_NAMESPACES

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

UserConfiguration    uconfig;
UserConfiguration    rconfig;
TagsMappingTable     tagsTable;

conform_string uconfigFile, rconfigFile;         // Name of the configuration file.

bool rel(false);
bool all(false);
bool dump(false);

void dumpHelp()
{
    cerr << "Description:  (Debug part)\n"
            "  Dump the mapping table of a participant to standard output,\n"
            "  or map a <reftag;systag> from stdin to stdout.\n"
            "\n"
            "Usage: map <pconfig> <rconfig> {switches}\n"
            "  <pconfig>   : configuration file of the evaluated participant\n"
            "  <rconfig>   : reference's configuration file\n"
            "Switches:\n"
            "  -rel        : compute a relative mapping.\n"
            "  -dumpall    : dump all informations.\n" 
            "  -dumpinv    : dump only System <- Pivot table.\n";
  	msg.switchesFormat(cerr);            
    cerr << "\n\n";
    exit (0);
}

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

    uconfigFile = params.front(); params.erase(params.begin());
    rconfigFile = params.front(); params.erase(params.begin());
   	msg.checkParams(params);
    for (Parameters::const_iterator currentArg = params.begin(); currentArg != params.end(); ++currentArg) {
        if (!strcmp(*currentArg, "-dumpall")) { all = true; dump = true; continue; }
        if (!strcmp(*currentArg, "-dumpinv")) { all = false; dump = true; continue; }
        if (!strcmp(*currentArg, "-rel")) {rel = true; continue; }
    }
}

int main(int argc, char* argv[]) {
    Parameters params(argc, argv);
    checkParams(params);
	conform_string reftag, systag;
    if (!rconfig.readFrom(rconfigFile, UserConfiguration::Reference, false))
        msg.seriousError("Evaluater", "Reference configuration file is not readeable, or it has too serious error(s)");
    if (!uconfig.readFrom(uconfigFile, UserConfiguration::System, false))
        msg.seriousError("Evaluater", "System configuration file is not readeable, or it has too serious error(s)");
	if (! tagsTable.readReference(rconfig, rconfig.mapName, rconfig.mapMode))
        msg.seriousError("Evaluater", "Mapping table file is not readeable, or it has too serious error(s)");
	if (! tagsTable.readSystem(uconfig, uconfig.mapName, uconfig.mapMode))
        msg.seriousError("Evaluater", "Mapping table file is not readeable, or it has too serious error(s)");

	if (dump)
	{
		if (all) tagsTable.dump();
		else tagsTable.dumpInv();
	} else {
		cout << "Please get: <reftag> <systag>  (0 to quit)\n";
		while (true) {
			if (! (cin >> reftag) || (reftag == "0")) exit(0);
			if (! (cin >> systag) || (systag == "0")) exit(0);
			cout << ">> Ref: '";
			if (rel)
			{
				cout << tagsTable.mapPivotToSys(BasicTag(reftag)) << "'  Sys: '"
				     << AlternatedTagsSet(systag);
			}
			else
			{
				cout << tagsTable.mapPivotToRef(BasicTag(reftag)) << "'  Sys: '"
				     << tagsTable.mapSysToRef(BasicTag(systag));
			}
			cout << "'\n";
		}
	}
}

