/*****************************************************************************
 *
 *  encodercapabilitydialog.cpp -- Show encoder capabilities.
 *
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is part of the Gui4 module of the DVDx 4 application.
 *
 *  GNU Lesser General Public License Usage
 *  This file may be used under the terms of the GNU Lesser General
 *  Public License version 2.1 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.LGPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU Lesser General Public License version 2.1 requirements
 *  will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 *
 *  GNU General Public License Usage
 *  Alternatively, this file may be used under the terms of the GNU
 *  General Public License version 3.0 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.GPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU General Public License version 3.0 requirements will be
 *  met: http://www.gnu.org/copyleft/gpl.html.
 *
 *****************************************************************************/

#include "encodercapabilitydialog.h"
#include "../video/video_functions.h"
#include <QLayout>
#include <QListWidget>
#include <QGroupBox>
#include <QFontMetrics>

EncoderCapabilityDialog::EncoderCapabilityDialog(const QString &name, encoder_engine_capability_t *capability, QWidget *parent)
	: QDialog(parent)
{
	setWindowTitle( tr("%1 engine capabilities").arg( name ) );
	
	int i, Nvideo, Naudio, Nsubtitle, Nmuxer;
	
	// Video
	QListWidget *videoList = new QListWidget(this);

	Nvideo = capability->video->count();

	QMapIterator<QString, QString> v(*capability->video);
	i = 0;
	while (v.hasNext()) 
	{
		v.next();
		availableVideo << new QListWidgetItem( " - " + v.key() );
		availableVideo[i]->setToolTip( v.value() );
		videoList->addItem( availableVideo[i] );
		i++;
	}

	videoList->sortItems(Qt::AscendingOrder);

	QVBoxLayout *videoTable = new QVBoxLayout;
	videoTable->addWidget(videoList);

	QGroupBox *videoGroup = new QGroupBox(tr("Available video codecs"));
	videoGroup->setLayout(videoTable);

	QVBoxLayout *videoLayout = new QVBoxLayout;
	videoLayout->addWidget(videoGroup);

	// Video libs
	if (capability->libVideo != 0)
	{
		QStringList libs = capability->libVideo->keys();
		int countLibs = libs.count();

		for (int i = 0; i < countLibs; i++)
		{
			QString lib = libs[i];

			QListWidget *codecList = new QListWidget(this);

			QStringListIterator v(capability->libVideo->value(lib));
			while (v.hasNext()) 
			{
				codecList->addItem( new QListWidgetItem( " - " + v.next() ) );
			}

			codecList->sortItems(Qt::AscendingOrder);

			QFontMetrics fm = codecList->fontMetrics();
			double lineHeight = fm.height() * 1.20;
			codecList->setMaximumHeight(( qMin(20 - videoList->count(), codecList->count()) +2) * lineHeight);

			QVBoxLayout *codecTable = new QVBoxLayout;
			codecTable->addWidget(codecList);

			QGroupBox *codecGroup = new QGroupBox(tr("Supported library - %1").arg(lib));
			codecGroup->setLayout(codecTable);

			videoLayout->addWidget(codecGroup);
		}
	}

	videoLayout->addStretch();

	// Audio
	QListWidget *audioList = new QListWidget(this);

	Naudio = capability->audio->count();
	
	QMapIterator<QString, QString> a(*capability->audio);
	i = 0;
	while (a.hasNext()) 
	{
		a.next();
		availableAudio << new QListWidgetItem( " - " + a.key() );
		availableAudio[i]->setToolTip( a.value() );
		audioList->addItem( availableAudio[i] );
		i++;
	}

	audioList->sortItems(Qt::AscendingOrder);

	QVBoxLayout *audioTable = new QVBoxLayout;
	audioTable->addWidget(audioList);

	QGroupBox *audioGroup = new QGroupBox(tr("Available audio codecs"));
	audioGroup->setLayout(audioTable);

	QVBoxLayout *audioLayout = new QVBoxLayout;
	audioLayout->addWidget(audioGroup);

	// Audio libs
	if (capability->libAudio != 0)
	{
		QStringList libs = capability->libAudio->keys();
		int countLibs = libs.count();

		for (int i = 0; i < countLibs; i++)
		{
			QString lib = libs[i];

			QListWidget *codecList = new QListWidget(this);

			QStringListIterator v(capability->libAudio->value(lib));
			while (v.hasNext()) 
			{
				codecList->addItem( new QListWidgetItem( " - " + v.next() ) );
			}

			codecList->sortItems(Qt::AscendingOrder);

			QFontMetrics fm = codecList->fontMetrics();
			double lineHeight = fm.height() * 1.20;
			codecList->setMaximumHeight(( qMin(20 - audioList->count(), codecList->count()) +2) * lineHeight);

			QVBoxLayout *codecTable = new QVBoxLayout;
			codecTable->addWidget(codecList);

			QGroupBox *codecGroup = new QGroupBox(tr("Supported library - %1").arg(lib));
			codecGroup->setLayout(codecTable);

			audioLayout->addWidget(codecGroup);
		}
	}

	audioLayout->addStretch();

	// Subtitle
	QListWidget *subtitleList = new QListWidget(this);

	Nsubtitle = capability->subtitle->count();
	
	QMapIterator<QString, QString> s(*capability->subtitle);
	i = 0;
	while (s.hasNext()) 
	{
		s.next();
		availableSubtitle << new QListWidgetItem( " - " + s.key() );
		availableSubtitle[i]->setToolTip( s.value() );
		subtitleList->addItem( availableSubtitle[i] );
		i++;
	}

	subtitleList->sortItems(Qt::AscendingOrder);

	QVBoxLayout *subtitleTable = new QVBoxLayout;
	subtitleTable->addWidget(subtitleList);

	QGroupBox *subtitleGroup = new QGroupBox(tr("Available subtitle codecs"));
	subtitleGroup->setLayout(subtitleTable);

	QVBoxLayout *subtitleLayout = new QVBoxLayout;
	subtitleLayout->addWidget(subtitleGroup);

	// Subtitle libs
	if (capability->libSubtitle != 0)
	{
		QStringList libs = capability->libSubtitle->keys();
		int countLibs = libs.count();

		for (int i = 0; i < countLibs; i++)
		{
			QString lib = libs[i];

			QListWidget *codecList = new QListWidget(this);

			QStringListIterator v(capability->libSubtitle->value(lib));
			while (v.hasNext()) 
			{
				codecList->addItem( new QListWidgetItem( " - " + v.next() ) );
			}

			codecList->sortItems(Qt::AscendingOrder);

			QFontMetrics fm = codecList->fontMetrics();
			double lineHeight = fm.height() * 1.20;
			codecList->setMaximumHeight(( qMin(20 - subtitleList->count(), codecList->count()) +2) * lineHeight);

			QVBoxLayout *codecTable = new QVBoxLayout;
			codecTable->addWidget(codecList);

			QGroupBox *codecGroup = new QGroupBox(tr("Supported library - %1").arg(lib));
			codecGroup->setLayout(codecTable);

			subtitleLayout->addWidget(codecGroup);
		}
	}

	subtitleLayout->addStretch();

	// Muxer
	QListWidget *muxerList = new QListWidget(this);

	Nmuxer = capability->muxer->count();

	QMapIterator<QString, QString> m(*capability->muxer);
	i = 0;
	while (m.hasNext()) 
	{
		m.next();
		availableMuxer << new QListWidgetItem( " - " + m.key() );
		availableMuxer[i]->setToolTip( m.value() );
		muxerList->addItem( availableMuxer[i] );
		i++;
	}

	muxerList->sortItems(Qt::AscendingOrder);

	QVBoxLayout *muxerTable = new QVBoxLayout;
	muxerTable->addWidget(muxerList);

	QGroupBox *muxerGroup = new QGroupBox(tr("Available file formats"));
	muxerGroup->setLayout(muxerTable);

	QVBoxLayout *muxerLayout = new QVBoxLayout;
	muxerLayout->addWidget(muxerGroup);

	// Muxer libs
	if (capability->libMuxer != 0)
	{
		QStringList libs = capability->libMuxer->keys();
		int countLibs = libs.count();

		for (int i = 0; i < countLibs; i++)
		{
			QString lib = libs[i];

			QListWidget *formatList = new QListWidget(this);

			QStringListIterator v(capability->libMuxer->value(lib));
			while (v.hasNext()) 
			{
				formatList->addItem( new QListWidgetItem( " - " + v.next() ) );
			}

			formatList->sortItems(Qt::AscendingOrder);

			QFontMetrics fm = formatList->fontMetrics();
			double lineHeight = fm.height() * 1.20;
			formatList->setMaximumHeight(( qMin(20 - muxerList->count(), formatList->count()) +2) * lineHeight);

			QVBoxLayout *formatTable = new QVBoxLayout;
			formatTable->addWidget(formatList);

			QGroupBox *formatGroup = new QGroupBox(tr("Supported library - %1").arg(lib));
			formatGroup->setLayout(formatTable);

			muxerLayout->addWidget(formatGroup);
		}
	}

	muxerLayout->addStretch();

	// Buttons
	buttonBox = new QDialogButtonBox(this);
	buttonBox->setFocusPolicy(Qt::StrongFocus);
	buttonBox->setOrientation(Qt::Horizontal);
	buttonBox->setStandardButtons(QDialogButtonBox::Ok);
	buttonBox->setCenterButtons(true);

	// Tabs
	tabWidget = new QTabWidget(this);
	tabWidget->setMovable(false);

	videoTab = new QWidget();
	audioTab = new QWidget();
	subtitleTab = new QWidget();
	muxerTab = new QWidget();

	if ( Nmuxer )
	{
		tabWidget->addTab(muxerTab, QString(tr("Format")));
	}

	if ( Nvideo || (!Naudio && !Nsubtitle && !Nmuxer) )
	{
		tabWidget->addTab(videoTab, QString(tr("Video")));
	}

	if ( Naudio )
	{
		tabWidget->addTab(audioTab, QString(tr("Audio")));
	}

	if ( Nsubtitle )
	{
		tabWidget->addTab(subtitleTab, QString(tr("Subtitle")));
	}

	tabWidget->setCurrentIndex(0);

	QVBoxLayout *tabVLayout = new QVBoxLayout;
	tabVLayout->addWidget(tabWidget);
	tabVLayout->addWidget(buttonBox);

	videoTab->setLayout(videoLayout);
	audioTab->setLayout(audioLayout);
	subtitleTab->setLayout(subtitleLayout);
	muxerTab->setLayout(muxerLayout);

	setLayout(tabVLayout);

	connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
	connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

	setAttribute(Qt::WA_AlwaysShowToolTips);
	setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
	setMinimumWidth( 260 );
}

