/**********************************************************************
 * 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 "accountfoldermanager.h"
#include <stdexcept>
#include <util/sll/qtutil.h>
#include "common.h"

namespace LeechCraft
{
namespace Snails
{
	AccountFolderManager::AccountFolderManager (QObject *parent)
	: QObject (parent)
	{
	}

	QList<Folder> AccountFolderManager::GetFolders () const
	{
		return Folders_;
	}

	QList<QStringList> AccountFolderManager::GetFoldersPaths() const
	{
		QList<QStringList> result;
		result.reserve (Folders_.size ());
		for (const auto& folder : Folders_)
			result << folder.Path_;
		return result;
	}

	QList<QStringList> AccountFolderManager::GetSyncFolders () const
	{
		QList<QStringList> result;
		for (const auto& folder : Folders_)
			if (Folder2Flags_ [folder.Path_] & FolderSyncable)
				result << folder.Path_;
		return result;
	}

	AccountFolderManager::FolderFlags AccountFolderManager::GetFolderFlags (const QStringList& folder) const
	{
		return Folder2Flags_ [folder];
	}

	void AccountFolderManager::ClearFolderFlags ()
	{
		Folder2Flags_.clear ();
	}

	void AccountFolderManager::AppendFolderFlags (const QStringList& folder, FolderFlag flags)
	{
		Folder2Flags_ [folder] |= flags;
	}

	void AccountFolderManager::SetFolders (const QList<Folder>& folders)
	{
		if (folders == Folders_)
			return;

		Folders_ = folders;

		emit foldersUpdated ();
	}

	QByteArray AccountFolderManager::Serialize () const
	{
		QByteArray result;

		QHash<QStringList, int> flags;
		for (const auto& pair : Util::Stlize (Folder2Flags_))
			flags [pair.first] = pair.second;

		QDataStream out (&result, QIODevice::WriteOnly);
		out << static_cast<quint8> (1);
		out << Folders_
			<< flags;

		return result;
	}

	void AccountFolderManager::Deserialize (const QByteArray& arr)
	{
		QDataStream in (arr);
		quint8 version = 0;
		in >> version;

		if (version < 1 || version > 1)
			throw std::runtime_error (qPrintable ("Unknown folder manager version " + QString::number (version)));

		QHash<QStringList, int> flags;
		in >> Folders_
			>> flags;

		Folders_.erase (std::remove_if (Folders_.begin (), Folders_.end (),
					[] (const Folder& folder)
					{
						return folder.Path_.isEmpty () ||
								std::all_of (folder.Path_.begin (), folder.Path_.end (),
										[] (const QString& elem) { return elem.isEmpty (); });
					}),
				Folders_.end ());

		for (const auto& pair : Util::Stlize (flags))
			Folder2Flags_ [pair.first] = static_cast<FolderFlags> (pair.second);
	}
}
}
