#include <MakeMovieCommand.h>
#include <PlotCommand.h>


MakeMovieCommand :: MakeMovieCommand() :
	name("MakeMovie")
{
}


string MakeMovieCommand :: execute(Projection *projection, Profile *profile)
{
	Gnuplot * gplt = projection->projection();
	PlotCommand * plotCmd = new PlotCommand;

	system ("mkdir -p mov");
	cout << "Creating images for the movie mov/proj" << projection->ID() << ".avi, Please wait..." << endl;

	for (int j = 0; j < profile->nSteps(); j++)
	{
		gplt->cmd ("set terminal png");
		gplt->cmd ("set output 'mov/proj%d_step%04d.png'", projection->ID(), j);
		plotCmd->execute(projection, profile, j);
	}

	gplt->cmd ("CMD = 'mencoder mf://mov/proj%d_*.png -mf fps=25:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o mov/proj%d.avi'; system(CMD);", projection->ID(), projection->ID());
	gplt->cmd ("system ('rm -f mov/*.png')");

	gplt->cmd ("set term %s noraise title 'Projection %d'", projection->terminal().c_str(), projection->ID());
	gplt->cmd ("unset output");

	system ("sleep 10");
	cout << "Movie mov/proj" << projection->ID() << ".avi created." << endl;

	return "";

}


