/***************************************************************************
 *   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 "core/equipment.h"
#include "core/profile.h"

#include "util/helpers.h"

#include <QSqlQuery>
#include <QVariant>
#include <QSqlError>
#include <QComboBox>
#include <QDebug>

Equipment::Equipment(Profile* p, const QString& name) :
	DbItem("equipment",
		QStringList() << "id" << "profile" << "name" << "model" << "brand" <<
		"purchase_date" << "type" << "weight" << "in_use" << "notes" << "photo"),
	m_Profile(p),
	m_Name(name),
	m_Model(),
	m_Brand(),
	m_PurchaseDate(QDate::currentDate()),
	m_Type(),
	m_Weight(0),
	m_InUse(true),
	m_Notes(),
	m_Photo()
{
}

Equipment::Equipment(Profile* p, const QSqlQuery& query) :
	DbItem(query.value(0).toULongLong(), "equipment",
		QStringList() << "id" << "profile" << "name" << "model" << "brand" <<
		"purchase_date" << "type" << "weight" << "in_use" << "notes" << "photo"),
	m_Profile(p),
	m_Name(query.value(2).toString()),
	m_Model(query.value(3).toString()),
	m_Brand(query.value(4).toString()),
	m_PurchaseDate(query.value(5).toDate()),
	m_Type(query.value(6).toString()),
	m_Weight(query.value(7).toUInt()),
	m_InUse(query.value(8).toBool()),
	m_Notes(query.value(9).toString()),
	m_Photo()
{
	m_Photo.loadFromData(query.value(10).toByteArray());
}

bool Equipment::list(Profile* profile, Equipments& result)
{
	QSqlQuery query;

	if (!query.prepare("SELECT " + Equipment().columnNames().join(",") + " FROM " + Equipment().tableName() + " WHERE profile=?"))
	{
		qWarning() << "prepare failed : " << query.lastError().text();
		return false;
	}

	query.addBindValue(profile->id());

	if (!query.exec())
	{
		qWarning() << "exec failed : " << query.lastError().text();
		return false;
	}

	while (query.next())
		result.append(new Equipment(profile, query));

	return true;
}

bool Equipment::serialize()
{
	Q_ASSERT(m_Profile);

	if (m_Profile == NULL)
	{
		qWarning() << "attempt to serialize equipment without profile";
		return false;
	}

	if (isRemoved())
		return remove();

	if (!DbItem::serialize(
			QVariantList() << id() << m_Profile->id() << m_Name << m_Model << m_Brand << m_PurchaseDate << m_Type <<
				m_Weight << m_InUse << m_Notes << pixmapToByteArray(m_Photo))
	)
		return false;

	return true;
}

void Equipment::toComboBox(QComboBox* combo, const Equipments& eqs)
{
	combo->addItem("---", QVariant::fromValue<Equipment*>(NULL));

	foreach (Equipment* e, eqs)
	{
		if (e->isRemoved())
			continue;

		combo->addItem(e->name(), QVariant::fromValue<Equipment*>(e));
	}
}
