#include <QtGui>
#include "mainfrm.h"

void mainfrm::player_percent_change()
{
	if ( mplayerProcess.state() == QProcess::Starting || mplayerProcess.state() == QProcess::Running )
	{
		mplayer_timer->start(100);
	}
	else
	{
		//player_open_video(0);
	}
	QString mycom2;
	mycom2 = "seek " + QString::number(player_progress->value()) + " 1\n";
	mplayerProcess.write( mycom2.toUtf8() );
}

void mainfrm::player_start_percent_change()
{
	mplayer_timer->stop();
}

void mainfrm::player_open_video(QString pathvideo)
{
	if ( mplayerProcess.state() == QProcess::Starting || mplayerProcess.state() == QProcess::Running )
	{
		mplayerProcess.kill();
		mplayerProcess.waitForFinished(1000);	
		mplayer_timer->stop();
	}
       	QStringList args;
        args << "-slave";
       	args << "-osdlevel" << "0";
       	args << "-quiet";
	args << "-zoom";
	//args << "-vo" << "gl:nomanyfmts";
	//args << "-vo" << "xshm";
	//args << "-dr";
	//args << "-double";
	#ifdef Q_WS_WIN
		args << "-wid" << QString::number(reinterpret_cast<unsigned int>(frame_tv->winId()));
		args << "-vo" << "directx:noaccel";
	#else
		args << "-wid" << QString::number(frame_tv->winId());
		//args << "-vo" << "gl";
	#endif
	args << pathvideo;
	args << "-vf" << "pp=lb";
        args << "-keepaspect";
	args << "-loop" << "0";
      	mplayerProcess.setProcessChannelMode(QProcess::MergedChannels);
      	mplayerProcess.start("mplayer", args);
      	if(!mplayerProcess.waitForStarted(3000))
      	{
		QMessageBox::information(this, "Manslide", tr("Impossible de demarrer mplayer. Est t il installe correctement dans /usr/bin ?"));
	}
	else
	{
		if ( ispreview != 6 )
		{
			player_progress->setEnabled(true);
			mplayer_timer->start(10);
			isplaying = true;
			bt_play_tv->setIcon(QIcon(":/new/prefix2/Interface/Theme/player_stop.png"));
		}
	}
}

void mainfrm::player_end(int, QProcess::ExitStatus)
{
	player_progress->setDisabled(true);
	frame_player->hide();
	mplayer_timer->stop();
	isplaying = false;
	if ( mplayerProcess.state() == QProcess::Starting || mplayerProcess.state() == QProcess::Running )
	{
		mplayerProcess.kill();	
	}
	menubar->setEnabled(true);
	toolBar->setEnabled(true);
	mplayerProcess.waitForFinished(2000);
	pntmygl->bcle_render = 1;
}

void mainfrm::player_current_percent()
{
	mplayerProcess.write("get_percent_pos \n");
}

void mainfrm::player_play_video()
{
	if ( mplayerProcess.state() == QProcess::Starting || mplayerProcess.state() == QProcess::Running )
	{
		if ( isplaying == true )
		{
			player_progress->setDisabled(true);
			mplayerProcess.write("pause \n");
			mplayer_timer->stop();
			isplaying = false;
			bt_play_tv->setIcon(QIcon(":/new/prefix2/Interface/Theme/player_play.png"));
		}
		else
		{
			player_progress->setEnabled(true);
			mplayerProcess.write("pause \n");
			mplayer_timer->start(10);
			isplaying = true;
			bt_play_tv->setIcon(QIcon(":/new/prefix2/Interface/Theme/player_stop.png"));
		}
	}
}

void mainfrm::player_read_stdout()
{
	if (mplayerProcess.canReadLine())
        {
        	QByteArray buffer(mplayerProcess.readLine());
                if(buffer.startsWith("ANS_PERCENT_POSITION"))
                {
                	buffer.remove(0, 21);
                        buffer.replace(QByteArray("'"), QByteArray(""));
                        buffer.replace(QByteArray(" "), QByteArray(""));
                        buffer.replace(QByteArray("\n"), QByteArray(""));
                        buffer.replace(QByteArray("\r"), QByteArray(""));
			player_progress->setValue(buffer.toInt());
                }
	}
}
