/***************************************************************************
 *   Copyright (C) 2008 by Andreas Silberstorff   *
 *   taki@Rivendell   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "kalvavideoplayer.h"

#include <solid/powermanagement.h>
#include <QtGui>
#include <QAction>
#include <QLabel>
#include <QGridLayout>
#include <QRect>
#include <QSize>
#include <QSizePolicy>
#include <QUrl>

#include <phonon/mediasource.h>
#include <phonon/mediaobject.h>
#include <phonon/volumeslider.h>
#include <phonon/seekslider.h>
#include <phonon/videoplayer.h>
#include <phonon/videowidget.h>
#include <phonon/backendcapabilities.h>

#include <klocale.h>
#include <kglobal.h>
#include <KPushButton>
#include <KDialog>
#include <KDebug>

namespace KalvaWidget {

KalvaVideoPlayer::KalvaVideoPlayer()
	: QWidget()
{
    init();
}

KalvaVideoPlayer::KalvaVideoPlayer ( QWidget* parent )
	: QWidget( parent )
{
    init();
}

void KalvaVideoPlayer::init()
{
        m_powerSaveCookie = 0;
	m_url = QUrl();
	createActions();
	createWidgets();
	layoutWidgets();
	
	player->videoWidget()->addAction( playerPlayAction );
	player->videoWidget()->addAction( playerPauseAction );
	player->videoWidget()->addAction( playerStopAction );
	player->videoWidget()->addAction( enterFullScreenAction );
	player->videoWidget()->setContextMenuPolicy( Qt::ActionsContextMenu );

	connect( player, SIGNAL( finished() ),
	   SLOT( playerFinished() ) );
	connect( fullScreenButton, SIGNAL( clicked() ),
	   enterFullScreenAction, SLOT( trigger() ) );
	connect( stopButton, SIGNAL( clicked() ),
	   playerStopAction, SLOT( trigger() ) );

	playerFinished();
	settingsChanged();
}

KalvaVideoPlayer::~KalvaVideoPlayer()
{
	delete player;
}

void KalvaVideoPlayer::createWidgets()
{
	int AddBarsScaleMode = 0;

	player = new Phonon::VideoPlayer ( Phonon::VideoCategory, this );
	player->mediaObject()->setTickInterval( 100 );
	player->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
	player->videoWidget()->setScaleMode( Phonon::VideoWidget::ScaleMode( AddBarsScaleMode ) );
	player->videoWidget()->setAspectRatio( Phonon::VideoWidget::AspectRatioAuto );

	volumeSlider  = new Phonon::VolumeSlider ( this );
	volumeSlider->setAudioOutput ( player->audioOutput() );
	volumeSlider->setOrientation( Qt::Vertical );

	seekSlider  = new Phonon::SeekSlider ( this );
	seekSlider->setMediaObject ( player->mediaObject() );

	playItem = KGuiItem( i18n("Play"), QString( "player_play" ), i18n( "Play the selected video file" ), i18n( "Play the selected video file" ) );
	pauseItem = KGuiItem( i18n("Pause"), QString( "player_pause" ), i18n( "Pause playback of video file" ), i18n( "Pause playback of video file" ) );
	playPauseButton = new KPushButton( playItem, this );

	stopItem = KGuiItem( i18n("Stop"), QString( "player_stop" ), i18n( "Stop playback of video file" ), i18n( "Stop playback of video file" ) );
	stopButton = new KPushButton( stopItem, this );

	KGuiItem fullScreenItem = KGuiItem( i18n("Fullscreen"), QString( "player_fullscreen" ), i18n( "Play video in fullscreen" ), i18n( "Play video in fullscreen" ) );
	fullScreenButton = new KPushButton( fullScreenItem, this );
	fullScreenButton->setCheckable( true );
	this->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
}

void KalvaVideoPlayer::layoutWidgets()
{
	QGridLayout* grid  = new QGridLayout ( this );
	grid->addWidget( volumeSlider, 0, 0, 1, 1 );
	grid->addWidget( player->videoWidget(), 0, 1, 1, 3 );
	grid->addWidget( seekSlider, 1, 1, 1, 3 );
	grid->addWidget( playPauseButton, 2, 1, 1, 1 );
	grid->addWidget( stopButton, 2, 2, 1, 1 );
	grid->addWidget( fullScreenButton, 2, 3, 1, 1 );

	grid->setColumnStretch( 1, 10 ),
	grid->setRowStretch( 0, 10 ),
	grid->setMargin( KDialog::spacingHint() );
	grid->setSpacing( KDialog::spacingHint() );
}

void KalvaVideoPlayer::createActions()
{
	playerPlayAction = new QAction( i18n( "Play" ), this );
	playerPlayAction->setShortcut( i18n( "SPACE" ) );
	playerPlayAction->setStatusTip( i18n( "Play selected video") );
	connect( playerPlayAction, SIGNAL( triggered() ),
		this, SLOT( play() ) );

	playerPauseAction = new QAction( i18n( "Pause" ), this );
	playerPauseAction->setStatusTip( i18n( "Pause video") );
	playerPauseAction->setShortcut( i18n( "SPACE" ) );
	connect( playerPauseAction, SIGNAL( triggered() ),
		this, SLOT( pause() ) );

	playerStopAction = new QAction( i18n( "Stop" ), this );
	playerStopAction->setStatusTip( i18n( "Stop video") );
	playerStopAction->setShortcut( i18n( "Q" ) );
	connect( playerStopAction, SIGNAL( triggered() ),
		this, SLOT( stop() ) );

	enterFullScreenAction = new QAction( i18n( "fullscreen" ), this );
	enterFullScreenAction->setCheckable( true );
	enterFullScreenAction->setStatusTip( i18n( "Toggle fullscreen mode" ) );
	enterFullScreenAction->setShortcut( i18n( "F11" ) );
	connect( enterFullScreenAction, SIGNAL( triggered( bool ) ),
		this, SLOT( enterFullScreen( bool) ) );
}

// ==================================
// public slots
// ==================================

void KalvaVideoPlayer::play()
{
	kDebug() << "play()";
	if ( m_url.isEmpty() ) {
		kDebug() << "m_url is empty";
		emit playButtonPressed();
	} else {
		kDebug() << "m_url is not empty, calling player->play()";
		player->play();
		playerPlayes();
	}
}

void KalvaVideoPlayer::pause()
{
	kDebug() << "pause()";
	player->pause();
	playerPaused();
	emit pauseButtonPressed();
}

void KalvaVideoPlayer::stop()
{
	kDebug() << "stop()";
	m_url = QUrl();
	player->stop();
	playerFinished();
	emit stopButtonPressed();
}

void KalvaVideoPlayer::enterFullScreen( bool enabled )
{
	kDebug() << "enterFullScreen(" << enabled << ")";

	if ( enabled )	{
		player->videoWidget()->enterFullScreen();
	} else {
		player->videoWidget()->exitFullScreen();
		fullScreenButton->setChecked( false );
	}
}


void KalvaVideoPlayer::load( const QUrl & url )
{
	kDebug() << "kalvavideoplayer::load(" << url << ")";
	m_url = url;
	player->stop();
	player->load( m_url );
	playerFinished();
}


void KalvaVideoPlayer::settingsChanged()
{
	kDebug() << "settingsChanged()";
}

// ==================================
// private slots
// ==================================

void KalvaVideoPlayer::playerPlayes()
{
	kDebug() << "playerPlayes()";
	playerPlayAction->setDisabled( true );
	playerPauseAction->setEnabled( true );
        m_powerSaveCookie = Solid::PowerManagement::beginSuppressingSleep( i18n( "A video is playing." ) );
	disconnect( playPauseButton, SIGNAL( clicked() ),
		    playerPlayAction, SLOT( trigger() ) );
	connect( playPauseButton, SIGNAL( clicked() ),
		 playerPauseAction, SLOT( trigger() ) );
	playPauseButton->setGuiItem( pauseItem );
	player->removeAction( playerPlayAction );
	player->addAction( playerPauseAction );
}

void KalvaVideoPlayer::playerPaused()
{
	kDebug() << "playerPaused()";
	playerPlayAction->setEnabled( true );
	playerPauseAction->setDisabled( true );
        Solid::PowerManagement::stopSuppressingSleep( m_powerSaveCookie );
	disconnect( playPauseButton, SIGNAL( clicked() ),
		    playerPauseAction, SLOT( trigger() ) );
	connect( playPauseButton, SIGNAL( clicked() ),
		 playerPlayAction, SLOT( trigger() ) );
	playPauseButton->setGuiItem( playItem );
	player->removeAction( playerPauseAction );
	player->addAction( playerPlayAction );
}

void KalvaVideoPlayer::playerFinished()
{
	kDebug() << "playerFinished()";
	playerPlayAction->setEnabled( true );
	playerPauseAction->setDisabled( true );
        Solid::PowerManagement::stopSuppressingSleep( m_powerSaveCookie );
	disconnect( playPauseButton, SIGNAL( clicked() ),
		    playerPauseAction, SLOT( trigger() ) );
	connect( playPauseButton, SIGNAL( clicked() ),
		 playerPlayAction, SLOT( trigger() ) );
	playPauseButton->setGuiItem( playItem );
	player->removeAction( playerPauseAction );
	player->addAction( playerPlayAction );
}

}

#include "kalvavideoplayer.moc"
