/**********************************************************************
 * 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 "flattenfiltermodel.h"

namespace LC
{
namespace Util
{
	FlattenFilterModel::FlattenFilterModel (QObject *parent)
	: QAbstractItemModel (parent)
	, Source_ (0)
	{
	}

	QModelIndex FlattenFilterModel::index (int row, int column, const QModelIndex& parent) const
	{
		if (parent.isValid ())
			return QModelIndex ();

		return createIndex (row, column);
	}

	QModelIndex FlattenFilterModel::parent (const QModelIndex&) const
	{
		return QModelIndex ();
	}

	int FlattenFilterModel::rowCount (const QModelIndex& parent) const
	{
		return parent.isValid () ? 0 : SourceIndexes_.size ();
	}

	int FlattenFilterModel::columnCount (const QModelIndex& parent) const
	{
		return parent.isValid () ? 0 : 1;
	}

	QVariant FlattenFilterModel::data (const QModelIndex& index, int role) const
	{
		return SourceIndexes_.value (index.row ()).data (role);
	}

	void FlattenFilterModel::SetSource (QAbstractItemModel *model)
	{
		if (Source_)
		{
			disconnect (Source_,
					SIGNAL (rowsInserted (QModelIndex, int, int)),
					this,
					SLOT (handleRowsInserted (QModelIndex, int, int)));
			disconnect (Source_,
					SIGNAL (rowsAboutToBeRemoved (QModelIndex, int, int)),
					this,
					SLOT (handleRowsAboutRemoved (QModelIndex, int, int)));
			disconnect (Source_,
					SIGNAL (dataChanged (QModelIndex, QModelIndex)),
					this,
					SLOT (handleDataChanged (QModelIndex, QModelIndex)));
		}

		beginResetModel ();
		SourceIndexes_.clear ();
		Source_ = model;
		endResetModel ();
		connect (Source_,
				SIGNAL (rowsInserted (QModelIndex, int, int)),
				this,
				SLOT (handleRowsInserted (QModelIndex, int, int)));
		connect (Source_,
				SIGNAL (rowsAboutToBeRemoved (QModelIndex, int, int)),
				this,
				SLOT (handleRowsAboutRemoved (QModelIndex, int, int)));
		connect (Source_,
				SIGNAL (dataChanged (QModelIndex, QModelIndex)),
				this,
				SLOT (handleDataChanged (QModelIndex, QModelIndex)));
	}

	bool FlattenFilterModel::IsIndexAccepted (const QModelIndex&) const
	{
		return true;
	}

	void FlattenFilterModel::handleDataChanged (const QModelIndex& top, const QModelIndex& bottom)
	{
		const auto& parent = top.parent ();
		for (int i = top.row (); i <= bottom.row (); ++i)
		{
			const auto& child = Source_->index (i, 0, parent);
			const int pos = SourceIndexes_.indexOf (child);
			if (pos < 0)
				continue;

			const auto& ourIdx = index (pos, 0);
			emit dataChanged (ourIdx, ourIdx);
		}
	}

	void FlattenFilterModel::handleRowsInserted (const QModelIndex& parent, int start, int end)
	{
		for (int i = start; i <= end; ++i)
		{
			const auto& child = Source_->index (i, 0, parent);
			if (IsIndexAccepted (child))
			{
				beginInsertRows (QModelIndex (), SourceIndexes_.size (), SourceIndexes_.size ());
				SourceIndexes_ << child;
				endInsertRows ();
			}

			if (int rc = Source_->rowCount (child))
				handleRowsInserted (child, 0, rc - 1);
		}
	}

	void FlattenFilterModel::handleRowsAboutRemoved (const QModelIndex& parent, int start, int end)
	{
		for (int i = start; i <= end; ++i)
		{
			const auto& child = Source_->index (i, 0, parent);
			const int pos = SourceIndexes_.indexOf (child);

			if (pos >= 0)
			{
				beginRemoveRows (QModelIndex (), pos, pos);
				SourceIndexes_.removeAt (pos);
				endRemoveRows ();
			}

			if (int rc = Source_->rowCount (child))
				handleRowsAboutRemoved (child, 0, rc - 1);
		}
	}
}
}
