/**********************************************************************
 * 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 "historysyncer.h"
#include <QFutureSynchronizer>
#include <QtDebug>
#include <util/sll/slotclosure.h>
#include <util/sll/either.h>
#include <util/sll/prelude.h>
#include <util/sll/visitor.h>
#include <util/sll/qtutil.h>
#include <util/threads/futures.h>
#include "interfaces/azoth/iaccount.h"
#include "interfaces/azoth/ihistoryplugin.h"
#include "interfaces/azoth/ihaveserverhistory.h"
#include "core.h"

namespace LeechCraft
{
namespace Azoth
{
	HistorySyncer::HistorySyncer (QObject *parent)
	: QObject { parent }
	{
	}

	void HistorySyncer::AddStorage (IHistoryPlugin *storage)
	{
		Storages_ << storage;
	}

	namespace
	{
		bool IsOnline (State st)
		{
			switch (st)
			{
			case SOffline:
			case SError:
			case SConnecting:
			case SInvalid:
				return false;
			default:
				return true;
			}
		}
	}

	void HistorySyncer::AddAccount (IAccount *acc)
	{
		const auto ihsh = qobject_cast<IHaveServerHistory*> (acc->GetQObject ());
		if (!ihsh)
			return;

		const auto accObj = acc->GetQObject ();
		new Util::SlotClosure<Util::NoDeletePolicy>
		{
			[this, acc]
			{
				if (!IsOnline (acc->GetState ().State_))
					CurrentlyOnline_.remove (acc);
				else if (!CurrentlyOnline_.contains (acc))
				{
					CurrentlyOnline_ << acc;
					StartAccountSync (acc);
				}
			},
			accObj,
			SIGNAL (statusChanged (EntryStatus)),
			accObj
		};
	}

	void HistorySyncer::StartAccountSync (IAccount *acc)
	{
		qDebug () << Q_FUNC_INFO
				<< acc->GetAccountID ();

		if (Storages_.isEmpty ())
		{
			qDebug () << Q_FUNC_INFO
					<< "no history storage plugins are loaded";
			return;
		}

		using RetType_t = Util::UnwrapFutureType_t<decltype (Storages_ [0]->RequestMaxTimestamp (acc))>;

		auto allStorages = std::make_shared<QFutureSynchronizer<RetType_t>> ();
		for (const auto& storage : Storages_)
			allStorages->addFuture (storage->RequestMaxTimestamp (acc));

		Util::Sequence (this, QtConcurrent::run ([allStorages] { allStorages->waitForFinished (); })) >>
				[=]
				{
					const auto& results = Util::Map (allStorages->futures (),
							[] (auto future) { return future.result (); });

					const auto& partition = Util::PartitionEithers (results);
					if (!partition.first.isEmpty ())
					{
						qWarning () << Q_FUNC_INFO
								<< "got storage errors:"
								<< partition.first
								<< "; aborting sync";
						return;
					}

					const auto& minDate = *std::min_element (partition.second.begin (), partition.second.end ());
					RequestAccountFrom (acc, minDate);
				};
	}

	void HistorySyncer::RequestAccountFrom (IAccount *acc, const QDateTime& from)
	{
		qDebug () << Q_FUNC_INFO
				<< acc->GetAccountID ()
				<< from;

		const auto ihsh = qobject_cast<IHaveServerHistory*> (acc->GetQObject ());
		Util::Sequence (this, ihsh->FetchServerHistory (from)) >>
				[this, acc] (const auto& res)
				{
					Util::Visit (res.AsVariant (),
							[] (const QString& err) { qWarning () << Q_FUNC_INFO << err; },
							[this, acc] (const auto& map) { this->AppendItems (acc, map); });		// workaround gcc crappiness
				};
	}

	void HistorySyncer::AppendItems (IAccount *acc,
			const IHaveServerHistory::MessagesSyncMap_t& map)
	{
		for (const auto& pair : Util::Stlize (map))
		{
			const auto& entry = qobject_cast<ICLEntry*> (Core::Instance ().GetEntry (pair.first));
			const auto& name = entry ?
					entry->GetEntryName () :
					pair.second.VisibleName_;

			for (const auto storage : Storages_)
				storage->AddRawMessages (acc->GetAccountID (),
						pair.first, name, pair.second.Messages_);
		}
	}
}
}
