/**********************************************************************
 * 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 "photosproxymodel.h"
#include <QStringList>
#include <QUrl>
#include <QtDebug>
#include "interfaces/blasq/collection.h"
#include "interfaces/blasq/isupportdeletes.h"

namespace LeechCraft
{
namespace Blasq
{
	namespace
	{
		const int ItemsInCollage = 3;

		enum ExtendedRole
		{
			SupportsDeletes = CollectionRole::CollectionRoleMax,
			IsSelected
		};
	}

	PhotosProxyModel::PhotosProxyModel (QObject *parent)
	: NamedModel<QIdentityProxyModel> (parent)
	{
	}

	QVariant PhotosProxyModel::data (const QModelIndex& index, int role) const
	{
		if (role == ExtendedRole::SupportsDeletes)
			return SupportsDeletes_;
		else if (role == ExtendedRole::IsSelected)
			return Selected_.contains (index.data (CollectionRole::ID).toString ());

		const auto& srcIdx = mapToSource (index);
		const auto& srcData = srcIdx.data (role);
		if (!srcData.isNull ())
			return srcData;

		if (role != CollectionRole::SmallThumb)
			return srcData;

		const auto type = srcIdx.data (CollectionRole::Type).toInt ();
		if (type == ItemType::Image)
			return srcData;

		QVariantList result;
		for (int i = 0; i < std::min (ItemsInCollage, sourceModel ()->rowCount (srcIdx)); ++i)
		{
			const auto& photoIdx = sourceModel ()->index (i, 0, srcIdx);
			const auto& url = photoIdx.data (CollectionRole::SmallThumb);
			result << url;
		}
		std::reverse (result.begin (), result.end ());
		return result;
	}

	void PhotosProxyModel::setSourceModel (QAbstractItemModel *model)
	{
		if (sourceModel ())
			disconnect (sourceModel (),
					SIGNAL (rowsInserted (QModelIndex, int, int)),
					this,
					SLOT (handleRowsInserted (QModelIndex, int, int)));

		beginResetModel ();
		blockSignals (true);

		QIdentityProxyModel::setSourceModel (model);

		auto names = roleNames ();
		names [ExtendedRole::SupportsDeletes] = "supportsDeletes";
		names [ExtendedRole::IsSelected] = "isSelected";
		setRoleNames (names);

		blockSignals (false);
		endResetModel ();

		if (model)
			connect (model,
					SIGNAL (rowsInserted (QModelIndex, int, int)),
					this,
					SLOT (handleRowsInserted (QModelIndex, int, int)));

	}

	void PhotosProxyModel::SetCurrentAccount (QObject *accObj)
	{
		SupportsDeletes_ = qobject_cast<ISupportDeletes*> (accObj);
	}

	void PhotosProxyModel::AddSelected (const QString& id, const QModelIndexList& idxs)
	{
		Selected_ << id;
		EmitDataChanged (idxs);
	}

	void PhotosProxyModel::RemoveSelected (const QString& id, const QModelIndexList& idxs)
	{
		Selected_.remove (id);
		EmitDataChanged (idxs);
	}

	void PhotosProxyModel::ClearSelected ()
	{
		Selected_.clear ();
	}

	void PhotosProxyModel::EmitDataChanged (const QModelIndexList& idxs)
	{
		for (const auto& idx : idxs)
		{
			const auto& mapped = mapFromSource (idx);
			emit dataChanged (mapped, mapped);
		}
	}

	void PhotosProxyModel::handleRowsInserted (const QModelIndex& parent, int from, int)
	{
		if (from >= ItemsInCollage)
			return;

		emit dataChanged (parent, parent);
	}
}
}
