/**********************************************************************
 * 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 "document.h"
#include <QtDebug>

namespace LeechCraft
{
namespace Monocle
{
namespace Postrus
{
	Document::Document (const QString& path, QObject *plugin)
	: SD_ (spectre_document_new ())
	, DocURL_ (QUrl::fromLocalFile (path))
	, Plugin_ (plugin)
	{
		spectre_document_load (SD_, path.toUtf8 ().constData ());
	}

	Document::~Document ()
	{
		spectre_document_free (SD_);
	}

	QObject* Document::GetBackendPlugin () const
	{
		return Plugin_;
	}

	QObject* Document::GetQObject ()
	{
		return this;
	}

	bool Document::IsValid () const
	{
		return spectre_document_status (SD_) != SPECTRE_STATUS_LOAD_ERROR;
	}

	DocumentInfo Document::GetDocumentInfo () const
	{
		DocumentInfo info;
		if (const char *title = spectre_document_get_title (SD_))
			info.Title_ = QString::fromUtf8 (title);
		if (const char *author = spectre_document_get_creator (SD_))
			info.Author_ = QString::fromUtf8 (author);
		return info;
	}

	int Document::GetNumPages () const
	{
		return spectre_document_get_n_pages (SD_);
	}

	namespace
	{
		QSize GetSpectrePageSize (SpectrePage *page)
		{
			QSize result;
			spectre_page_get_size (page, &result.rwidth (), &result.rheight ());
			return result;
		}
	}

	QSize Document::GetPageSize (int index) const
	{
		auto page = spectre_document_get_page (SD_, index);
		const auto& result = GetSpectrePageSize (page);
		spectre_page_free (page);
		return result;
	}

	QImage Document::RenderPage (int index, double xRes, double yRes)
	{
		auto page = spectre_document_get_page (SD_, index);

		auto rc = spectre_render_context_new ();
		auto size = GetPageSize (index);
		spectre_render_context_set_scale (rc, xRes, yRes);
		size.rwidth () *= xRes;
		size.rheight () *= yRes;

		unsigned char *data = 0;
		int rowLength = 0;
		spectre_page_render (page, rc, &data, &rowLength);
		spectre_render_context_free (rc);
		spectre_page_free (page);

		const QImage& img = rowLength == size.width () * 4 ?
				QImage (data, size.width (), size.height (), QImage::Format_RGB32) :
				QImage (data, rowLength / 4, size.height (), QImage::Format_RGB32)
					.copy (0, 0, size.width (), size.height ());
		free (data);
		return img;
	}

	QList<ILink_ptr> Document::GetPageLinks (int)
	{
		return QList<ILink_ptr> ();
	}

	QUrl Document::GetDocURL () const
	{
		return DocURL_;
	}
}
}
}
