/*****************************************************************************
 *
 *  playerstatusdialog.cpp -- Show player status.
 *
 *  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 "playerstatusdialog.h"
#include "radiobox.h"
#include <QLayout>
#include <QLabel>
#include <QLineEdit>
#include <QGroupBox>

PlayerStatusDialog::PlayerStatusDialog(player_engine_status_t *status, QWidget *parent)
	: QDialog(parent)
{
	setWindowTitle( tr("Player status") );

	setAttribute(Qt::WA_AlwaysShowToolTips);
	setMinimumWidth( 260 );
	
	QLabel *backendLabel = new QLabel( tr("Backend") );
	QLineEdit *backendLineEdit = new QLineEdit( status->player );
	backendLineEdit->setToolTip( status->player );
	backendLineEdit->setFocusPolicy(Qt::NoFocus);

	QLabel *versionLabel = new QLabel( tr("Version") );
	QLineEdit *versionLineEdit = new QLineEdit( status->version );
	versionLineEdit->setToolTip( status->version );
	versionLineEdit->setFocusPolicy(Qt::NoFocus);

#if defined( WIN32 )
	QLabel *openGlLabel = new QLabel( tr("OpenGL") );
	QLineEdit *openGlLineEdit = new QLineEdit();
	if (status->state == Video::Status::EngineOk && status->supportsOpenGL )
	{
		openGlLineEdit->setText( tr("supported") );
	}
	else if ( status->state == Video::Status::EngineOk && !status->supportsOpenGL )
	{
		openGlLineEdit->setText( tr("not supported") );
		openGlLineEdit->setToolTip( tr(	"<p>Windows switches color space when starting playback, "
										"producing annoying (but hurtless) black screen flashes.</p>") );
	}
	openGlLineEdit->setFocusPolicy(Qt::NoFocus);
#endif

	QGridLayout *signatureLayout = new QGridLayout;
	signatureLayout->addWidget(backendLabel,	0, 0 );
	signatureLayout->addWidget(backendLineEdit,	0, 1 );
	signatureLayout->addWidget(versionLabel,	1, 0 );
	signatureLayout->addWidget(versionLineEdit,	1, 1 );
#if defined( WIN32 )
	signatureLayout->addWidget(openGlLabel,		2, 0 );
	signatureLayout->addWidget(openGlLineEdit,	2, 1 );
#endif

    QGroupBox *stateGroupBox = new QGroupBox(this);
	stateGroupBox->setTitle(tr("Backend state"));

	RadioBox *idleButton = new RadioBox( stateGroupBox );
	RadioBox *notreadyButton = new RadioBox( stateGroupBox );
	RadioBox *readyButton = new RadioBox( stateGroupBox );

	idleButton->setText( tr("Not defined") );
	notreadyButton->setText( tr("Not ready") );
	readyButton->setText( tr("Ready") );

	idleButton->setToolTip( tr("Player backend is not defined (check binary path).") );
	notreadyButton->setToolTip( tr("Failed to read player backend status.") );
	readyButton->setToolTip( tr("Player backend is ready.") );

	switch (status->state)
	{
	case Video::Status::EngineOk:
		readyButton->setChecked(true);
		break;
	case Video::Status::IdleEngine:
		idleButton->setChecked(true);
		break;
	case Video::Status::ParseFail:
		notreadyButton->setChecked(true);
		break;
	case Video::Status::RawStatus:
		notreadyButton->setChecked(true);
		break;
	default:
		notreadyButton->setChecked(true);
	}

	QVBoxLayout *stateVLayout = new QVBoxLayout(stateGroupBox);
    stateVLayout->addWidget(readyButton);
    stateVLayout->addWidget(notreadyButton);
    stateVLayout->addWidget(idleButton);

	QHBoxLayout *stateHLayout = new QHBoxLayout;
	stateHLayout->addWidget(stateGroupBox);
	stateHLayout->setAlignment( Qt::AlignCenter );

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

	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addLayout(signatureLayout);
	mainLayout->addSpacing( 10 );
	mainLayout->addLayout(stateHLayout);
	mainLayout->addSpacing( 10 );
	mainLayout->addWidget(buttonBox);

	setLayout(mainLayout);

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

	buttonBox->setFocus();  // remove RadioBox focus on Linux
}
