/***************************************************************************
 *   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(PROFILE__H)

#define PROFILE__H

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

#include <QDate>
#include <QList>
#include <QPixmap>

class Lap;

class QSqlQuery;

class Profile : public DbItem
{
	Q_DISABLE_COPY(Profile);

	public:
		enum Gender { Unknown = -1, Male = 0, Female = 1 };

	public:
		Profile(const QSqlQuery& query);
		Profile();
		~Profile();

	public:
		bool serialize();
		static bool list(Profiles& result);
		static Profile* unserialize(const QString& name);

		const QString& name() const { return m_Name; }
		const QDate& birthDate() const { return m_BirthDate; }
		Gender gender() const { return m_Gender; }
		quint32 height() const { return m_Height; }
		const QPixmap& photo() const { return m_Photo; }

		void setName(const QString& n) { m_Name = n; setDirty(true); }
		void setBirthDate(const QDate& b) { m_BirthDate = b; setDirty(true); }
		void setGender(Gender g) { m_Gender = g; setDirty(true); }
		void setHeight(quint32 h) { m_Height = h; setDirty(true); }
		void setPhoto(const QPixmap& p) { m_Photo = p; setDirty(true); }

		const Activities& activities() const { return m_Activities; }
		Activities& activities() { return m_Activities; }

		Categories& categories() { return m_Categories; }
		const Categories& categories() const { return m_Categories; }

		Equipments& equipments() { return m_Equipments; }
		const Equipments& equipments() const { return m_Equipments; }

		AthleteLogs& athleteLogs() { return m_AthleteLogs; }
		const AthleteLogs& athleteLogs() const { return m_AthleteLogs; }

		static quint32 calcEnergyConsumption(const Profile& profile, const Lap& lap);

		bool removeActivity(Activity* a);

		void setValidCategory(Activities& a) const;
		void calcEnergyConsumption(Activities& acts) const;
		void setValidName(Activities& a) const;
		void setValidStartTime(Activities& a) const;

		quint32 currentWeight() const;

		bool hasActivity(const Activity& a) const;

		double bmi(const AthleteLog& a) const;

		Category* rootCategory() { return m_Categories.size() > 0 ? m_Categories.first() : NULL; }
		const Category* rootCategory() const { return m_Categories.size() > 0 ? m_Categories.first() : NULL; }

		Equipment* findEquipment(quint64 id);
		Category* findCategory(quint64 id);

	protected:
		void clear();

	private:
		QString m_Name;
		QDate m_BirthDate;
		Gender m_Gender;
		quint32 m_Height; ///< in milli-meters
		QPixmap m_Photo;
		Activities m_Activities;
		AthleteLogs m_AthleteLogs;
		Categories m_Categories;
		Equipments m_Equipments;
};

#endif
