/**********************************************************************
 * 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 "feedserrormanager.h"
#include <interfaces/core/icoreproxy.h>
#include <interfaces/core/ientitymanager.h>
#include <util/gui/util.h>
#include <util/sll/visitor.h>
#include <util/xpc/downloaderrorstrings.h>
#include <util/xpc/util.h>
#include "storagebackendmanager.h"
#include "storagebackend.h"

namespace LC::Aggregator
{
	bool FeedsErrorManager::ParseError::operator== (const FeedsErrorManager::ParseError& other) const
	{
		return Error_ == other.Error_;
	}

	FeedsErrorManager::FeedsErrorManager (const ICoreProxy_ptr& proxy)
	: Proxy_ { proxy }
	{
	}

	namespace
	{
		auto MakeEventId (IDType_t id)
		{
			return "FeedID/" + QString::number (id);
		}
	}

	void FeedsErrorManager::AddFeedError (IDType_t id, const Error& error)
	{
		auto& errors = Errors_ [id];
		if (errors.contains (error))
			return;

		errors << error;
		emit gotErrors (id);

		struct ErrorInfo
		{
			QString Short_;
			QString Full_;
		};

		const auto& errInfo = Util::Visit (error,
				[] (const FeedsErrorManager::ParseError& e)
					{ return ErrorInfo { tr ("parse error"), tr ("Parse error: ") + e.Error_ }; },
				[] (const IDownload::Error& e)
					{ return ErrorInfo { Util::GetErrorString (e.Type_), e.Message_ }; });

		const auto& storage = StorageBackendManager::Instance ().MakeStorageBackendForThread ();
		const auto& channels = storage->GetChannels (id);
		const auto& reprName = channels.size () == 1 ?
				channels [0].Title_ :
				storage->GetFeed (id).URL_;

		auto e = Util::MakeAN ("Aggregator",
				tr ("Error updating feed %1: %2.")
					.arg (Util::FormatName (reprName))
					.arg (errInfo.Short_),
				Priority::Warning,
				"org.LeechCraft.Aggregator",
				AN::CatNews, AN::TypeNewsSourceBroken,
				MakeEventId (id),
				{},
				0, 1,
				errInfo.Full_);
		Proxy_->GetEntityManager ()->HandleEntity (e);
	}

	void FeedsErrorManager::ClearFeedErrors (IDType_t id)
	{
		if (!Errors_.remove (id))
			return;

		emit clearedErrors (id);

		Proxy_->GetEntityManager ()->HandleEntity (Util::MakeANCancel ("org.LeechCraft.Aggregator", MakeEventId (id)));
	}

	QList<FeedsErrorManager::Error> FeedsErrorManager::GetFeedErrors (IDType_t id) const
	{
		return Errors_.value (id);
	}
}
