/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2013  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "screenshotsavedialog.h"
#include <algorithm>
#include <QImageWriter>
#include <QBuffer>
#include <QTimer>
#include <QtDebug>
#include <util/util.h>

namespace LeechCraft
{
namespace Poshuku
{
	ScreenShotSaveDialog::ScreenShotSaveDialog (const QPixmap& source,
			QWidget *parent)
	: QDialog (parent)
	, Source_ (source)
	, PixmapHolder_ (new QLabel ())
	, RenderScheduled_ (false)
	{
		PixmapHolder_->setAlignment (Qt::AlignTop | Qt::AlignLeft);
		Ui_.setupUi (this);
	
		QList<QByteArray> formats = QImageWriter::supportedImageFormats ();
		formats.removeAll ("ico");
		if (formats.contains ("jpg"))
			formats.removeAll ("jpeg");
		std::sort (formats.begin (), formats.end ());
		for (QList<QByteArray>::const_iterator i = formats.begin (),
				end = formats.end (); i != end; ++i)
			Ui_.FormatCombobox_->addItem (i->toUpper ());
		if (formats.contains ("png"))
			Ui_.FormatCombobox_->setCurrentIndex (formats.indexOf ("png"));
	
		Ui_.Scroller_->setWidget (PixmapHolder_);
	}
	
	QByteArray ScreenShotSaveDialog::Save ()
	{
		QString format = Ui_.FormatCombobox_->currentText ();
		int quality = Ui_.QualitySlider_->value ();
	
		QBuffer renderBuffer;
		Source_.save (&renderBuffer, qPrintable (format), quality);
		return renderBuffer.data ();
	}
	
	void ScreenShotSaveDialog::ScheduleRender ()
	{
		if (RenderScheduled_)
			return;
	
		QTimer::singleShot (500,
				this,
				SLOT (render ()));
	
		RenderScheduled_ = true;
	}
	
	void ScreenShotSaveDialog::render ()
	{
		RenderScheduled_ = false;
		if (!Ui_.PreviewBox_->isChecked ())
		{
			Ui_.FileSizeLabel_->setText (tr ("File size unknown"));
			PixmapHolder_->setPixmap (QPixmap ());
			PixmapHolder_->resize (1, 1);
			return;
		}
	
		QString format = Ui_.FormatCombobox_->currentText ();
		int quality = Ui_.QualitySlider_->value ();
	
		QBuffer renderBuffer;
		Source_.save (&renderBuffer, qPrintable (format), quality);
		QByteArray renderData = renderBuffer.data ();
		Rendered_.loadFromData (renderData);
		Ui_.FileSizeLabel_->setText (Util::MakePrettySize (renderData.size ()));
		PixmapHolder_->setPixmap (Rendered_);
		PixmapHolder_->resize (Rendered_.size ());
	}
	
	void ScreenShotSaveDialog::on_QualitySlider__valueChanged ()
	{
		ScheduleRender ();
	}
	
	void ScreenShotSaveDialog::on_FormatCombobox__currentIndexChanged ()
	{
		ScheduleRender ();
	}
}
}
