/***************************************************************************
 *   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            *
 ***************************************************************************/

#if !defined(LAP__H)

#define LAP__H

#include "core/types.h"
#include "core/dbitem.h"

#include <qglobal.h>
#include <QList>
#include <QDateTime>

class QSqlQuery;

class Activity;
class Track;

class Lap : public DbItem
{
	Q_DISABLE_COPY(Lap);

	friend class Activity;
	friend class Track;

	public:
		Lap(Activity* a, const QSqlQuery& query);
		Lap();
		~Lap();

	public:
		bool serialize();
		static bool list(Activity* a, Laps& result);

		const QDateTime& startTime() const { return m_StartTime; }
		quint32 duration() const { return m_Duration; }
		quint32 distance() const { return m_Distance; }
		quint32 maximumSpeed() const { return m_MaximumSpeed; }
		quint32 energyConsumption() const { return m_EnergyConsumption; }
		quint32 averageHeartRate() const { return m_AverageHeartRate; }
		quint32 maximumHeartRate() const { return m_MaximumHeartRate; }
		quint32 averageCadence() const { return m_AverageCadence; }
		quint32 ascent() const { return m_Ascent; }
		quint32 descent() const { return m_Descent; }

		void setStartTime(const QDateTime& s) { m_StartTime = s; setDirty(true); }
		void setDuration(quint32 d) { m_Duration = d; setDirty(true); }
		void setDistance(quint32 d) { m_Distance = d; setDirty(true); }
		void setMaximumSpeed(quint32 m) { m_MaximumSpeed = m; setDirty(true); }
		void setEnergyConsumption(quint32 e) { m_EnergyConsumption = e; setDirty(true); }
		void setAverageHeartRate(quint32 a) { m_AverageHeartRate = a; setDirty(true); }
		void setMaximumHeartRate(quint32 m) { m_MaximumHeartRate = m; setDirty(true); }
		void setAverageCadence(quint32 a) { m_AverageCadence = a; setDirty(true); }
		void setAscent(quint32 value) { m_Ascent = value; }
		void setDescent(quint32 value) { m_Descent = value; }

		quint32 calcAverageCadence() const;
		void calcHeartRates(quint32& avg, quint32& max) const;
		quint32 calcDistance() const;
		quint32 calcDuration() const;
		QDateTime calcStartTime() const;
		bool calcBoundaries(double& minLat, double& minLon, double& maxLat, double& maxLon) const;

		static quint32 calcMaxSpeed(const Lap& lap);
		static quint32 calcTotalAscent(const Lap& lap);
		static quint32 calcTotalDescent(const Lap& lap);

		void addTrack(Track* t);
		const Tracks& tracks() const { return m_Tracks; }
		void calcTrackPointSpeed();

	protected:
		const Activity* activity() const { return m_Activity; }
		void setActivity(Activity* a) { m_Activity = a; setDirty(true); }

		void clear();

	private:
		Activity* m_Activity;
		QDateTime m_StartTime;
		quint32 m_Duration; ///< in milli-seconds
		quint32 m_Distance; ///< in milli-meters
		quint32 m_MaximumSpeed; ///< in km/h * 1000
		quint32 m_EnergyConsumption; ///< in kJ
		quint32 m_AverageHeartRate;
		quint32 m_MaximumHeartRate;
		quint32 m_AverageCadence;
		quint32 m_Ascent; ///< in milli-meters
		quint32 m_Descent; ///< in milli-meters
		Tracks m_Tracks;
};

#endif
