/***************************************************************************
*   Copyright (C) 2006 by                                                 *
*   Jason Kivlighn (jkivlighn@gmail.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.                                   *
***************************************************************************/

#include "htmlbookexporter.h"

#include <tqfile.h>
#include <tqstylesheet.h>

#include <kdebug.h>

#include "backends/recipedb.h"
#include "datablocks/categorytree.h"

HTMLBookExporter::HTMLBookExporter( CategoryTree *categories, const TQString& basedir, const TQString &format ) :
		HTMLExporter( basedir+"/index", format ), m_categories(categories), m_basedir(basedir)
{
}

HTMLBookExporter::~HTMLBookExporter()
{
}

int HTMLBookExporter::headerFlags() const
{
	return RecipeDB::Categories | RecipeDB::Title;
}

TQString HTMLBookExporter::createContent( const RecipeList& recipes )
{
	RecipeList::const_iterator recipe_it;
	for ( recipe_it = recipes.begin(); recipe_it != recipes.end(); ++recipe_it ) {
		for ( ElementList::const_iterator cat_it = ( *recipe_it ).categoryList.begin(); cat_it != ( *recipe_it ).categoryList.end(); ++cat_it ) {
			TQMap<TQString,TQTextStream*>::iterator stream_it = fileMap.find( (*cat_it).name );
			(**stream_it) << "<br /><br />";
			(**stream_it) << TQString("<a name=\""+(*recipe_it).title+"\" />");
			(**stream_it) << HTMLExporter::createContent(*recipe_it);
			(**stream_it) << TQString("[ <a href=\"#top\">Top</a> ]");
			(**stream_it) << TQString("[ <a href=\"index.html\">Back</a> ]");
			(**stream_it) << "<br /><br />";
		}
	}

	return TQString::null;
}

TQString HTMLBookExporter::createHeader( const RecipeList &list )
{
	TQString output = HTMLExporter::createHeader(list);

	TQString catLinks;
	TQTextStream catLinksStream(&catLinks,IO_WriteOnly);
	createCategoryStructure(catLinksStream,list);

	return output+"<h1>Krecipes Recipes</h1><div>"+catLinks+"</li></ul></div>";
}

TQString HTMLBookExporter::createFooter()
{
	TQMap<TQString,TQTextStream*>::const_iterator it;
	for ( it = fileMap.begin(); it != fileMap.end(); ++it ) {
		(**it) << HTMLExporter::createFooter();

		(*it)->device()->close();

		//does it matter the order of deletion here?
		TQIODevice *file = (*it)->device();
		delete *it;
		delete file;
	}

	TQString output = HTMLExporter::createFooter();
	return output;
}

void HTMLBookExporter::createCategoryStructure( TQTextStream &xml, const RecipeList &recipes )
{
	TQValueList<int> categoriesUsed;
	for ( RecipeList::const_iterator recipe_it = recipes.begin(); recipe_it != recipes.end(); ++recipe_it ) {
		for ( ElementList::const_iterator cat_it = ( *recipe_it ).categoryList.begin(); cat_it != ( *recipe_it ).categoryList.end(); ++cat_it ) {
			TQMap<TQString,TQTextStream*>::iterator stream_it = fileMap.find( (*cat_it).name );
			if ( categoriesUsed.find( ( *cat_it ).id ) == categoriesUsed.end() ) {
				categoriesUsed << ( *cat_it ).id;

				TQString catPageName = m_basedir+"/"+escape((*cat_it).name)+".html";
				TQFile *catPage = new TQFile( catPageName );
				catPage->open( IO_WriteOnly );
				TQTextStream *stream = new TQTextStream( catPage );
				stream->setEncoding( TQTextStream::UnicodeUTF8 );
				(*stream) << HTMLExporter::createHeader(recipes);
				(*stream) << TQString("<a name=\"top\" />");
				(*stream) << "<h1>"<<(*cat_it).name<<"</h1>";

				stream_it = fileMap.insert((*cat_it).name,stream);
			}
			(**stream_it) << TQString("[ <a href=\"#" + (*recipe_it).title + "\">" + (*recipe_it).title + "</a> ]");
		}
	}

	if ( !categoriesUsed.empty() ) {
		//only keep the relevant category structure
		removeIfUnused( categoriesUsed, m_categories );

		xml << "<ul>\n";
		writeCategoryStructure( xml, m_categories );
		xml << "</ul>\n";
	}
}

bool HTMLBookExporter::removeIfUnused( const TQValueList<int> &cat_ids, CategoryTree *parent, bool parent_should_show )
{
	for ( CategoryTree * it = parent->firstChild(); it; it = it->nextSibling() ) {
		if ( cat_ids.find( it->category.id ) != cat_ids.end() ) {
			parent_should_show = true;
			removeIfUnused( cat_ids, it, true ); //still recurse, but doesn't affect 'parent'
		}
		else {
			bool result = removeIfUnused( cat_ids, it );
			if ( parent_should_show == false )
				parent_should_show = result;
		}
	}

	if ( !parent_should_show && parent->category.id != -1 ) {
		//FIXME: CategoryTree is broken when deleting items
		//delete parent;

		parent->category.id = -2; //temporary workaround
	}

	return parent_should_show;
}

void HTMLBookExporter::writeCategoryStructure( TQTextStream &xml, const CategoryTree *categoryTree )
{
	if ( categoryTree->category.id != -2 ) {
		if ( categoryTree->category.id != -1 ) {
			TQString catPageName = TQStyleSheet::escape(categoryTree->category.name)+".html";

			xml << "\t<li>\n\t\t<a href=\""+catPageName+"\">"+TQStyleSheet::escape( categoryTree->category.name ).replace("\"","&quot;") + "</a>\n";
		}
	
		for ( CategoryTree * child_it = categoryTree->firstChild(); child_it; child_it = child_it->nextSibling() ) {
			if ( categoryTree->parent() != 0 )
				xml << "<ul>\n";
			writeCategoryStructure( xml, child_it );
			if ( categoryTree->parent() != 0 )
				xml << "</ul>\n";
		}
	
		if ( categoryTree->category.id != -1 )
			xml << "\t</li>\n";
	}
}
