/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  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.
 **********************************************************************/

#pragma once

#include <memory>
#include <QImage>
#include <QMetaType>
#include <QStringList>
#include <QDateTime>
#include "ilink.h"

class QUrl;

template<typename>
class QFuture;

namespace LeechCraft
{
namespace Monocle
{
	/** @brief Document metadata.
	 *
	 * All fields in this structure can be null.
	 */
	struct DocumentInfo
	{
		/** @brief Document title.
		 */
		QString Title_;
		/** @brief The subject line of this document.
		 */
		QString Subject_;
		/** @brief Description of the document.
		 */
		QString Description_;
		/** @brief The author of the document.
		 */
		QString Author_;

		/** @brief Genres of this document.
		 */
		QStringList Genres_;
		/** @brief Keywords corresponding to this document.
		 */
		QStringList Keywords_;

		/** @brief Date this document was created.
		 */
		QDateTime Date_;
	};

	/** @brief Basic interface for documents.
	 *
	 * This interface is the basic interface for documents returned from
	 * format backends.
	 *
	 * Pages actions (like rendering) are also performed via this
	 * interface. Pages indexes are zero-based.
	 *
	 * This class has some signals, and one can use the GetQObject()
	 * method to get an object of this class as a QObject and connect to
	 * those signals.
	 *
	 * There are also other interfaces for extended functionality like
	 * documents that can be changed and saved, the documents containing
	 * annotations and so on. See the <em>See also</em> section for the
	 * details.
	 *
	 * @sa IBackendPlugin::LoadDocument()
	 * @sa IDynamicDocument, IHaveTextContent, ISaveableDocument
	 * @sa ISearchableDocument, ISupportAnnotations, ISupportForms
	 * @sa IHaveTOC, ISupportPainting
	 */
	class IDocument
	{
	public:
		/** @brief Virtual destructor.
		 */
		virtual ~IDocument () {}

		/** @brief Returns the parent backend plugin.
		 *
		 * This function should return the instance object of the backend
		 * plugin that created this document.
		 *
		 * The returned value should obviously implement IBackendPlugin.
		 *
		 * @return The parent backend plugin instance object.
		 */
		virtual QObject* GetBackendPlugin () const = 0;

		/** @brief Returns this object as a QObject.
		 *
		 * This function can be used to connect to the signals of this
		 * class.
		 *
		 * @return This object as a QObject.
		 */
		virtual QObject* GetQObject () = 0;

		/** @brief Returns whether this document is valid.
		 *
		 * An invalid document is basically equivalent to a null pointer,
		 * all operations on it lead to undefined behavior.
		 *
		 * @return Whether this document is valid.
		 */
		virtual bool IsValid () const = 0;

		/** @brief Returns the document metadata.
		 *
		 * @return The document metadata.
		 */
		virtual DocumentInfo GetDocumentInfo () const = 0;

		/** @brief Returns the number of pages in this document.
		 *
		 * @return The number of pages in this document.
		 */
		virtual int GetNumPages () const = 0;

		/** @brief Returns the size in pixels of the given page.
		 *
		 * This function returns the physical dimensions of the given
		 * \em page in pixels.
		 *
		 * Some formats support different pages having different sizes in
		 * the same document, thus the size should be queried for each
		 * \em page.
		 *
		 * @param[in] page The index of the page to query.
		 * @return The size of the given \em page.
		 */
		virtual QSize GetPageSize (int page) const = 0;

		/** @brief Renders the given \em page at the given scale.
		 *
		 * This function should return an image with the given \em page
		 * rendered at the given \em xScale and \em yScale for <em>x</em>
		 * and <em>y</em> axises correspondingly. That is, the image's
		 * size should be equal to the following:
		 * \code
			auto size = GetPageSize (page);
			size.rwidth () *= xScale;
			size.rheight () *= yscale;
		   \endcode
		 *
		 * @param[in] page The index of the page to render.
		 * @param[in] xScale The scale of the <em>x</em> axis.
		 * @param[in] yScale The scale of the <em>y</em> axis.
		 * @return The rendering of the given page.
		 */
		virtual QFuture<QImage> RenderPage (int page, double xScale, double yScale) = 0;

		/** @brief Returns the links found at the given \em page.
		 *
		 * If the format doesn't support links, an empty list should be
		 * returned.
		 *
		 * The ownership of the returned links objects is passed to the
		 * caller.
		 *
		 * @param[in] page The page index to query.
		 *
		 * @sa ILink
		 */
		virtual QList<ILink_ptr> GetPageLinks (int page) = 0;

		/** @brief Returns the URL of the document.
		 *
		 * This method should return the URL of this document. URLs on
		 * the local filesystem should obviously have the <em>file</em>
		 * scheme.
		 *
		 * @return The URL of this document.
		 */
		virtual QUrl GetDocURL () const = 0;

		/** @brief Describes a position within a document.
		 */
		struct Position
		{
			/** @brief The index of the page in question.
			 */
			int Page_;

			/** @brief The position within the page.
			 *
			 * The position is absolute, that is, between 0 and the bottom right
			 * corner of the page as returned by IDocument::GetPageSize().
			 */
			QPointF PagePosition_;
		};

		/** @brief Emitted when navigation is requested.
		 *
		 * For example, this signal is emitted when a navigation link in
		 * a table of contents has been triggered.
		 *
		 * If \em filename is empty, \em pageNum, \em x and \em y are all
		 * related to he current document. Otherwise \em filename should
		 * be loaded first.
		 *
		 * @param[out] filename The filename of the document to navigate
		 * to, or an empty string if the current document should be used.
		 * @param[out] position The position within the document described by
		 * \em filename.
		 *
		 * @sa Position
		 */
		virtual void navigateRequested (const QString& filename, const Position& position) = 0;

		/** @brief Emitted when printing is requested.
		 *
		 * This signal is emitted when printing is requested, for
		 * example, by a link action.
		 *
		 * @param[out] pages The list of pages to print, or an empty list
		 * to print all pages.
		 */
		virtual void printRequested (const QList<int>& pages) = 0;
	};

	/** @brief Shared pointer to a document.
	 */
	typedef std::shared_ptr<IDocument> IDocument_ptr;
}
}

Q_DECLARE_INTERFACE (LeechCraft::Monocle::IDocument,
		"org.LeechCraft.Monocle.IDocument/1.0")
Q_DECLARE_METATYPE (LeechCraft::Monocle::IDocument::Position)
