/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#include "gui/importdialogtree.h"

#include "gui/importmodel.h"

#include "gui/delegates/categorydelegate.h"
#include "gui/delegates/equipmentdelegate.h"

#include "core/profile.h"
#include "core/activity.h"
#include "core/category.h"
#include "core/equipment.h"

#include "veneer/datetime.h"

#include "util/unit.h"

#include <QSortFilterProxyModel>
#include <QHeaderView>
#include <QDebug>

ImportDialogTree::ImportDialogTree(QWidget* parent)
	: QTreeView(parent)
{
	connect(this, SIGNAL(activated(const QModelIndex&)), SLOT(onActivated(const QModelIndex&)));
}

void ImportDialogTree::init(Profile& profile, Activities& activities)
{
	ImportModel* importModel = new ImportModel(this, profile, activities);

	connect(importModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));

	QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);
	proxyModel->setSortRole(ImportModel::sortingRole);
	proxyModel->setDynamicSortFilter(true);
	proxyModel->setSourceModel(importModel);

	setModel(proxyModel);

	setItemDelegateForColumn(ImportModel::categoryCol, new CategoryDelegate(this, profile.categories()));
	setItemDelegateForColumn(ImportModel::equipmentCol, new EquipmentDelegate(this, profile.equipments()));

	header()->setSortIndicator(1, Qt::DescendingOrder);

	emit numSelectedChanged(sourceModel()->importActivities().size(), sourceModel()->activities().size());
}

ImportModel* ImportDialogTree::sourceModel()
{
	QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
	return static_cast<ImportModel*>(proxyModel->sourceModel());
}

const ImportModel* ImportDialogTree::sourceModel() const
{
	const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
	return static_cast<const ImportModel*>(proxyModel->sourceModel());
}

void ImportDialogTree::selectAllImports(bool)
{
	sourceModel()->selectAllImports();
}

void ImportDialogTree::unselectAllImports(bool)
{
	sourceModel()->unselectAllImports();
}

const QSet<const Activity*>& ImportDialogTree::importActivities() const
{
	return sourceModel()->importActivities();
}

void ImportDialogTree::setAllCategories(Category* c)
{
	sourceModel()->setAllCategories(c);
}

void ImportDialogTree::setAllEquipment(Equipment* e)
{
	sourceModel()->setAllEquipments(e);
}

void ImportDialogTree::onActivated(const QModelIndex& idx)
{
	const QModelIndex index = sourceModel()->index(static_cast<QSortFilterProxyModel*>(model())->mapToSource(idx).row(), 0);
	bool importChecked = sourceModel()->data(index).toBool();
	sourceModel()->setData(index, !importChecked);
}

void ImportDialogTree::onDataChanged(const QModelIndex& idx, const QModelIndex&)
{
	if (idx.column() == ImportModel::importCol)
		emit numSelectedChanged(sourceModel()->importActivities().size(), sourceModel()->activities().size());
}
