/***************************************************************************
 *   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 "lib/pluginsystem/pluginsettingspage.h"
#include "lib/pluginsystem/pluginmanager.h"
#include "lib/pluginsystem/pluginfactory.h"
#include "lib/interfaces/plugininterface.h"
#include "lib/interfaces/perspectiveinterface.h"
#include "lib/interfaces/deviceinterface.h"
#include "lib/interfaces/schedulerinterface.h"
#include "lib/interfaces/channellistproviderinterface.h"

#include <QList>
#include <QLabel>
#include <QGridLayout>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QHeaderView>
#include <QVariant>

#include <KLocale>
#include <KDebug>
#include <KConfigDialog>

// TODO store active state of plugins
// TODO documentation

namespace KalvaPluginSystem {

PluginSettingsPage::PluginSettingsPage(KConfigDialog *dialog) : QWidget(),
		m_dialog(dialog)
{
	setupIcons();
	createWidgets();
	layoutWidgets();

	findPlugins();
}


PluginSettingsPage::~PluginSettingsPage()
{
}

void PluginSettingsPage::createWidgets()
{
	m_label = new QLabel();
	m_label->setText( i18n( "Kalva plugins" ) );

	m_toolTip = i18n( "Doulbleclick to toggle activation." );

	m_treeWidget = new QTreeWidget();
	m_treeWidget->setAlternatingRowColors( false );
	m_treeWidget->setSelectionMode( QAbstractItemView::SingleSelection );
	m_treeWidget->setColumnCount( 1 );
	m_treeWidget->header()->hide();
	m_treeWidget->setWordWrap( true );

	m_perspectivesItem = new QTreeWidgetItem( m_treeWidget );
	m_perspectivesItem->setText( 0, "Perspectives" );
	m_perspectivesItem->setIcon( 0, m_interfaceIcon );
	m_perspectivesItem->setData( 0, Qt::UserRole, QVariant( -1 ) );
	m_treeWidget->setItemExpanded( m_perspectivesItem, true );

	m_devicesItem = new QTreeWidgetItem( m_treeWidget );
	m_devicesItem->setText( 0, "Devices" );
	m_devicesItem->setIcon( 0, m_interfaceIcon );
	m_devicesItem->setData( 0, Qt::UserRole, QVariant( -1 ) );
	m_treeWidget->setItemExpanded( m_devicesItem, true );

	m_schedulersItem = new QTreeWidgetItem( m_treeWidget );
	m_schedulersItem->setText( 0, "Schedulers" );
	m_schedulersItem->setIcon( 0, m_interfaceIcon );
	m_schedulersItem->setData( 0, Qt::UserRole, QVariant( -1 ) );
	m_treeWidget->setItemExpanded( m_schedulersItem, true );

	m_channellistprovidersItem = new QTreeWidgetItem( m_treeWidget );
	m_channellistprovidersItem->setText( 0, "Channellist providers" );
	m_channellistprovidersItem->setIcon( 0, m_interfaceIcon );
	m_channellistprovidersItem->setData( 0, Qt::UserRole, QVariant( -1 ) );
	m_treeWidget->setItemExpanded( m_channellistprovidersItem, true );

	connect ( m_treeWidget, SIGNAL( itemDoubleClicked ( QTreeWidgetItem *, int ) ),
		  this, SLOT( itemDoubleClicked ( QTreeWidgetItem *, int ) ) );
}

void PluginSettingsPage::layoutWidgets()
{
	QGridLayout *mainLayout = new QGridLayout( this );
	mainLayout->setColumnStretch( 0, 1 );
	mainLayout->setColumnStretch( 2, 1 );
	mainLayout->addWidget( m_label, 0, 0, 1, 3 );
	mainLayout->addWidget( m_treeWidget, 1, 0, 1, 3 );
}

void PluginSettingsPage::setupIcons()
{
	m_interfaceIcon.addPixmap( style()->standardPixmap( QStyle::SP_DirOpenIcon ),
                             QIcon::Normal, QIcon::On );
	m_interfaceIcon.addPixmap( style()->standardPixmap( QStyle::SP_DirClosedIcon ),
                             QIcon::Normal, QIcon::Off );
	m_featureIcon.addPixmap( style()->standardPixmap( QStyle::SP_FileIcon ) );

	m_interfaceIcon.addPixmap( style()->standardPixmap( QStyle::SP_DirOpenIcon ),
                             QIcon::Normal, QIcon::On );
	m_interfaceIcon.addPixmap( style()->standardPixmap( QStyle::SP_DirClosedIcon ),
                             QIcon::Normal, QIcon::Off );
	m_featureIcon.addPixmap( style()->standardPixmap( QStyle::SP_FileIcon ) );
}

void PluginSettingsPage::findPlugins()
{
	kDebug() << "findPlugins()";
	PluginManager &manager = PluginManager::instance();
	kDebug() << "manager found";
	QList<PluginFactory *> factories = manager.factories();
	kDebug() << "factories recieved";

        for (int factoryID = 0; factoryID < factories.size(); ++factoryID)
	{
		kDebug() << "factoryID = " << factoryID;
		PluginFactory* factory = factories.at(factoryID);
		kDebug() << "factory found at ID " << factoryID;
		if (factory->perspective() != 0) {
			populateTreeWidget( factory, m_perspectivesItem, factoryID );
		}
		if (factory->device() != 0) {
			populateTreeWidget( factory, m_devicesItem, factoryID );
		}
 		if (factory->scheduler() != 0) {
			populateTreeWidget( factory, m_schedulersItem, factoryID );
 		}
 		if (factory->channellistprovider() != 0) {
			populateTreeWidget( factory, m_channellistprovidersItem, factoryID );
 		}
	}

}

void PluginSettingsPage::populateTreeWidget( PluginFactory* factory, QTreeWidgetItem* item, int factoryID )
{
	kDebug() << "PluginSettingsPage::populateTreeWidget()";
	KalvaPlugin::PluginInterface* interface = factory->plugin();

	QTreeWidgetItem *pluginItem = new QTreeWidgetItem( item );
	pluginItem->setText( 0, interface->title() );
	pluginItem->setIcon( 0, m_featureIcon );
	pluginItem->setToolTip( 0, m_toolTip );
	pluginItem->setWhatsThis( 0, interface->whatsThis() );

	QFont boldFont = pluginItem->font( 0 );
	if (factory->isEnabled())
	{
		boldFont.setBold( true );
	} else
	{
		boldFont.setBold( false );
	}
	pluginItem->setFont( 0, boldFont );
	pluginItem->setData( 0, Qt::UserRole, QVariant( factoryID ) );
	m_treeWidget->setItemExpanded( pluginItem, true );
}


void PluginSettingsPage::itemDoubleClicked ( QTreeWidgetItem* item, int column )
{
	kDebug() << "PerspectiveSettingsPage::itemDoubleClicked(" << column << ")";
	PluginManager &manager = PluginManager::instance();
	QList<PluginFactory *> factories = manager.factories();

	QVariant variant = item->data( 0, Qt::UserRole );
	kDebug() << "variant = " << variant;

 	int factoryID = variant.value<int>();
 	kDebug() << "factoryID = " << factoryID;

	if ( factoryID == -1 )
	{
		return;
	}
	if ( factoryID > factories.size() )
	{
		return;
	}

	PluginFactory *factory = factories.at(factoryID);
	kDebug() << "factory = " << factory;

	bool enabled = factory->isEnabled();
	kDebug() << "enabled = " << enabled;

	// toggle enabled
	factory->setEnabled( (! enabled) );
	kDebug() << " toggle ";

	QFont boldFont = item->font( 0 );

	enabled = factory->isEnabled();
	if (enabled)
	{
		boldFont.setBold( true );
		factory->addToConfigDialog( m_dialog );
	} else
	{
		boldFont.setBold( false );
	}
	item->setFont( 0, boldFont );
}

}

