/*
 * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <QtCore/QDebug>

#include <QtGui/QPainter>

#include <ZLImage.h>

#include "ZLQtImageUtils.h"
#include "ZLQtImageManager.h"

ZLQtImageData::ZLQtImageData() : myImage(0), myX(0), myY(0) {
}

ZLQtImageData::~ZLQtImageData() {
	if (myImage != 0) {
		delete myImage;
	}
}

unsigned int ZLQtImageData::width() const {
	return (myImage != 0) ? myImage->width() : 0;
}

unsigned int ZLQtImageData::height() const {
	return (myImage != 0) ? myImage->height() : 0;
}

void ZLQtImageData::init(unsigned int width, unsigned int height) {
	if (myImage != 0) {
		delete myImage;
	}
	myImage = new QImage(width, height, QImage::Format_RGB32);
}

void ZLQtImageData::setPosition(unsigned int x, unsigned int y) {
	myX = x;
	myY = y;
}

void ZLQtImageData::moveX(int delta) {
	myX += delta;
}

void ZLQtImageData::moveY(int delta) {
	myY += delta;
}

void ZLQtImageData::setPixel(unsigned char r, unsigned char g, unsigned char b) {
	myImage->setPixel(myX, myY, qRgb(r, g, b));
}

void ZLQtImageData::copyFrom(const ZLImageData &source, unsigned int targetX, unsigned int targetY) {
	const QImage *sourceImage = ((const ZLQtImageData*)&source)->image();
	if ((myImage != 0) && (sourceImage != 0)) {
		QPainter painter(myImage);
		painter.drawImage(targetX, targetY, *sourceImage);
	}
}

shared_ptr<ZLImageData> ZLQtImageManager::createData() const {
	return new ZLQtImageData();
}

bool ZLQtImageManager::convertImageDirect(const std::string &stringData, ZLImageData &data) const {
	((ZLQtImageData&)data).init(0, 0);
	return ((ZLQtImageData&)data).myImage->loadFromData((const unsigned char*)stringData.data(), stringData.length());
}

shared_ptr<const ZLImage> ZLQtImageManager::makeBatchImage(const std::vector<shared_ptr<const ZLImage> > &images, shared_ptr<const ZLImage> defaultImage) const {
	QPixmap defaultPixmap = ZLQtImageUtils::ZLImageToQPixmap(defaultImage);
	QList<QPixmap> pixmaps;
	foreach(shared_ptr<const ZLImage> image, images) {
		if (!image.isNull() && image->good()) {
			pixmaps.push_back(ZLQtImageUtils::addOppositeBorder(ZLQtImageUtils::ZLImageToQPixmap(image), 2));
		}
	}

	for (size_t i = (std::size_t)pixmaps.size(); i < images.size(); ++i) {
		pixmaps.push_front(defaultPixmap);
	}

	int minHeight = countMinSize(pixmaps).height();
	for (int i = 0; i < pixmaps.size(); ++i) {
		pixmaps.replace(i, pixmaps.at(i).scaledToHeight(minHeight, Qt::FastTransformation));
	}

	QSize maxSize = countMaxSize(pixmaps);
	QSize offset = calcOffset(pixmaps.size(), maxSize);
	maxSize += QSize(offset.width() * (pixmaps.size() - 1), offset.height() * (pixmaps.size() - 1));

	QPixmap batch(maxSize);
	batch.fill(Qt::transparent);
	QPainter painter(&batch);

	QPoint drawPoint(0,0);
	foreach(QPixmap p, pixmaps) {
		painter.drawPixmap(drawPoint, p);
		drawPoint += QPoint(offset.width(), offset.height());
	}

	return ZLQtImageUtils::QPixmapToZLImage(batch);
}

QSize ZLQtImageManager::calcOffset(int coversNumber, QSize forSize) {
	//offset percents are arithmetic progressions
	static const int widthStart = 37;
	static const double widthDiff = -5;
	static const double heightStart = 15.6;
	static const double heightDiff = -1.3;

	const int WIDTH_PERCENT = widthStart + (coversNumber - 1) * widthDiff;
	const int HEIGHT_PERCENT = heightStart + (coversNumber - 1) * heightDiff;
	const int DX = forSize.width() * WIDTH_PERCENT / 100;
	const int DY = forSize.height() * HEIGHT_PERCENT / 100;
	return QSize(DX, DY);
}

QSize ZLQtImageManager::countMaxSize(const QList<QPixmap> &pixmaps) {
	QSize maxSize(0,0);
	foreach(QPixmap p, pixmaps) {
		maxSize = maxSize.expandedTo(p.size());
	}
	return maxSize;
}

QSize ZLQtImageManager::countMinSize(const QList<QPixmap> &pixmaps) {
	if (pixmaps.empty()) {
		return QSize(0,0);
	}
	QSize minSize = pixmaps.at(0).size();
	foreach(QPixmap p, pixmaps) {
		minSize = minSize.boundedTo(p.size());
	}
	return minSize;
}
