/**********************************************************************
 * 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 "contextwrapper.h"
#include <QSortFilterProxyModel>
#include <QSettings>
#include <QStringList>
#include <QCoreApplication>
#include <QtDebug>
#include <util/qml/unhidelistviewbase.h>
#include <util/qml/unhidelistmodel.h>
#include <util/gui/autoresizemixin.h>
#include <util/models/rolenamesmixin.h>
#include "plotmanager.h"
#include "sensorsgraphmodel.h"

namespace LC
{
namespace HotSensors
{
	class SensorsFilterModel : public Util::RoleNamesMixin<QSortFilterProxyModel>
	{
		QStringList Hidden_;
	public:
		SensorsFilterModel (QObject *parent)
		: RoleNamesMixin<QSortFilterProxyModel> (parent)
		{
		}

		void SetSourceWithRoles (QAbstractItemModel *model)
		{
			setSourceModel (model);
			setRoleNames (model->roleNames ());
		}

		void SetHidden (const QStringList& hidden)
		{
			Hidden_ = hidden;
			invalidateFilter ();
		}

	protected:
		bool filterAcceptsRow (int row, const QModelIndex&) const
		{
			const auto& idx = sourceModel ()->index (row, 0);
			return !Hidden_.contains (idx.data (SensorsGraphModel::Role::SensorName).toString ());
		}
	};

	ContextWrapper::ContextWrapper (QAbstractItemModel *model, ICoreProxy_ptr proxy, QObject *parent)
	: QObject (parent)
	, Proxy_ (proxy)
	, Filter_ (new SensorsFilterModel (this))
	{
		Filter_->setDynamicSortFilter (true);
		Filter_->SetSourceWithRoles (model);
	}

	QStringList ContextWrapper::LoadHiddenNames () const
	{
		QSettings settings (qApp->organizationName (), qApp->applicationName () + "_HotSensors");
		settings.beginGroup (Context_);
		const auto& list = settings.value ("Hidden").toStringList ();
		settings.endGroup ();
		return list;
	}

	void ContextWrapper::SaveHiddenNames (const QStringList& names) const
	{
		QSettings settings (qApp->organizationName (), qApp->applicationName () + "_HotSensors");
		settings.beginGroup (Context_);
		settings.setValue ("Hidden", names);
		settings.endGroup ();
	}

	void ContextWrapper::setContext (const QString& context)
	{
		Context_ = context;

		const auto& list = LoadHiddenNames ();
		Filter_->SetHidden (list);
	}

	QObject* ContextWrapper::getModel () const
	{
		return Filter_;
	}

	void ContextWrapper::sensorUnhideListRequested (int x, int y, const QRect& rect)
	{
		if (CurrentList_)
		{
			CurrentList_->deleteLater ();
			return;
		}

		QList<QStandardItem*> items;
		for (const auto& name : LoadHiddenNames ())
		{
			auto item = new QStandardItem;
			item->setData (name, Util::UnhideListModel::ItemClass);
			item->setData (name, Util::UnhideListModel::ItemName);
			item->setData (QUrl (), Util::UnhideListModel::ItemIcon);
			item->setData (QString (), Util::UnhideListModel::ItemDescription);
			items << item;
		}
		if (items.isEmpty ())
			return;

		auto list = new Util::UnhideListViewBase (Proxy_,
				[&items] (QStandardItemModel *model)
					{ model->invisibleRootItem ()->appendRows (items); });
		connect (list,
				SIGNAL (itemUnhideRequested (QString)),
				this,
				SLOT (unhideSensor (QString)));
		new Util::AutoResizeMixin ({ x, y }, [rect] () { return rect; }, list);
		list->show ();
		CurrentList_ = list;
	}

	void ContextWrapper::unhideSensor (const QString& name)
	{
		auto list = LoadHiddenNames ();
		if (!list.removeAll (name))
			return;

		SaveHiddenNames (list);
		Filter_->SetHidden (list);
	}

	void ContextWrapper::hideSensor (const QString& name)
	{
		auto list = LoadHiddenNames ();
		list << name;
		list.removeDuplicates ();
		SaveHiddenNames (list);

		Filter_->SetHidden (list);
	}
}
}
