/***************************************************************************
 *   Copyright (C) 2004, 2005 Thomas Nagy                                  *
 *   tnagy2^8@yahoo.fr                                                     *
 *                                                                         *
 *   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 <qdir.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qmap.h>
#include <qpixmap.h>

#include <kurl.h>
#include <kdebug.h>
#include <ktempdir.h>
#include <kgenericfactory.h>

#include "settings.h"
#include "datastruct/DItem.h"
#include "datastruct/DDataControl.h"
#include "datastruct/DDataItem.h"

#include "kdissbeamerslides.h"

K_EXPORT_COMPONENT_FACTORY(libkdissbeamerslides, kdissbeamerslidesFactory) 
	QObject *kdissbeamerslidesFactory::createObject (QObject *, const char *, const char *, const QStringList &) { return new kdissbeamerslides; }


kdissbeamerslides::kdissbeamerslides() : DGenerator()
{
	m_data = NULL;
}

const QString kdissbeamerslides::identifikation()
{
	return "kdissbeamerslides";
}

const QString kdissbeamerslides::fullName()
{
	return i18n("Slides Beamer");
}

const QString kdissbeamerslides::group()
{
	return "LaTeX";
}

const QString kdissbeamerslides::description()
{
	return i18n("This template generates beamer latex slides. A minimum beamer distribution is included for your convenience, but you may want to check the beamer website for installing the complete distribution on your system (http://beamer.sourceforge.net), and consulting the documentation. Most of the output is commented and does not appear in the slides. You will have to work a bit on the tex file generated, but the results are worth the effort :)");
}

const QString kdissbeamerslides::quickstart()
{
	return i18n( "Go into the directory created and run 'make' to create a pdf file (to view use 'make viewpdf'\n"
			"Acrobat Reader (Adobe tool) may give more interesting results (fullscreen display)\n"
			"A postscript document can be generated by using the command 'make ps' (to wiew use 'make viewps')\n"
			"Using a LaTeX tool such as Kile is recommended for editing the generated file main.tex");
}

void kdissbeamerslides::outputPixParagraph(DDataItem* item, QTextStream & s)
{
	if (item->m_pix.isNull())
		return;

	// the output is commented
	s<<"%XXX : to include the pictures, you will have to convert them to eps\n";
	s<<"%\\begin{figure}[htbp]\n";
	s<<"%\\begin{center}\n";
	s<<"%\\includegraphics[width=3cm]{pics/pic-"<<QString::number(item->Id())<<"}\n";
	s<<"%\\caption{\\footnotesize{"<<protectTex(item->m_piccaption)<<"}}\n";
	s<<"%%"<<item->m_picurl<<"\n";
	s<<"%\\end{center}\n";
	s<<"%\\end{figure}\n\n";
}

void kdissbeamerslides::outputURL(DDataItem* item, QTextStream & s)
{
	for (unsigned int i=0; i<item->m_urllist.count(); i++)
	{
		QString url = item->m_urllist[i].m_url;
		QString caption = item->m_urllist[i].m_caption;

		if (!caption.length()) caption = url;


		s<<commentTex(url)<<"\n";
		s<<commentTex(caption)<<"\n";
	}
}

void kdissbeamerslides::outputComment(DDataItem* item, QTextStream & s)
{
	if (item->m_comment.length()>0)
		s<<commentTex(item->m_comment)<<"\n";
}

void kdissbeamerslides::outputData(DDataItem* item, QTextStream & s)
{
	s<<commentTex( htmlToTex(item->m_text) )<<"\n\n";

	outputPixParagraph(item, s);
	outputURL(item, s);
	outputComment(item, s);
}

QString randomTrans()
{
	int val = random()%7;
	QString ret;
	switch (val)
	{
		case 0:
			ret = "[Split]";
			break;

		case 1:
			ret = "[Blinds]";
			break;

		case 2:
			ret = "[Box]";
			break;

		case 3:
			ret = "[Wipe]";
			break;

		case 4:	
			ret = "[Dissolve]";
			break;

		case 5:	    
			ret = "[Glitter]";
			break;

		case 6:
			ret = "[Replace]";
			break;

		default:
			break;
	}
	return ret;
}

void kdissbeamerslides::writeCompleteSlide(DDataItem* item, QTextStream & s, int level)
{
	//kdWarning()<<"calling kdissbeamerslides::writeCompleteSlide "<<level<<" "<<item<<endl;
	if (level == 0)
	{
		s<<"%------------------------------------------------------------------- \n";
		s<<"\\begin{frame}\n";
		s<<"\\frametitle{"<<protectTex(item->m_summary)<<"}\n\n";

		if (item->countChildren() > 0)
		{	    
			s<<"\\begin{itemize}\n";
			outputData(item, s);

			for (int i = 0; i<item->countChildren(); i++)
			{
				writeCompleteSlide((DDataItem*) m_data->Item(item->childNum(i)), s, level+1);
			}

			s<<"\\end{itemize}\n";
		}
		s<<"\\end{frame}\n";
		s<<"%------------------------------------------------------------------- \n";
	}
	else if (level == 1)
	{
		s<<"\\item "<<protectTex(item->m_summary)<<"\n%\n";
		outputData(item, s);

		if (item->countChildren() > 0)
		{
			s<<"\\begin{itemize}\n";
			for (int i = 0; i<item->countChildren(); i++)
			{
				writeCompleteSlide((DDataItem*) m_data->Item(item->childNum(i)), s, level+1);
			}
			s<<"\\end{itemize}\n";
		}
	}
	else if (level == 2)
	{
		s<<"\\item "<<protectTex(item->m_summary)<<"\n";
		outputData(item, s);

		if (item->countChildren() > 0)
		{
			s<<"\\begin{itemize}\n";
			for (int i = 0; i<item->countChildren(); i++)
			{
				writeCompleteSlide((DDataItem*) m_data->Item(item->childNum(i)), s, level+1);
			}
			s<<"\\end{itemize}\n";
		}
	}
	else if (level == 3)
	{
		s<<"\\item "<<protectTex(item->m_summary)<<"\n";
		outputData(item, s);

		for (int i = 0; i<item->countChildren(); i++)
		{
			writeCompleteSlide((DDataItem*) m_data->Item(item->childNum(i)), s, level+1);
		}
	}
	else if (level > 3)
	{
		// too much nesting, output in comments
		s<<commentTex(item->m_summary)<<"\n";
		outputData(item, s);

		for (int i = 0; i<item->countChildren(); i++)
		{
			writeCompleteSlide((DDataItem*) m_data->Item(item->childNum(i)), s, level+1);
		}
	}
}

void kdissbeamerslides::writeItem(DDataItem* item, QTextStream & s, int level)
{
	DGenerator::writeItem(item, s, level);

	if (level == 0)
	{
		if (! item->m_pix.isNull())
		{
			s<<"% you will have to convert the picture to .eps to make this work :\n";
			s<<"%\\Logo(2,5){\\includegraphics[width=1cm]{"<<QString::number(item->Id())<<".eps}}\n";
		}

		// comment this text since it is out of the body text
		s<<"\n"<<commentTex( htmlToTex(item->m_text) )<<"\n";
		s<<commentTex(item->m_comment)<<"\n";

	}

	for (int i = 0; i<item->countChildren(); i++)
	{
		DDataItem* subtree = (DDataItem*) m_data->Item(item->childNum(i));

		// 10 is the maximum maximum size of a subtree 
		// that can fit on a single slide

		if (level == 0 && item->countChildren() > 1)
		{
			s<<"\\section{"<<protectTex(subtree->m_summary)<<"}\n";
		}
		else if (level == 1 && item->countChildren() > 1)
		{
			s<<"\\subsection{"<<protectTex(subtree->m_summary)<<"}\n";
		}

		if (subtree->countFamily() < 10)
		{
			writeCompleteSlide(subtree, s, 0);
		}

		else if (level == 0)
		{
			outputData(subtree, s);
			writeItem(subtree, s, level+1);
		}
		else if (level == 1)	
		{
			outputData(subtree, s);
			writeItem(subtree, s, level+1);
		}
		else
		{
			writeCompleteSlide(subtree, s, 0);
			kdWarning()<<i18n("Your mindmap is insane !!")<<endl;
		}
	}
}

bool kdissbeamerslides::writeMaindoc(DDataItem *root, const QString & path)
{
	QFile textfile(path+"/main.tex");
	if (!textfile.open(IO_WriteOnly | IO_Append) )
	{
		kdWarning()<<"could not open LaTeX file for append :-/"<<endl;
		return false;
	}

	QTextStream u(&textfile);
	if (Settings::templatesUTF8())
	{
		u.setEncoding(QTextStream::UnicodeUTF8);
		u<<"\\usepackage{ucs}\n";
		u<<"\\usepackage[utf8]{inputenc}\n\n\n";
	}
	else
	{
		u<<"\\usepackage[latin1]{inputenc}\n\n\n";
	}

	// make a nice title for the first page
	u<<"\\title{"<<protectTex(root->m_summary)<<"}\n";
	u<<"\\date{}\n";
	u<<"\\author{"<<protectTex(m_data->m_fname)<<" "<<protectTex(m_data->m_sname)<<"}\n";

	// standard headers
	u<<"\n";
	u<<"\\begin{document}\n";

	u<<"\\frame[plain]{\\titlepage}\n\n";

	u<<"%\\tableofcontents\n";
	u<<i18n("% tables of contents are generated from the \\section and \\subsection statements");
	u<<"\n";
	u<<i18n("% you may probably wish to insert them before the appropriate frames");
	u<<"\n\n\n";

	u<<"\\begin{frame}\n \\frametitle{Outline}\n \\tableofcontents\n \\end{frame} \n\n";

	// now the central part, with text, pictures, etc ..
	writeItem(root, u, 0);

	// central part is finished
	u<<"\\end{document}\n\n";
	textfile.close();

	notifyDocGenerated( path+"/main.tex" );

	return true;
}

#include "kdissbeamerslides.moc"
