/*****************************************************************************
 *
 *  confaudmp2lavcmencoderdialog.cpp -- Input the LibAVCodec MP2 audio parameters.
 *
 * Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *
 *****************************************************************************/

#include "../video/video_functions.h"

#include "confaudmp2lavcmencoderdialog.h"
#include "../encoder/encoder.h"
#include "macros.h"
#include <QtGui>

ConfAudMp2LavcMEncoderDialog::ConfAudMp2LavcMEncoderDialog(encoder_audio_settings_t *settings, 
	audio_t *track, QWidget *parent)
	: QDialog(parent)
{
	callerSettings = settings;
	audioSource = track;

	// Local copy allows to cancel changes
	encSettings = new encoder_audio_settings_t( *settings );

	setMinimumSize( 280, 360 );
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

	verticalLayout = new QVBoxLayout(this);

	// Global buttons
	buttonBox = new QDialogButtonBox(this);

	buttonBox->setOrientation(Qt::Horizontal);
	buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);

	// Build all tabs
	tabWidget = new QTabWidget(this);
	tabWidget->setMovable(false);

	mainTab = new QWidget();

	tabWidget->addTab(mainTab, QString());

	verticalLayout->addWidget(tabWidget);
	verticalLayout->addWidget(buttonBox);

	tabWidget->setCurrentIndex(0);

	// Main tab 
	mainTabVLayout = new QVBoxLayout(mainTab);
	mainTabVLayout->setSpacing(12);

	// - Bitrate
	bitrateGroupBox = new QGroupBox(mainTab);
	bitrateHLayout = new QHBoxLayout(bitrateGroupBox);
	bitrateComboBox = new QComboBox(bitrateGroupBox);
	bitrateHSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	bitrateHLayout->addWidget(bitrateComboBox);
	bitrateHLayout->addItem(bitrateHSpacer);

	mainTabVLayout->addWidget(bitrateGroupBox);

	// - Sampling rate
	srateGroupBox = new QGroupBox(mainTab);
	srateHLayout = new QHBoxLayout(srateGroupBox);
	srateComboBox = new QComboBox(srateGroupBox);
	srateHSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	srateHLayout->addWidget(srateComboBox);
	srateHLayout->addItem(srateHSpacer);

	mainTabVLayout->addWidget(srateGroupBox);

	// - Preset
	presetGroupBox = new QGroupBox(mainTab);
	presetVLayout = new QVBoxLayout(presetGroupBox);
	presetDvdCheckBox = new QCheckBox(presetGroupBox);
	presetSvcdCheckBox = new QCheckBox(presetGroupBox);
	presetVcdCheckBox = new QCheckBox(presetGroupBox);
	
	presetVLayout->addWidget(presetDvdCheckBox);
	presetVLayout->addWidget(presetSvcdCheckBox);
	presetVLayout->addWidget(presetVcdCheckBox);

	mainTabVLayout->addWidget(presetGroupBox);

	// - Spacer
	verticalSpacer = new QSpacerItem(20, 10, QSizePolicy::Minimum, QSizePolicy::Expanding);

	mainTabVLayout->addItem(verticalSpacer);

	// - Default settings
	defaultHLayout = new QHBoxLayout();
	defaultPushButton = new QPushButton(mainTab);
	defaultHSpacer = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

	defaultHLayout->addWidget(defaultPushButton);
	defaultHLayout->addItem(defaultHSpacer);

	mainTabVLayout->addLayout(defaultHLayout);

	// Captions
	setWindowTitle( tr("MPEG-1 Layer II audio encoder") );

	tabWidget->setTabText(tabWidget->indexOf(mainTab), tr("MP2 libavcodec settings") );

	bitrateGroupBox->setTitle( tr(" Bitrate ( kbps ) ") );
	
	srateGroupBox->setTitle( tr(" Sampling rate ") );

	presetGroupBox->setTitle( tr(" Standard MP2 audio presets ") );
	presetDvdCheckBox->setText( tr("DVD") );
	presetSvcdCheckBox->setText( tr("SVCD") );
	presetVcdCheckBox->setText( tr("VideoCD") );
	
	defaultPushButton->setText( tr("Default settings") );

	// Tool tips
	setToolTip();

	setAttribute(Qt::WA_AlwaysShowToolTips);

	populateBitrate();
	populateSampling();

	// Update controls from current settings before connecting signals
	updateControls();

	connect(bitrateComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectBitrate(int)));

	connect(srateComboBox, SIGNAL(activated(int)), 
		this, SLOT(selectSampling(int)));

	connect(presetDvdCheckBox, SIGNAL(clicked()), 
		this, SLOT(selectPresetDvd()));
	connect(presetSvcdCheckBox, SIGNAL(clicked()), 
		this, SLOT(selectPresetSvcd()));
	connect(presetVcdCheckBox, SIGNAL(clicked()), 
		this, SLOT(selectPresetVcd()));

	connect(defaultPushButton, SIGNAL(clicked()), 
		this, SLOT(defaultPushButtonClicked()));

	// Global buttons
	connect(buttonBox, SIGNAL(rejected()), this, SLOT(close()));
	connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
}

void ConfAudMp2LavcMEncoderDialog::accept()
{
	*callerSettings = *encSettings;

	QDialog::accept();
}

void ConfAudMp2LavcMEncoderDialog::updateControls()
{
	int idx;
	bool vcd, svcd, dvd;

	idx = bitrateComboBox->findData( encSettings->mp2.bitrate );

	if ( idx == -1 ) // should not happened
	{
		bitrateComboBox->setCurrentIndex( 0 );
		encSettings->mp2.bitrate = bitrateComboBox->itemData( 0 ).toInt();
	}
	else
	{
		bitrateComboBox->setCurrentIndex( idx );
	}

	idx = srateComboBox->findData( encSettings->mp2.srate );

	if ( idx == -1 ) // should not happened
	{
		srateComboBox->setCurrentIndex( 0 );
		encSettings->mp2.srate = srateComboBox->itemData( 0 ).toInt();
	}
	else
	{
		srateComboBox->setCurrentIndex( idx );
	}

	vcd = encSettings->mp2.bitrate == 224
			&& encSettings->mp2.vbr == 0
			&& encSettings->mp2.srate == 44100;

	svcd = ( (encSettings->mp2.bitrate == 224 && encSettings->mp2.vbr != 0) 
				|| encSettings->mp2.bitrate <= 384 )
			&& encSettings->mp2.srate == 44100;

	dvd = encSettings->mp2.bitrate <= 1536
			&& (encSettings->mp2.srate == 48000 || encSettings->mp2.srate == 96000);

	presetVcdCheckBox->setChecked(vcd);
	presetSvcdCheckBox->setChecked(svcd);
	presetDvdCheckBox->setChecked(dvd);
}

void ConfAudMp2LavcMEncoderDialog::populateBitrate()
{
	bitrateComboBox->clear();

	QMap<int, QString> bitrate;
	bitrate.insert( 32, tr("32 kbits/s") );
	bitrate.insert( 48, tr("48 kbits/s") );
	bitrate.insert( 56, tr("56 kbits/s") );
	bitrate.insert( 64, tr("64 kbits/s") );
	bitrate.insert( 80, tr("80 kbits/s") );
	bitrate.insert( 96, tr("96 kbits/s") );
	bitrate.insert( 112, tr("112 kbits/s") );
	bitrate.insert( 128, tr("128 kbits/s") );
	bitrate.insert( 160, tr("160 kbits/s") );
	bitrate.insert( 192, tr("192 kbits/s") );
	bitrate.insert( 224, tr("224 kbits/s") );
	bitrate.insert( 256, tr("256 kbits/s") );
	bitrate.insert( 320, tr("320 kbits/s") );
	bitrate.insert( 384, tr("384 kbits/s") );

	QMapIterator<int, QString> i(bitrate);
	while ( i.hasNext() )
	{
		i.next();

		bitrateComboBox->addItem( i.value(), i.key() );
	}

	int index = bitrateComboBox->findData( encSettings->mp2.bitrate );

	if ( index == -1 ) // should not happened
	{
		bitrateComboBox->setCurrentIndex( 0 );
		encSettings->mp2.bitrate = bitrateComboBox->itemData( 0 ).toInt();
	}
	else
	{
		bitrateComboBox->setCurrentIndex( index );
	}
}

void ConfAudMp2LavcMEncoderDialog::populateSampling()
{
	srateComboBox->clear();

	QMap<int, QString> sampling;
	sampling.insert( 0, tr("(input rate)") );
	sampling.insert( 44100, tr("44,100 Hz") );
	sampling.insert( 48000, tr("48,000 Hz") );

	QMapIterator<int, QString> i(sampling);
	while ( i.hasNext() )
	{
		i.next();

		srateComboBox->addItem( i.value(), i.key() );
	}

	int index = srateComboBox->findData( encSettings->mp2.srate );

	if ( index == -1 ) // should not happened
	{
		srateComboBox->setCurrentIndex( 0 );
		encSettings->mp2.srate = srateComboBox->itemData( 0 ).toInt();
	}
	else
	{
		srateComboBox->setCurrentIndex( index );
	}
}

void ConfAudMp2LavcMEncoderDialog::selectBitrate(const int index)
{
	// Do nothing if combobox is empty or reset
	if (index < 0)
	{
		return;
	}

	encSettings->mp2.bitrate = bitrateComboBox->itemData( index ).toInt();

	updateControls();
}

void ConfAudMp2LavcMEncoderDialog::selectSampling(const int index)
{
	// Do nothing if combobox is empty or reset
	if (index < 0)
	{
		return;
	}

	encSettings->mp2.srate = srateComboBox->itemData( index ).toInt();

	updateControls();
}

void ConfAudMp2LavcMEncoderDialog::selectPresetDvd()
{
	encSettings->mp2.dvd();

	updateControls();
}

void ConfAudMp2LavcMEncoderDialog::selectPresetSvcd()
{
	encSettings->mp2.svcd();

	updateControls();
}

void ConfAudMp2LavcMEncoderDialog::selectPresetVcd()
{
	encSettings->mp2.vcd();

	updateControls();
}

void ConfAudMp2LavcMEncoderDialog::defaultPushButtonClicked()
{
	encSettings->mp2.clear();

	updateControls();
}

void ConfAudMp2LavcMEncoderDialog::setToolTip()
{
	bitrateGroupBox->setToolTip( boxToolTip("br", tr("None"), "<32-384>",
						tr("Sets the audio bitrate in kbit/s. MP2 libavcodec only encodes at constant bit rate. "
"Birates defined in ISO/IEC 11172-3: 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 and 384 kbit/s.")) );
	
}

QString ConfAudMp2LavcMEncoderDialog::labelToolTip( const QString &defaults, const QString &details )
{
	QString strDefault( tr("Default: ") ); 

	return tr(	"<p><b>%1</b> %2</p>"
				"<p></p>"
				"<p>%3</p>" )
				.arg(Qt::escape(strDefault)).arg(Qt::escape(defaults))
				.arg(Qt::escape(details));
}

QString ConfAudMp2LavcMEncoderDialog::editToolTip( const QString &name, const QString &range )
{
	return tr("<b>%1</b> %2").arg( Qt::escape(name) ).arg( Qt::escape(range) );
}

QString ConfAudMp2LavcMEncoderDialog::boxToolTip( const QString &name, const QString &defaults, const QString &range, const QString &details )
{
	QString strDefault( tr("Default: ") ); 

	return tr(	"<p><b>%1</b> %2</p>"
				"<p></p>"
				"<p><b>%3</b> %4</p>"
				"<p></p>"
				"<p>%5</p>" )
				.arg(Qt::escape(name)).arg(Qt::escape(range))
				.arg(Qt::escape(strDefault)).arg(Qt::escape(defaults))
				.arg(Qt::escape(details));
}
