/***************************************************************************
 *   Copyright (C) 2007 by Sébastien Laoût                                 *
 *   slaout@linux62.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "fullscreen.h"
#include "coverdisplay.h"
#include "amarokapi.h"
#include "playerinformation.h"

#include <qdesktopwidget.h>
#include <klocale.h>
#include <kapplication.h>
#include <kiconloader.h>
#include <kglobal.h>
#include <qimage.h>
#include <kimageeffect.h>
#include <kstandarddirs.h>
#include <kmessagebox.h>

FullScreen::FullScreen(QWidget *parent, const char *name)
 : QWidget(parent, name, Qt::WNoAutoErase)
{
	QDesktopWidget desktop;
	resize(desktop.screenGeometry(this).size());

	setCaption(i18n("Kirocker Music Display"));
	setIcon(kapp->iconLoader()->loadIcon("kirocker", KIcon::Desktop, 48, KIcon::DefaultState));

	QImage backgroundImage(KGlobal::dirs()->findResource("data", "kirocker/images/colorwaves.jpg"));
	backgroundImage = backgroundImage.smoothScale(size());
	KImageEffect::blend(Qt::black, backgroundImage, 0.8);

	QPixmap backgroundPixmap;
	backgroundPixmap.convertFromImage(backgroundImage);

	m_coverDisplay = new CoverDisplay(this);
	m_coverDisplay->setGeometry(rect());
	m_coverDisplay->setBackgroundOrigin(QWidget::ParentOrigin);
	m_coverDisplay->setPaletteBackgroundColor(Qt::black);
	m_coverDisplay->setPaletteBackgroundPixmap(backgroundPixmap);
	m_coverDisplay->setFullScreen();
	m_coverDisplay->show();

	connect( m_coverDisplay, SIGNAL(closeAsked()), this, SLOT(close()) );
}

FullScreen::~FullScreen()
{
}

void FullScreen::keyPressEvent(QKeyEvent *event)
{
	QWidget::keyPressEvent(event);

	PlayerInformation *infos = PlayerInformation::instance();
	switch (event->key()) {
		case Qt::Key_Space:
		case Qt::Key_Return:   AmarokApi::playPause();     break;
		case Qt::Key_P:
		case Qt::Key_PageUp:   AmarokApi::previousTrack(); break;
		case Qt::Key_N:
		case Qt::Key_PageDown: AmarokApi::nextTrack();     break;
		case Qt::Key_Left:     infos->seekRelative(-10);   break;
		case Qt::Key_Right:    infos->seekRelative(10);    break;
		case Qt::Key_Up:       AmarokApi::volumeUp();      break;
		case Qt::Key_Down:     AmarokApi::volumeDown();    break;
		case Qt::Key_F1:       clickStar(1);               break;
		case Qt::Key_F2:       clickStar(2);               break;
		case Qt::Key_F3:       clickStar(3);               break;
		case Qt::Key_F4:       clickStar(4);               break;
		case Qt::Key_F5:       clickStar(5);               break;
		case Qt::Key_Q:
		case Qt::Key_Escape:   close();                    break;
		default:
			if (!event->text().isEmpty()) // Do not catch Alt, Ctrl... and other modifiers (no display on Alt+Tab...)
				showKeysInformation();
			break;
	}
}

void FullScreen::clickStar(int star)
{
	PlayerInformation *infos = PlayerInformation::instance();

	if (infos->status() == PlayerInformation::Stopped)
		return;

	int rating = star * 2;
	if (infos->rating() == 1)
		rating = 0;
	else if (infos->rating() == rating)
		rating -= 1;
	infos->changeRating(rating);
}

void FullScreen::showKeysInformation()
{
	KMessageBox::information(
		this,
		"<p>" + i18n("Keyboard shortcuts available in full screen mode:") + "</p><table>"
		"<tr><th>" + i18n("Space or Enter:")         + "</th><td>" + i18n("Play/pause")         + "</td></tr>"
		"<tr><th>" + i18n("PageUp/PageDown or P/N:") + "</th><td>" + i18n("Change track")       + "</td></tr>"
		"<tr><th>" + i18n("Left/Right:")             + "</th><td>" + i18n("Seek in track")      + "</td></tr>"
		"<tr><th>" + i18n("Up/Down:")                + "</th><td>" + i18n("Change volume")      + "</td></tr>"
		"<tr><th>" + i18n("F1/F2/F3/F4/F5:")         + "</th><td>" + i18n("Change star rating") + "</td></tr>"
		"<tr><th>" + i18n("Escape or Q:")            + "</th><td>" + i18n("Close full screen")  + "</td></tr>"
		"</table>",
		i18n("%1 - %2").arg("Keyboard Shortcuts", i18n("Kirocker Music Display")),
		/*dontShowAgainName=*/QString(),
		KMessageBox::Notify | KMessageBox::PlainCaption
	);
}

void FullScreen::contextMenuEvent(QContextMenuEvent *event)
{
	QWidget::contextMenuEvent(event);

	showKeysInformation();
}

void FullScreen::windowActivationChange(bool oldActive)
{
	// Disable screensaver only when this fullscreen window is enabled:
	AmarokApi::enableScreensaver(!isActiveWindow());
	AmarokApi::enableOSD(!isActiveWindow());
	m_coverDisplay->hideControls();

	QWidget::windowActivationChange(oldActive);
}

#include "fullscreen.moc"
