#include <string>
#include <PlotCommand.h>

using namespace std;


PlotCommand :: PlotCommand() :
	name("Plot")
{
}


string PlotCommand :: execute(Projection *projection, Profile *profile)
{
	return execute (projection, profile, profile->index());
}


string PlotCommand :: execute(Projection *projection, Profile *profile, int index_)
{
	string command, withTitle, compName, index;
	Gnuplot * gplt = projection->projection();
	stringstream lineStyle, ii;


	if ( profile->decoration() )
		withTitle = "";
	else
		withTitle = "no";

	ii << index_;
	index = ii.str();

	compName = projection->compToLabel(1);
	command  = "plot '" + profile->fileName() + "' index " + index;
	command += " using (" + projection->compToProj(0) + "):(" +  projection->compToProj(1) + ")";
	command += " " + withTitle + "title '" + compName + "' w l ls 1";

	for (int j = 2; j < projection->nComps(); j++)
	{
		lineStyle << j;
		compName = projection->compToLabel(j);
		command += ",\\\n    '' index " + index;
		command += " using (" + projection->compToProj(0) + "):(" +  projection->compToProj(j) + ")";
		command += " " + withTitle + "title '" + compName + "' w l ls " + lineStyle.str();
		lineStyle.str(string());

	}

	// add particular projection commands
	for (int j=0; j <projection->nCmd(); j++)
		command += projection->cmd(j)->execute(projection, profile);

	// DEBUG
	if ( profile->debug() ) cout << "Command: " << command << endl;

	//gplt->cmd (command.c_str());
	return command;
}


	/* Example of a gnuplot command:
		plot 'data.dat' using 1:2      title 'data 1', \
     		''         u     1:3      t     'data 2', \
     		''         u     1:4      t     'data 3', \
     		''         u 1:($2+$3+$4) t     'sum of datas'
	*/


