/***************************************************************************
 *   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 "tvperspective.h"
#include "generalsettingspage.h"

#include "settings.h"
#include <PluginManager>

#include <QtGui>
#include <QString>
#include <QUrl>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QSizePolicy>

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

namespace TvPerspective {

Plugin::Plugin()
{
	playProcess = 0L;
}

Plugin::~Plugin()
{
	delete m_channellist;
	delete m_videoplayer;
	delete m_recorder;
}

QString Plugin::title()
{
	return i18n( "TV" );
}

QString Plugin::whatsThis()
{
	return i18n( "The TV perspective provides a channellist to choose the station to tune to and a video player for watching the TV programm." );
}

QString Plugin::toolTip()
{
	return i18n( "The TV perspective provides a channellist to choose the station to tune to and a video player for watching the TV programm." );
}


QWidget* Plugin::createManager( QWidget *parent )
{
	m_channellist = new KalvaWidget::KalvaChannellist( parent );
	QGridLayout* grid  = new QGridLayout( parent );
	grid->addWidget( m_channellist );
	grid->setMargin( KDialog::spacingHint() );
	grid->setSpacing( KDialog::spacingHint() );

	return m_channellist;
}

QWidget* Plugin::createMainView( QWidget *parent )
{
        QTabWidget* tabWidget = new QTabWidget( parent );
	createVideoPlayer( tabWidget );
	createRecorder( tabWidget );

	QGridLayout* grid  = new QGridLayout( parent );
	grid->addWidget( tabWidget );
	grid->setMargin(KDialog::spacingHint());
	grid->setSpacing(KDialog::spacingHint());
	setupConnections();
	return tabWidget;
}

void Plugin::createVideoPlayer(QTabWidget *parent)
{
	QWidget* box = new QWidget(parent);
	QLabel* stationLabel = new QLabel( i18n("station") );
	QLabel* channelLabel = new QLabel( i18n("channel") );
	QLabel* frequenceLabel = new QLabel( i18n("frequence") );
	m_stationEntry = new QLineEdit();
	m_channelEntry = new QLineEdit();
	m_frequenceEntry = new QLineEdit();
	m_videoplayer = new KalvaWidget::KalvaVideoPlayer();
     
	QHBoxLayout* channelLayout = new QHBoxLayout();
	channelLayout->addWidget( stationLabel );
	channelLayout->addWidget( m_stationEntry );
	channelLayout->addWidget( channelLabel );
	channelLayout->addWidget( m_channelEntry );
	channelLayout->addWidget( frequenceLabel );
	channelLayout->addWidget( m_frequenceEntry );
	channelLayout->setMargin( KDialog::spacingHint() );
	channelLayout->setSpacing( KDialog::spacingHint() );

	QGridLayout* playerLayout = new QGridLayout();
	playerLayout->addWidget( m_videoplayer, 1, Qt::AlignJustify );
	playerLayout->setMargin( KDialog::spacingHint() );
	playerLayout->setSpacing( KDialog::spacingHint() );

	QVBoxLayout* mainLayout = new QVBoxLayout(box);
	mainLayout->addLayout( channelLayout, 10 );
	mainLayout->addLayout( playerLayout, 80 );
	mainLayout->setMargin( KDialog::spacingHint() );
	mainLayout->setSpacing( KDialog::spacingHint() );

	parent->addTab( box, i18n("Player") );
}

void Plugin::createRecorder(QTabWidget *parent)
{
	m_recorder = new KalvaWidget::KalvaRecorder( parent );

	QGridLayout* grid  = new QGridLayout( parent );
	grid->addWidget( m_recorder );
	grid->setMargin(KDialog::spacingHint());
	grid->setSpacing(KDialog::spacingHint());

	parent->addTab( m_recorder, i18n("Recorder") );
}

void Plugin::setupConnections()
{
	connect( m_channellist, SIGNAL( selectionChanged( KalvaData::Channel * ) ),
		 this, SLOT( setChannel( KalvaData::Channel * ) ) );
	connect( m_channellist, SIGNAL( selectionChanged( KalvaData::Channel * ) ),
		m_recorder, SLOT( setChannel( KalvaData::Channel * ) ) );
	connect( m_videoplayer, SIGNAL( playButtonPressed() ),
		 this, SLOT( play() ) );
	connect( m_videoplayer, SIGNAL( pauseButtonPressed() ),
		 this, SLOT( pause() ) );
	connect( m_videoplayer, SIGNAL( stopButtonPressed() ),
		 this, SLOT( stop() ) );
	connect( m_recorder, SIGNAL( recordButtonSelected() ),
		 this, SLOT( record() ) );
}

void Plugin::setChannel(KalvaData::Channel* channel)
{
	kDebug() << "channelSelected()";
	kDebug() << "channel->station = " << channel->station();
	kDebug() << "channel->channel = " << channel->channel();
	kDebug() << "channel->frequence = " << channel->frequence();
	m_stationEntry->setText( channel->station() );
	m_channelEntry->setText( channel->channel() );
	m_frequenceEntry->setText( channel->frequence() );
	if (m_device != 0L) {
		m_device->setChannel (channel );
	}
	if (playProcess != 0L) {
		stop();
		play();
	}
}

void Plugin::settingsChanged()
{
	kDebug() << "settingsChanged()";
	QString titel = TvPerspectiveSettings::devicedriver();
	m_device = KalvaPluginSystem::PluginManager::instance().device( titel );
	m_videoplayer->settingsChanged();

	m_channellist->clear();
	QString channellistProviderName = TvPerspectiveSettings::channellistprovider();
	if (channellistProviderName == 0L) {
		kDebug() << "channellistProviderName not set";
		return;
	}
	kDebug() << "channellistProvider = " << channellistProviderName;
	KalvaPlugin::ChannellistProviderInterface* channellistProvider =
		KalvaPluginSystem::PluginManager::instance().channellistprovider(channellistProviderName);

	if (channellistProvider == 0L) {
		kDebug() << "channellistProvider not found";
		return;
	}
	QList<KalvaData::Channel*> list = channellistProvider->getChannellist();
	m_channellist->setChannellist(list);
}

void Plugin::addToConfigDialog( KConfigDialog *dialog )
{
	kDebug() << "addToConfigDialog()" << endl;

	GeneralSettingsPage* generalSettings = new GeneralSettingsPage();
	dialog->addPage(generalSettings,
		TvPerspectiveSettings::self(),
		i18n("TV settings"),
		"tvperspective_generalsetting");
	connect(dialog, SIGNAL(settingsChanged(const QString&)), generalSettings, SLOT(loadSettings()));
}

void Plugin::record()
{
	kDebug() << "record()";
	if (m_device != 0L) {
		kDebug() << "calling device->createRecordCommand()";
		QString titel = m_recorder->titel();
		QTime duration = m_recorder->duration();
		KalvaData::KalvaScriptCommand* cmd = m_device->createRecordCommand( titel, duration );
		if (cmd->test()) {
			// TODO implement scheduling
			cmd->run();
		}
	}
}

void Plugin::play()
{
	kDebug() << "play()";
	if (m_device != 0L) {
		kDebug() << "calling device->createPlayCommand()";
		KalvaData::KalvaScriptCommand* cmd = m_device->createPlayCommand();
		if (cmd->test()) {
			if (playProcess != 0L) {
				playProcess->kill();
				delete playProcess;
				playProcess = 0L;
			}
			playProcess = cmd->run();
			QString fifoFileName = cmd->url();
			m_videoplayer->load( QUrl("file://" + fifoFileName) );
			m_videoplayer->play();
		}
	}
}

void Plugin::pause()
{
	kDebug() << "pause()";
}

void Plugin::stop()
{
	kDebug() << "stop()";
	if (playProcess != 0L) {
		playProcess->kill();
		delete playProcess;
		playProcess = 0L;
	}
}

}

Q_EXPORT_PLUGIN2( kalva_tv_perspective, TvPerspective::Plugin )

#include "tvperspective.moc"
