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

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

#include "veneer/iconloader.h"

#include <QDebug>

CategoriesDockWidget::CategoriesDockWidget(QWidget* parent) :
	QWidget(parent)
{
	setupUi(this);
}

void CategoriesDockWidget::on_categoriesTree_itemSelectionChanged()
{
	emit selectedCategoryChanged(selectedCategory());
}

Category* CategoriesDockWidget::selectedCategory() const
{
	if (categoriesTree->selectedItems().size() != 1)
		return NULL;

	return categoriesTree->selectedItems()[0]->data(0, Qt::UserRole).value<Category*>();
}

void buildCategoryItemMap(QTreeWidget* tree, const Category* oldCategory, Categories& cats, QMap<const Category*, QTreeWidgetItem*>& items, QTreeWidgetItem* parentItem = NULL)
{
	foreach(Category* c, cats)
	{
		if (!c->isRemoved())
		{
			QTreeWidgetItem* cItem = new QTreeWidgetItem();
			cItem->setText(0, c->name());
			cItem->setIcon(0, BarIcon("folder"));
			cItem->setData(0, Qt::UserRole, QVariant::fromValue(c));

			if (parentItem == NULL)
				tree->addTopLevelItem(cItem);
			else
				parentItem->addChild(cItem);

			cItem->setExpanded(true);

			items.insert(c, cItem);

			if (oldCategory == c)
				tree->setCurrentItem(cItem);

			buildCategoryItemMap(tree, oldCategory, c->subCategories(), items, cItem);
		}
	}
}

void CategoriesDockWidget::updateCategoryTree(Profile* profile)
{
	const Category* oldCategory = selectedCategory();

	categoriesTree->clear();

	if (profile == NULL)
		return;

	QMap<const Category*, QTreeWidgetItem*> categoryItems;
	buildCategoryItemMap(categoriesTree, oldCategory, profile->categories(), categoryItems);

	if (categoriesTree->topLevelItemCount() > 0 && categoriesTree->selectedItems().size() == 0)
		categoriesTree->setCurrentItem(categoriesTree->topLevelItem(0));

	categoriesTree->sortItems(0, Qt::AscendingOrder);

	foreach(QTreeWidgetItem* item, categoryItems)
		item->sortChildren(0, Qt::AscendingOrder);
}
