/*
*/

#include <gst/gst.h>
#include <gst-phonon/gst-phonon.h>
#include "mediaobject.h"
#include <QTimer>
#include <kdebug.h>

namespace Phonon
{
namespace Gst
{
MediaObject::MediaObject( QObject* parent )
	: AbstractMediaProducer( parent )
	, m_aboutToFinishNotEmitted( true )
{
	//kDebug( 604 ) << k_funcinfo << endl;
	object = gpho_media_object_new();
}

MediaObject::~MediaObject()
{
	//kDebug( 604 ) << k_funcinfo << endl;
	gst_object_unref(object);
}

KUrl MediaObject::url() const
{
	//kDebug( 604 ) << k_funcinfo << endl;
	return m_url;
}

qint64 MediaObject::totalTime() const
{
	//kDebug( 604 ) << k_funcinfo << endl;
	return 1000*60*3; // 3 minutes
}

qint32 MediaObject::aboutToFinishTime() const
{
	//kDebug( 604 ) << k_funcinfo << endl;
	return m_aboutToFinishTime;
}

void MediaObject::setUrl( const KUrl& url )
{
	//kDebug( 604 ) << k_funcinfo << endl;
    AbstractMediaProducer::stop();
    m_aboutToFinishNotEmitted = true;
	m_url = url;
	emit length( totalTime() );
	QMultiMap<QString, QString> metaData;
	metaData.insert( "TITLE", "Fake video" );
	metaData.insert( "ARTIST", "Matthias Kretz" );
	emit metaDataChanged( metaData );
}

void MediaObject::openMedia(Phonon::MediaObject::Media media, const QString &)
{
	switch (media)
	{
		case Phonon::MediaObject::None:
			setUrl(KUrl());
			break;

		default:
			break;
	}
}

void MediaObject::setAboutToFinishTime( qint32 newAboutToFinishTime )
{
	//kDebug( 604 ) << k_funcinfo << endl;
	m_aboutToFinishTime = newAboutToFinishTime;
	if( currentTime() < totalTime() - m_aboutToFinishTime ) // not about to finish
		m_aboutToFinishNotEmitted = true;
}

void MediaObject::play()
{
	//kDebug( 604 ) << k_funcinfo << endl;
	AbstractMediaProducer::play();
}

void MediaObject::pause()
{
	//kDebug( 604 ) << k_funcinfo << endl;
    if (state() == Phonon::PlayingState || state() == Phonon::BufferingState) {
        AbstractMediaProducer::pause();
    }
}

void MediaObject::stop()
{
    //kDebug(604) << k_funcinfo << endl;
    if (state() == Phonon::PlayingState || state() == Phonon::BufferingState || state() == Phonon::PausedState) {
        AbstractMediaProducer::stop();
        m_aboutToFinishNotEmitted = true;
    }
}

void MediaObject::seek( qint64 time )
{
	//kDebug( 604 ) << k_funcinfo << endl;
	AbstractMediaProducer::seek( time );

	if( currentTime() < totalTime() - m_aboutToFinishTime ) // not about to finish
		m_aboutToFinishNotEmitted = true;
}

#if 0
void MediaObject::emitTick()
{
	AbstractMediaProducer::emitTick();
	if( currentTime() >= totalTime() - m_aboutToFinishTime ) // about to finish
	{
		if( m_aboutToFinishNotEmitted )
		{
			m_aboutToFinishNotEmitted = false;
			emit aboutToFinish( totalTime() - currentTime() );
		}
	}
	if( currentTime() >= totalTime() ) // finished
	{
		stop();
		emit finished();
	}
}
#endif

}}

#include "mediaobject.moc"
// vim: sw=4 ts=4
