/***************************************************************************
*   Copyright (C) 2006-2008 by Ian Reinhart Geiser                        *
*   geiseri@yahoo.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.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "docbookreporter.h"
#include "block.h"
#include "mindmap.h"

#include <QDateTime>
#include <QPainter>
#include <QFileInfo>
#include <QImage>

#if QT_VERSION >= 0x040300

#include <QXmlStreamWriter>


void DocbookReporter::generateSection( Block * node, QXmlStreamWriter * doc )
{
	doc->writeStartElement("section");
	doc->writeTextElement( "title", node->title());

	if( !node->emblem().isEmpty() ) generateImage( node, doc );

	foreach( QString para, node->text().split("\n") )
		doc->writeTextElement("para", para );

	bool inList = false;
	foreach( Block *child, node->children() )
	{
		if( child->shapeType() == Block::Path )
		{
			if( inList == false )
			{
				doc->writeStartElement("orderedlist");
			}
			generateList( child, doc );
			inList = true;
		}
		else
		{
			if( inList == true )
			{
				doc->writeEndElement();
			}
			generateSection( child, doc);
			inList = false;
		}
	}

	doc->writeEndElement();
}

void DocbookReporter::generateList( Block * node, QXmlStreamWriter * doc )
{
	doc->writeStartElement("listitem");
	doc->writeTextElement( "para", node->title());

	if( !node->emblem().isEmpty() ) generateImage( node, doc );

	if( !node->text().isEmpty() )
	{
		doc->writeStartElement("itemizedlist");
		foreach( QString para, node->text().split("\n") )
		{
			if( !para.isEmpty() )
			{
				doc->writeStartElement("listitem");
				doc->writeTextElement("para", para );
				doc->writeEndElement();
			}
		}
		doc->writeEndElement();
	}

	foreach( Block *child, node->children() )
	{
		doc->writeStartElement("orderedlist");
		generateList( child, doc );
		doc->writeEndElement();
	}
	doc->writeEndElement();
}


void DocbookReporter::generateArticleHeader( MindMap * map, QXmlStreamWriter * doc )
{
	doc->writeStartElement("articleinfo");
	doc->writeTextElement("title", map->getProperty("document.title", "Untitled").toString() );

	QString author = map->getProperty("document.author").toString();
	if( !author.isEmpty() )
	{
		doc->writeStartElement("author");
		doc->writeTextElement("surname", author );
		doc->writeEndElement();
	}
	QString abstract = map->getProperty("document.description").toString();
	if( !abstract.isEmpty())
	{
		doc->writeStartElement("abstract");
		foreach( QString para, abstract.split("\n") )
			doc->writeTextElement("para", para );
		doc->writeEndElement();
	}

	QString copyrightHolder = map->getProperty("docbook.copyrightholder").toString();
	QDate copyrightDate = map->getProperty("docbook.copyrightdate").toDate();

	if( !copyrightHolder.isEmpty() )
	{
		doc->writeStartElement("copyright");
		doc->writeTextElement("year", copyrightDate.toString() );
		doc->writeTextElement("holder", copyrightHolder);
	}

	doc->writeTextElement("pubdate", copyrightDate.toString());
	doc->writeEndElement();

}

QString DocbookReporter::generateReport( MindMap * map )
{
	QString report;

	QXmlStreamWriter streamer( &report );
	streamer.setAutoFormatting(true);
	streamer.writeStartDocument("1.0");
	streamer.writeDTD("<!DOCTYPE article PUBLIC \"-//OASIS//DTD DocBook XML V4.1.2//EN\" \"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd\">");
	streamer.writeStartElement("article");

	generateArticleHeader( map,&streamer );
	generateSection( map->root(), &streamer );

	streamer.writeEndElement();
	streamer.writeEndDocument();

	return report;
}

void printPDFImage( Block *node, const QString &fileName )
{
	QPrinter pdfPrinter;
	pdfPrinter.setPageSize(QPrinter::A7);
	pdfPrinter.setOrientation(QPrinter::Landscape);
	pdfPrinter.setOutputFormat(QPrinter::PdfFormat);
	pdfPrinter.setOutputFileName(fileName);
	pdfPrinter.setResolution(QPrinter::HighResolution);
	pdfPrinter.setFullPage(true);
	QPainter painter(&pdfPrinter);
	node->drawEmblemImage(pdfPrinter.pageRect(), &painter);
	painter.end();

}

void DocbookReporter::generateImage( Block * node, QXmlStreamWriter * doc )
{
	QFileInfo emblemFileInfo( node->emblem() );
	QImage outputImage( node->naturalEmblemSize().toSize(), QImage::Format_ARGB32_Premultiplied);
	QPainter painter(&outputImage);
	node->drawEmblemImage(QRectF(QPointF(0,0),node->naturalEmblemSize()), &painter);
	painter.end();

	QString baseImageName( node->pathString() + "-" + emblemFileInfo.baseName());
	baseImageName.replace('.','-');

	doc->writeStartElement("mediaobject");

	doc->writeStartElement("imageobject");
	doc->writeStartElement("imagedata");
	doc->writeAttribute( "fileref", baseImageName + ".png" );
	doc->writeAttribute( "format", "PNG" );
	doc->writeEndElement();
	doc->writeEndElement();

	doc->writeStartElement("imageobject");
	doc->writeStartElement("imagedata");
	doc->writeAttribute( "fileref", baseImageName + ".pdf" );
	doc->writeAttribute( "format", "EPS" );
	doc->writeEndElement();
	doc->writeEndElement();

	doc->writeEndElement();
	if( !outputImage.save(m_resourceBaseDir + "/" + baseImageName + ".png", "PNG", 100 ) )
	{
		qDebug("error");
	}
	printPDFImage(node, m_resourceBaseDir + "/" + baseImageName + ".pdf");
}

#else

QString DocbookReporter::generateReport( MindMap *map ){ return QString();}
void DocbookReporter::generateSection( Block *node, QXmlStreamWriter *doc ){;}
void DocbookReporter::generateList( Block *node, QXmlStreamWriter *doc ){;}
void DocbookReporter::generateImage( Block *node, QXmlStreamWriter *doc ){;}
void DocbookReporter::generateArticleHeader( MindMap *map, QXmlStreamWriter *doc ){;}

#endif

QString DocbookReporter::resourceBaseDir() const
{
	return m_resourceBaseDir;
}

void DocbookReporter::setResourceBaseDir( const QString& theValue )
{
	m_resourceBaseDir = theValue;
}

DocbookReporter::DocbookReporter()
{}


DocbookReporter::~DocbookReporter()
{}

