/* -*- mode: c++; tab-width: 4; c-basic-offset: 4 -*- */
/*
 * Copyright (C) 2005 Atmark <atmarkat _AT_ msn _DOT_ com>
 *                    AGAWA Koji <i _AT_ atty _DOT_ jp>
 *
 * 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 <qfont.h>
#include <qpalette.h>
#include <qcolor.h>
#include <qlabel.h>
#include <qlcdnumber.h>
#include <qlayout.h>
#include <qevent.h>
#include <qframe.h>
#include <qimage.h>

#include <unistd.h>

#include "playinfo.h"

PlayInfo::PlayInfo(const Configuration &conf, QWidget *parent, const char *name)
	:	QWidget(parent, name),
		config(conf)
{
	// Title
	titleLabel = new QLabel(tr("Title"), this);
	title = new QLabel(tr("Unknown"), this);
	
	// Artist
	artistLabel = new QLabel(tr("Artist"), this);
	artist = new QLabel(tr("Unknown"), this);
	
	// Album
	albumLabel = new QLabel(tr("Album"), this);
	album = new QLabel(tr("Unknown"), this);

	titleLabel->setMinimumWidth(1);
	title->setMinimumWidth(1);
	artistLabel->setMinimumWidth(1);
	artist->setMinimumWidth(1);
	albumLabel->setMinimumWidth(1);
	album->setMinimumWidth(1);
	
	// Number
	currentNumber = new QLCDNumber(5, this);
	currentNumber->display(0);
	currentNumber->setSegmentStyle(QLCDNumber::Filled);
	currentNumber->setFrameStyle(QFrame::NoFrame);
	
	numberSeparator = new QLabel("/", this);
	
	totalNumber = new QLCDNumber(5, this);
	totalNumber->display(0);
	totalNumber->setSegmentStyle(QLCDNumber::Filled);
	totalNumber->setFrameStyle(QFrame::NoFrame);
	
	numberLayout = new QHBoxLayout();
	numberLayout->addWidget(currentNumber);
	numberLayout->addWidget(numberSeparator);
	numberLayout->addWidget(totalNumber);
	
	infoLayout = new QVBoxLayout();
	infoLayout->setMargin(5);
	infoLayout->setResizeMode(QLayout::Fixed);
	infoLayout->addWidget(titleLabel);
	infoLayout->addWidget(title);
	infoLayout->addWidget(artistLabel);
	infoLayout->addWidget(artist);
	infoLayout->addWidget(albumLabel);
	infoLayout->addWidget(album);
	infoLayout->addLayout(numberLayout);
	
	coverArtProvider = new CoverArtProvider(parent);
	coverArt = new CoverArt(this);
	
	screenLayout = new QBoxLayout(this, QBoxLayout::LeftToRight);
	screenLayout->addWidget(coverArt);
	screenLayout->addLayout(infoLayout);
	
	CheckOrientation();
}

void PlayInfo::resizeEvent(QResizeEvent *e)
{
	CheckOrientation();
}

void PlayInfo::CheckOrientation()
{
	if (width() * 0.75 > height() || !config.changePlayInfoLayoutInPortaitMode)
	{
		// landscape mode...
		if (screenLayout->direction() != QBoxLayout::LeftToRight)
			screenLayout->setDirection(QBoxLayout::LeftToRight);

		coverArt->setMinimumSize(width() / 2, 64);
	}
	else
	{
		// portrait mode...
		if (screenLayout->direction() != QBoxLayout::TopToBottom)
			screenLayout->setDirection(QBoxLayout::TopToBottom);

		coverArt->setMinimumSize(64, height() / 2);	
	}
}

void PlayInfo::setFont(const QFont &font)
{
//	QFont font(config.font, config.fontSize);
	
	// Title
	title->setFont(font);
	
	// Artist
	artist->setFont(font);
	
	// Album
	album->setFont(font);
	
	// Counter
	// currentNumber->setFont(font);
	numberSeparator->setFont(font);
	// totalNumber->setFont(font);
}

void PlayInfo::setColor(const QColor &fontColor, const QColor &bgColor)
{
//	QColor fontColor(config.fontColorR, config.fontColorG, config.fontColorB);
//	QColor bgColor(config.bgColorR, config.bgColorG, config.bgColorB);

	QColorGroup colorGroup;
	colorGroup.setColor(QColorGroup::Background, bgColor);
	colorGroup.setColor(QColorGroup::Base, bgColor);
	colorGroup.setColor(QColorGroup::Button, bgColor);
	colorGroup.setColor(QColorGroup::Text, fontColor);
	colorGroup.setColor(QColorGroup::Foreground, fontColor);
	colorGroup.setColor(QColorGroup::ButtonText, fontColor);
	
	QPalette palette(colorGroup, colorGroup, colorGroup);
	
	this->setPalette(palette);
	
	// Title
	titleLabel->setPalette(palette);
	title->setPalette(palette);
	
	// Artist
	artistLabel->setPalette(palette);
	artist->setPalette(palette);
	
	// Album
	albumLabel->setPalette(palette);
	album->setPalette(palette);
	
	// Counter
	currentNumber->setPalette(palette);
	numberSeparator->setPalette(palette);
	totalNumber->setPalette(palette);
}

void PlayInfo::setTitle(const QString &s)
{
	title->setText(s);
}

void PlayInfo::setArtist(const QString &s)
{
	artist->setText(s);
}

void PlayInfo::setAlbum(const QString &s)
{
	album->setText(s);
}

void PlayInfo::setMediaInfo(const Media &info)
{
	if (!coverArtProvider->isCached(info.artist(), info.album()) || 
		!(info.artist() == artist->text() && info.album() == album->text()))
	{
		if (coverArtProvider->loadCover(info.artist(), info.album(), info.location().toString()))
		{
			coverArt->setImage(coverArtProvider->coverArt());
		}
		else
		{
			coverArt->setImage(NULL);
		}
		
		coverArt->update();
	}
	
	setTitle(info.title());
	setArtist(info.artist());
	setAlbum(info.album());
}

void PlayInfo::setTotalNumber(unsigned int n)
{
	totalNumber->display((int)n);
}

void PlayInfo::setCurrentNumber(unsigned int n)
{
	currentNumber->display((int)n);
}
