/**********************************************************************
 * 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 "restoresessiondialog.h"
#include <interfaces/iinfo.h>

namespace LeechCraft
{
namespace TabSessManager
{
	RestoreSessionDialog::RestoreSessionDialog (QWidget *parent)
	: QDialog (parent)
	{
		Ui_.setupUi (this);
	}

	void RestoreSessionDialog::SetPages (const QHash<QObject*, QList<RecInfo>>& pages)
	{
		Q_FOREACH (QObject *obj, pages.keys ())
		{
			IInfo *ii = qobject_cast<IInfo*> (obj);
			auto parent = new QTreeWidgetItem (QStringList (ii->GetName ()));
			parent->setIcon (0, ii->GetIcon ());
			parent->setData (0, Qt::UserRole,
					QVariant::fromValue<QObject*> (obj));
			Ui_.Tabs_->addTopLevelItem (parent);

			Q_FOREACH (const RecInfo& info, pages [obj])
			{
				const auto& name = info.Name_.isEmpty () ?
						'<' + tr ("no name") + '>' :
						info.Name_;
				auto item = new QTreeWidgetItem (QStringList (name));
				item->setIcon (0, info.Icon_);
				item->setCheckState (0, Qt::Checked);
				item->setData (0, Qt::UserRole, QVariant::fromValue (info));
				parent->addChild (item);
			}

			Ui_.Tabs_->expandItem (parent);
		}
	}

	QHash<QObject*, QList<RecInfo>> RestoreSessionDialog::GetPages () const
	{
		QHash<QObject*, QList<RecInfo>> result;

		for (int i = 0, size = Ui_.Tabs_->topLevelItemCount ();
				i < size; ++i)
		{
			auto parent = Ui_.Tabs_->topLevelItem (i);

			QList<RecInfo> infos;
			for (int j = 0, jsize = parent->childCount ();
				j < jsize; ++j)
			{
				auto child = parent->child (j);

				if (child->checkState (0) != Qt::Checked)
					continue;

				infos << child->data (0, Qt::UserRole).value<RecInfo> ();
			}

			result [parent->data (0, Qt::UserRole).value<QObject*> ()] = infos;
		}

		return result;
	}

	void RestoreSessionDialog::CheckAll (Qt::CheckState state)
	{
		for (int i = 0, size = Ui_.Tabs_->topLevelItemCount ();
				i < size; ++i)
		{
			auto parent = Ui_.Tabs_->topLevelItem (i);
			for (int j = 0, jsize = parent->childCount (); j < jsize; ++j)
				parent->child (j)->setCheckState (0, state);
		}
	}

	void RestoreSessionDialog::on_SelectAll__released ()
	{
		CheckAll (Qt::Checked);
	}

	void RestoreSessionDialog::on_SelectNone__released ()
	{
		CheckAll (Qt::Unchecked);
	}
}
}
