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

#include "util/helpers.h"

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

AthleteLog::AthleteLog() :
	DbItem("athletelog", QStringList() << "id" << "profile" << "date" << "weight" <<
			"body_fat_percent" << "resting_heart_rate" << "max_heart_rate" <<
			"blood_pressure_systolic" << "blood_pressure_diastolic" << "energy_consumption" <<
			"sleep_duration" << "notes" << "photo"),
	m_Profile(NULL),
	m_Date(),
	m_Weight(0),
	m_BodyFatPercent(0),
	m_RestingHeartRate(0),
	m_MaxHeartRate(0),
	m_BloodPressureSystolic(0),
	m_BloodPressureDiastolic(0),
	m_EnergyConsumption(0),
	m_SleepDuration(0),
	m_Notes(),
	m_Photo()
{
}

AthleteLog::AthleteLog(const QSqlQuery& query, Profile* p) :
	DbItem(query.value(0).toULongLong(),
			"athletelog", QStringList() << "id" << "profile" << "date" << "weight" <<
			"body_fat_percent" << "resting_heart_rate" << "max_heart_rate" <<
			"blood_pressure_systolic" << "blood_pressure_diastolic" << "energy_consumption" <<
			"sleep_duration" << "notes" << "photo"),
	m_Profile(p),
	m_Date(query.value(2).toDate()),
	m_Weight(query.value(3).toUInt()),
	m_BodyFatPercent(query.value(4).toUInt()),
	m_RestingHeartRate(query.value(5).toUInt()),
	m_MaxHeartRate(query.value(6).toUInt()),
	m_BloodPressureSystolic(query.value(7).toUInt()),
	m_BloodPressureDiastolic(query.value(8).toUInt()),
	m_EnergyConsumption(query.value(9).toUInt()),
	m_SleepDuration(query.value(10).toUInt()),
	m_Notes(query.value(11).toString()),
	m_Photo()
{
	m_Photo.loadFromData(query.value(12).toByteArray());
}

bool AthleteLog::list(Profile* profile, AthleteLogs& result)
{
	QSqlQuery query;

	if (!query.prepare("SELECT " + AthleteLog().columnNames().join(",") + " FROM " + AthleteLog().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 AthleteLog(query, profile));

	return true;
}

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

	if (m_Profile == NULL)
	{
		qDebug() << "attempt to serialize an athlete log without a profile";
		return false;
	}

	if (isRemoved())
		return remove();

	if (!DbItem::serialize(
			QVariantList() << id() << m_Profile->id() << m_Date << m_Weight << m_BodyFatPercent <<
			m_RestingHeartRate << m_MaxHeartRate << m_BloodPressureSystolic << m_BloodPressureDiastolic <<
			m_EnergyConsumption << m_SleepDuration << m_Notes << pixmapToByteArray(m_Photo))
	)
		return false;

	return true;
}
