/*
 * Copyright (C) 2007 Andre Beckedorf <evilJazz _AT_ katastrophos _DOT_ net>
 * 
 * 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 <qevent.h>
#include <qpainter.h>

#include "debug.h"
#include "media.h"
#include "imagefx.h"

#include "coverart.h"

CoverArt::CoverArt(QWidget *parent, const char *name)
	:	QWidget(parent, name),
		imageValid_(false),
		coverImage_(NULL)
{
	setBackgroundMode(NoBackground);
	setWFlags(Qt::WNorthWestGravity | Qt::WRepaintNoErase | Qt::WResizeNoErase);
}

CoverArt::~CoverArt()
{

}

void CoverArt::paintEvent(QPaintEvent *)
{
	QPainter painter(this);

	if (coverImage_ && !coverImage_->isNull())
	{
		if (!imageValid_)
		{
			DTIMERINIT(test);
			
			float Cx, Cy;
			float RScaleX, RScaleY;
			float MaxWidth, MaxHeight;
			
			MaxWidth = width() - 20;
			MaxHeight = height() - 50;
			
			Cx = coverImage_->width();
			Cy = coverImage_->height();
			
			RScaleX = MaxWidth / Cx;
			RScaleY = MaxHeight / Cy;
	
			if (RScaleX >= RScaleY)
			{
				Cx = Cx * RScaleY;
				Cy = MaxHeight;
			}
			else
			{
				Cx = MaxWidth;
				Cy = Cy * RScaleX;
			}
			
			if (Cx > 0 && Cy > 0)
			{
				DTIMERINIT(test2);
				scaledImage_ = coverImage_->smoothScale(Cx, Cy);
				DTIMERPRINT(test2, "smooth scale");
				
				DTIMERSTART(test2);
				scaledImage_ = ImageFX::createReflection(scaledImage_, 60);
				DTIMERPRINT(test2, "create reflection");

				tempImage_.create(width(), height(), 32);
				tempImage_.setAlphaBuffer(false);
	
				paintToBuffer();	
				
				imageValid_ = true;
			}
			DTIMERPRINT(test, "cover art preparation");
		}
		
		painter.drawPixmap(QPoint(0, 0), doubleBuffer_);
	}
	else
	{
		painter.fillRect(0, 0, width(), height(), QBrush(black));
		painter.setPen(white);
		painter.drawText(QPoint(20, height() / 2), "No cover available.");
	}
}

void CoverArt::paintToBuffer()
{
	DTIMERINIT(test2);

	tempImage_.fill(0xFF000000);
	
	FloatPoint p1, p2, p3, p4;
	
	ImageFX::calculatePerspectivePoints(
		width() / 2, (height() - scaledImage_.height()) / 2 + 15, scaledImage_.width(), scaledImage_.height(),
		30, 0.2, 0.5, true,
		p1, p2, p3, p4
	);
	
	ImageFX::projectiveTransformation(
		scaledImage_, tempImage_, 
		p1, p2, p3, p4,
		true
	);

	DTIMERPRINT(test2, "projective transformation");
	
	doubleBuffer_.convertFromImage(tempImage_);

	// clean up, this is temporary until I get
	// the funky effects finished...
	tempImage_.reset();
	scaledImage_.reset();
}

void CoverArt::resizeEvent(QResizeEvent *e)
{
	imageValid_ = false;
	repaint();
}
