/*****************************************************************************
 *
 *  encoderpreviewdialog.cpp -- Wrap a single JobManagerWidget in a window.
 * 
 *  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 "macros.h"
#include "encoderpreviewdialog.h"
#include "mediainfodatadialog.h"
#include "infoboxdialog.h"
#include <QtGui>
#include <QFontMetrics>
#include <QFile>
#include <QDir>

EncoderPreviewDialog::EncoderPreviewDialog(const QString &title, JobManagerWidget *jobManagerWidget, 
	PlayerDialog::Preferences *playerPreferences, QWidget *refConsole)
	: isEncoding(false)
{
	this->jobManagerWidget = jobManagerWidget;
	this->playerPreferences = playerPreferences;
	this->mainConsole = refConsole;  // reference for centering

	QString str = title.isEmpty() ? tr("Encoding preview") : title;
	setWindowTitle( str );

	deleteOutputBox = new QCheckBox();
	deleteOutputBox->setToolTip(tr("Delete output preview file when finished"));
	
	controlButton = new QPushButton();
	previewButton = new QPushButton(tr("Preview"));
	previewButton->setToolTip(tr("Play preview"));
	infoButton = new QPushButton(tr("Informations"));
	infoButton->setToolTip(tr("Show preview media informations"));

	QHBoxLayout *outputLayout = new QHBoxLayout();
	outputLayout->addWidget(deleteOutputBox);
	outputLayout->addStretch(4);

	QHBoxLayout *controlLayout = new QHBoxLayout();
#ifdef Q_OS_MAC
	controlLayout->setSpacing(10);
#endif
	controlLayout->addStretch(4);
	controlLayout->addWidget(previewButton);
	controlLayout->addWidget(infoButton);
	controlLayout->addWidget(controlButton);

	QVBoxLayout *mainLayout = new QVBoxLayout();

	mainLayout->addItem(outputLayout);
	mainLayout->addWidget(jobManagerWidget);
	mainLayout->addItem(controlLayout);
	setLayout(mainLayout);
	
	connect(controlButton, SIGNAL(clicked()), this, SLOT(close()));
	connect(previewButton, SIGNAL(clicked()), this, SLOT(playPreview()));
	connect(infoButton, SIGNAL(clicked()), this, SLOT(showPreviewInfo()));

	setWindowModality(Qt::ApplicationModal);

#ifdef WIN32
	jobManagerWidget->setMinimumWidth( 100 * jobManagerWidget->fontMetrics().averageCharWidth() );
#else
	jobManagerWidget->setMinimumWidth( 70 * jobManagerWidget->fontMetrics().averageCharWidth() );
#endif

	center();
}

void EncoderPreviewDialog::setEncoding()
{
	controlButton->setText(tr("Stop encoding preview"));
	controlButton->setToolTip(tr("Abort current encoding process"));

	isEncoding = true;

	previewButton->setEnabled(false);
	infoButton->setEnabled(false);
}

void EncoderPreviewDialog::setEncodingDone()
{
	controlButton->setText(tr("Close"));
	controlButton->setToolTip(tr("Close encoding preview window"));

	isEncoding = false;

	previewButton->setEnabled(true);
	infoButton->setEnabled(true);
}

void EncoderPreviewDialog::setOutput(const QString &str)
{
	output = QDir::toNativeSeparators(str);
}

void EncoderPreviewDialog::playPreview()
{
	PlayerDialog preview( tr("%1 - Preview").arg(APPTITLE), playerPreferences, 0);

#ifdef Q_OS_MAC64
	preview.show();
	QCoreApplication::processEvents();
	preview.hide();
#endif

	preview.setCurrentFilePath(output);
	preview.exec();
}

void EncoderPreviewDialog::showPreviewInfo()
{
	interfacemediainfo_data_t mediaInfoData;

	// Use a PlayerDialog object to get MediaInfo data
	PlayerDialog playerDlg( "No display", playerPreferences, 0);	
	playerDlg.setInfoMode(true);
	playerDlg.setCurrentFilePath(output);
	mediaInfoData = playerDlg.readMediaInfoData();

	MediaInfoDataDialog dlg(&mediaInfoData, this);
	dlg.exec();		
}

void EncoderPreviewDialog::closeEvent(QCloseEvent *ev)
{
	if (isEncoding) abortEncodingPreview();

	if (deleteOutputBox->isChecked() && QFile::exists(output)) QFile::remove(output);

	ev->accept();
}

void EncoderPreviewDialog::showEvent(QShowEvent *ev)
{
	deleteOutputBox->setText(tr("Output: %1").arg(output));
	deleteOutputBox->setChecked(true);

	adjustSize();

	ev->accept();
}

void EncoderPreviewDialog::center()
{
	if (mainConsole)
	{
		QDesktopWidget desktop;
		QRect dkrect = desktop.availableGeometry(desktop.screenNumber(mainConsole));

		int	x = mainConsole->pos().x() + (mainConsole->size().width() - width()) / 2;
		int	y = mainConsole->pos().y() + mainConsole->size().height() / 2;
		
		move( x, y );
	}
}

void EncoderPreviewDialog::abortEncodingPreview()
{
	emit abortedByUser();
	
	jobManagerWidget->abortProcesses();

	// Inform user
	InfoBoxDialog infoBox(this);

	infoBox.setSmall();
	infoBox.setText(tr("Encoding preview has been aborted."));
	infoBox.exec();
}

void EncoderPreviewDialog::setNoConsoleButton(const bool no)
{
	showConsoleButton = !no;

	jobManagerWidget->setNoConsoleButton(no);
}
