/***************************************************************************
 *   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/importdialog.h"
#include "ui_importdialog.h"

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

#include "veneer/settings.h"

#include "util/unit.h"
#include "util/helpers.h"

#include <QCheckBox>
#include <QComboBox>

ImportDialog::ImportDialog(QWidget* parent) :
	QDialog(parent),
	m_Profile(NULL),
	m_Activities(NULL)
{
	setupUi(this);

	setupConnections();

	Veneer::restoreDialogSize("importDialog", this);
}

ImportDialog::~ImportDialog()
{
	Veneer::saveDialogSize("importDialog", this);
}

void ImportDialog::setProfileAndActivities(Profile* profile, Activities* activities)
{
	m_Profile = profile;
	m_Activities = activities;

	if (m_Profile == NULL || m_Activities == NULL)
		return;

	importTree->init(*profile, *activities);
	Category::toComboBox(allCategoriesCombo, profile->categories());
	Equipment::toComboBox(allEquipmentCombo, profile->equipments());

	loadTreeWidgetHeaderConfig(importTree, Veneer::Settings::self()->importTreeColumnWidths(), Veneer::Settings::self()->importTreeColumnPositions(), Veneer::Settings::self()->importTreeColumnVisible());
}

void ImportDialog::setupConnections()
{
	connect(selectAllButton, SIGNAL(clicked(bool)), importTree, SLOT(selectAllImports(bool)));
	connect(unselectAllButton, SIGNAL(clicked(bool)), importTree, SLOT(unselectAllImports(bool)));
	connect(allCategoriesCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAllCatergoriesIndexChanged(int)));
	connect(allEquipmentCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAllEquipmentIndexChanged(int)));
	connect(importTree, SIGNAL(numSelectedChanged(int, int)), SLOT(onNumSelectionChanged(int, int)));
}

void ImportDialog::accept()
{
	Activities::iterator i = m_Activities->begin();
	while (i != m_Activities->end())
	{
		if (!importTree->importActivities().contains(*i))
		{
			delete *i;
			i = m_Activities->erase(i);
		}
		else
			i++;
	}

	Veneer::Settings::self()->setImportTreeColumnPositions(treeViewHeaderColPositions(importTree));
	Veneer::Settings::self()->setImportTreeColumnVisible(treeViewHeaderColVisible(importTree));
	Veneer::Settings::self()->setImportTreeColumnWidths(treeViewHeaderColWidths(importTree));

	QDialog::accept();
}

void ImportDialog::onAllCatergoriesIndexChanged(int idx)
{
	Category* c = allCategoriesCombo->itemData(idx, Qt::UserRole).value<Category*>();
	importTree->setAllCategories(c);
}

void ImportDialog::onAllEquipmentIndexChanged(int idx)
{
	Equipment* e= allEquipmentCombo->itemData(idx, Qt::UserRole).value<Equipment*>();
	importTree->setAllEquipment(e);
}

void ImportDialog::onNumSelectionChanged(int numSelected, int total)
{
	selectedLabel->setText(tr("%1 of %2 selected").arg(numSelected).arg(total));
}
