/***************************************************************************
 *   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 "pluginfactory.h"
#include "settings.h"
#include <klocale.h>
#include <kglobal.h>
#include <KDialog>
#include <KDebug>

#include <QTabWidget>
#include <QStackedWidget>

#include <QPluginLoader>

namespace KalvaPluginSystem {

PluginFactory::PluginFactory(
		 QTabWidget *manager, QStackedWidget *mainViewStack, QObject *plugin )
	: QObject(),
	  m_managerTabWidget( manager ),
	  m_mainViewStackedWidget( mainViewStack ),
	  m_managerWidget( 0 ),
	  m_mainViewWidget( 0 ),
	  m_enabled( false )
{
//	kDebug() << "Constructing PluginFactory";
	m_plugin = qobject_cast<KalvaPlugin::PluginInterface *>(plugin);
//	kDebug() << "casted PluginInterface";
	if (m_plugin == 0) {
		kDebug() << "PluginInterface missing!!!";
	}
	kDebug() << "PluginInterface stored.";
	m_perspective = qobject_cast<KalvaPlugin::PerspectiveInterface *>(plugin);
//	kDebug() << "casted PerspectiveInterface";
	if (m_perspective != 0) {
		kDebug() << "PerspectivInterface stored.";
	}
	m_device = qobject_cast<KalvaPlugin::DeviceInterface *>(plugin);
//	kDebug() << "casted DeviceInterface";
	if (m_device != 0) {
		kDebug() << "DeviceInterface stored.";
	}
	m_scheduler = qobject_cast<KalvaPlugin::SchedulerInterface *>(plugin);
	if (m_scheduler != 0) {
		kDebug() << "SchedulerInterface stored.";
	}
//	kDebug() << "casted ChannellistProviderInterface";
	m_channellistprovider = qobject_cast<KalvaPlugin::ChannellistProviderInterface *>(plugin);
	if (m_channellistprovider != 0) {
		kDebug() << "ChannellistProviderInterface stored.";
	}
}

PluginFactory::~PluginFactory()
{
}

void PluginFactory::addToConfigDialog( KConfigDialog *dialog )
{
	// only add page(s) once to a config dialog
	if ( dialog != m_dialog )
	{
		m_dialog = dialog;
		m_plugin->addToConfigDialog( dialog );
	}
}

void PluginFactory::settingsChanged()
{
	if (m_enabled) {
		KalvaData::KalvaGeneralSettings* settings = new KalvaData::KalvaGeneralSettings();
		settings->setCollectionRoot(Settings::collection_root());
		m_plugin->setKalvaGeneralSettings( settings );
		m_plugin->settingsChanged();
	}
}

void PluginFactory::setEnabled(bool enabled)
{
	if (enabled)
	{
		enable();
	} else
	{
		disable();
	}
	m_enabled = enabled;
}

void PluginFactory::enable()
{
 	if ( m_perspective != 0 )
 	{
		enablePerspective();
 	}
}

void PluginFactory::disable()
{
 	if ( m_perspective != 0 )
 	{
 		disablePerspective();
 	}
}

void PluginFactory::enablePerspective()
{
	kDebug() << "enablePerspective()";
	if ( m_managerWidget == 0 )
	{
		kDebug() << "constructing perspective widgets";
		m_managerWidget = m_perspective->createManager( m_managerTabWidget );
		kDebug() << "managerwidget constructed";
		m_mainViewWidget = m_perspective->createMainView( m_mainViewStackedWidget );
		kDebug() << "mainviewwidget constructed";
		connect( m_managerTabWidget, SIGNAL( currentChanged( int ) ),
			 this,      SLOT( perspectiveSelected( int ) ) );
		kDebug() << "managerwidget connected";
	}
	m_managerTabWidget->addTab( m_managerWidget, m_plugin->title() );
	kDebug() << "managerwidget added";
	m_mainViewStackedWidget->addWidget( m_mainViewWidget );
	kDebug() << "mainviewwidget added";
}

void PluginFactory::disablePerspective()
{
	kDebug() << "disPerspective()";
  	if ( m_managerWidget != 0 )
  	{
 		m_managerTabWidget->removeTab( m_managerTabWidget->indexOf( m_managerWidget ) );
 		m_mainViewStackedWidget->removeWidget( m_mainViewWidget );
  	}
}

void PluginFactory::perspectiveSelected( int perspectiveId )
{
	kDebug() << "perspectiveSelected(" << perspectiveId << ")";
	if ( m_managerTabWidget->currentWidget() == m_managerWidget )
	{
		kDebug() << "activate mainViewWidget";
		int viewId = m_mainViewStackedWidget->indexOf( m_mainViewWidget );
		m_mainViewStackedWidget->setCurrentIndex( viewId );
		kDebug() << "mainViewWidget activated";
	}
}

}