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

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

TrackPoint::TrackPoint() :
	DbItem("trackpoint",
		QStringList() << "id" << "track" << "ts" << "latitude" << "longitude" << "speed" << "altitude" << "heart_rate" << "cadence"),
	m_Track(NULL),
	m_TimeStamp(),
	m_Position(),
	m_Speed(0),
	m_Altitude(0),
	m_HeartRate(0),
	m_Cadence(0)
{
}

TrackPoint::TrackPoint(Track* track, const QSqlQuery& query) :
	DbItem(query.value(0).toULongLong(), "trackpoint",
		QStringList() << "id" << "track" << "ts" << "latitude" << "longitude" << "speed" << "altitude" << "heart_rate" << "cadence"),
	m_Track(track),
	m_TimeStamp(query.value(2).toDateTime()),
	m_Position(Position(query.value(3).toDouble(), query.value(4).toDouble())),
	m_Speed(query.value(5).toUInt()),
	m_Altitude(query.value(6).toInt()),
	m_HeartRate(query.value(7).toUInt()),
	m_Cadence(query.value(8).toUInt())
{
	m_TimeStamp.setTimeSpec(Qt::UTC);
}

bool TrackPoint::serialize()
{
	Q_ASSERT(m_Track);

	if (m_Track == NULL)
	{
		qWarning() << "tried to serialize a trackpoing that has no track";
		return false;
	}

	if (isRemoved())
		return remove();

	return DbItem::serialize(
		QVariantList() <<
			id() << m_Track->id() << m_TimeStamp << m_Position.latitude() << m_Position.longitude() <<
			m_Speed << m_Altitude << m_HeartRate << m_Cadence
	);
}

bool TrackPoint::list(Track* track, TrackPoints& result)
{
	QSqlQuery query;

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

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

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

	while (query.next())
		result.append(new TrackPoint(track, query));

	return true;
}
