/**********************************************************************
 * 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.
 **********************************************************************/

#include "opmlparser.h"
#include <QFile>
#include <QObject>
#include <QDomDocument>
#include <QDomElement>
#include <util/sll/domchildrenrange.h>
#include <util/sll/either.h>
#include <util/sll/functor.h>

namespace LC
{
namespace Aggregator
{
	OPMLParser::OPMLParser (const QDomDocument& document)
	: Document_ (document)
	{
	}
	
	bool OPMLParser::IsValid ()
	{
		auto root = Document_.documentElement ();
		if (root.tagName () != "opml")
			return false;
	
		auto heads = root.elementsByTagName ("head");
		if (heads.size () != 1 || !heads.at (0).isElement ())
			return false;
	
		auto bodies = root.elementsByTagName ("body");
		if (bodies.size () != 1 || !bodies.at (0).isElement ())
			return false;

		return !bodies.at (0).toElement ().elementsByTagName ("outline").isEmpty ();

	}
	
	OPMLParser::OPMLinfo_t OPMLParser::GetInfo ()
	{
		OPMLinfo_t result;
	
		auto entries = Document_.documentElement ().firstChildElement ("head").childNodes ();
	
		for (int i = 0; i < entries.size (); ++i)
		{
			auto elem = entries.at (i).toElement ();
			if (elem.isNull ())
				continue;
	
			result [elem.tagName ()] = elem.text ();
		}
	
		return result;
	}
	
	OPMLParser::items_container_t OPMLParser::Parse ()
	{
		if (!CacheValid_)
		{
			Items_.clear ();

			auto body = Document_.documentElement ().firstChildElement ("body");
			for (const auto& outline : Util::DomChildren (body, "outline"))
				ParseOutline (outline);

			CacheValid_ = true;
		}
	
		return Items_;
	}
	
	void OPMLParser::ParseOutline (const QDomElement& parentOutline, QStringList previousStrings)
	{
		if (parentOutline.hasAttribute ("xmlUrl"))
		{
			OPMLItem item;
			item.URL_ = parentOutline.attribute ("xmlUrl");
			item.HTMLUrl_ = parentOutline.attribute ("htmlUrl");
			item.Title_ = parentOutline.attribute ("title");
			item.CustomFetchInterval_ = parentOutline.attribute ("useCustomFetchInterval") == "true";
			item.MaxArticleAge_ = parentOutline.attribute ("maxArticleAge").toInt ();
			item.FetchInterval_ = parentOutline.attribute ("fetchInterval").toInt ();
			item.MaxArticleNumber_ = parentOutline.attribute ("maxArticleNumber").toInt ();
			item.Description_ = parentOutline.attribute ("description");
			item.Categories_ = previousStrings;
	
			Items_.push_back (item);
		}
	
		if (parentOutline.attribute ("text").size ())
			previousStrings << parentOutline.attribute ("text");

		for (const auto& outline : Util::DomChildren (parentOutline, "outline"))
			ParseOutline (outline, previousStrings);
	}

	OPMLParseResult_t ParseOPML (const QString& filename)
	{
		QFile file { filename };
		if (!file.open (QIODevice::ReadOnly))
			return OPMLParseResult_t::Left (QObject::tr ("Could not open file %1 for reading.")
					.arg (filename));

		QString errorMsg;
		int errorLine, errorColumn;
		QDomDocument document;
		if (!document.setContent (&file,
				true,
				&errorMsg,
				&errorLine,
				&errorColumn))
			return OPMLParseResult_t::Left (QObject::tr ("XML error, file %1, line %2, column %3, error:<br />%4")
					.arg (filename)
					.arg (errorLine)
					.arg (errorColumn)
					.arg (errorMsg));

		OPMLParser parser { document };
		if (!parser.IsValid ())
			return OPMLParseResult_t::Left (QObject::tr ("OPML from file %1 is not valid.")
					.arg (filename));

		return OPMLParseResult_t::Right (parser);
	}

	OPMLItemsResult_t ParseOPMLItems (const QString& filename)
	{
		return ParseOPML (filename) * [] (auto parser) { return parser.Parse (); };
	}
}
}
