/*****************************************************************************
 *
 *  playerdialog.cpp -- Wrap a PlayerWidget 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 "../video/video_constants.h"
#include "playerdialog.h"
#include "mediainfodatadialog.h"
#include "infoboxdialog.h"
#include <QLayout>
#include <QDesktopWidget>
#include <QFontMetrics>
#include <QRect>
#include <QFileDialog>
#include <QDir>
#include <QDebug>
#include <QApplication>
#include <QResizeEvent>
#include <QStatusBar>

PlayerDialog::PlayerDialog(const QString &title, Preferences *prefs, QWidget *parent)
	: QDialog(parent), 
	playerWidget(0), infoMode(false)
{
	playerTitle = title.isEmpty() ? tr("Player") : title;
	preferences = *prefs;

	setWindowTitle( playerTitle );

	playerWidget = new PlayerWidget(0,
		preferences.mplayerPath, preferences.mediainfoPath, preferences.ffprobePath,
		preferences.inputChannel, preferences.outputChannel, 
		preferences.errorChannel, preferences.logChannel, preferences.verbosity, 
		&preferences.playerPreferences, this);

	playerWidget->setNoChoiceInList(false);
	
	QVBoxLayout *mainLayout = new QVBoxLayout(this);

	mainLayout->addWidget(playerWidget);

#ifdef Q_OS_MAC
	QStatusBar statusBar(this);
	setSizePolicy( QSizePolicy::Fixed , QSizePolicy::Fixed );
	statusBar.setSizeGripEnabled(true);
#endif

	center();
}

void PlayerDialog::center()
{
	if (preferences.referenceConsole)
	{
		QDesktopWidget desktop;
		QRect dkrect = desktop.availableGeometry(desktop.screenNumber(preferences.referenceConsole));

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

void PlayerDialog::setCurrentFilePath(const QString &filePath)
{	
	if ( playerWidget->setInput(filePath, Video::Media::File) )
	{
		currentFile = filePath;
		QString fileName = QFileInfo(filePath).fileName();
		setWindowTitle(fileName.isEmpty()? playerTitle 
			: tr("%1 - %2").arg(playerTitle).arg(fileName));

		if (!infoMode) playerWidget->playRequest();
	}
	else
	{
		currentFile.clear();
		setWindowTitle( playerTitle );
	}
}

void PlayerDialog::setInfoMode(const bool yes)
{
	infoMode = yes;
}
