/***************************************************************************
 *   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 "fileexport/gpxexporter.h"

#include "core/activity.h"
#include "core/lap.h"
#include "core/track.h"
#include "core/trackpoint.h"

#include <QXmlStreamWriter>
#include <QDateTime>
#include <QDebug>

GpxExporter::GpxExporter() :
	ExporterInterface()
{
}

bool GpxExporter::canExport(const QString& suffix, const Activities& activities)
{
	return suffix == "gpx" && activities.size() == 1;
}

bool GpxExporter::writeTrack(QXmlStreamWriter& xml, const Track& trk)
{
	foreach(const TrackPoint* tp, trk.trackPoints())
	{
		if (!tp->position().isValid())
			continue;

		xml.writeStartElement("trkpt");

		xml.writeAttribute("lat", QString::number(tp->position().latitude(), 'f', 9));
		xml.writeAttribute("lon", QString::number(tp->position().longitude(), 'f', 9));

		xml.writeTextElement("ele", QString::number(tp->altitude() / 1000.0));
		xml.writeTextElement("time", tp->timeStamp().toString(Qt::ISODate) + 'Z');

		xml.writeEndElement();
	}

	return true;
}

bool GpxExporter::writeLaps(QXmlStreamWriter& xml, const Activity& a)
{
	bool rval = true;

	foreach(const Lap* lap, a.laps())
	{
		foreach(const Track* track, lap->tracks())
		{
			xml.writeStartElement("trkseg");
			rval = writeTrack(xml, *track);
			xml.writeEndElement();

			if (!rval)
				break;
		}

		if (!rval)
			break;
	}

	return rval;
}

bool GpxExporter::writeRootElement(QXmlStreamWriter& xml, const Activities& activities)
{
	xml.writeStartElement("gpx");
	xml.writeAttribute("version", "1.0");
	xml.writeAttribute("creator", "Workout");
	xml.writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
	xml.writeAttribute("xmlns", "http://www.topografix.com/GPX/1/0");
	xml.writeAttribute("xsi:schemaLocation", "http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd");

	const Activity* a = activities.first();
	xml.writeTextElement("time", a->timeStamp().toString(Qt::ISODate) + 'Z');

	double minLon, minLat, maxLon, maxLat;
	if (a->calcBoundaries(minLat, minLon, maxLat, maxLon))
	{
		xml.writeStartElement("bounds");
		xml.writeAttribute("minlat", QString::number(minLat, 'f', 9));
		xml.writeAttribute("minlon", QString::number(minLon, 'f', 9));
		xml.writeAttribute("maxlat", QString::number(maxLat, 'f', 9));
		xml.writeAttribute("maxlon", QString::number(maxLon, 'f', 9));
		xml.writeEndElement();
	}

	return true;
}

bool GpxExporter::writeActivities(QXmlStreamWriter& xml, const Activities& activities)
{
	bool rval = true;

	xml.writeStartElement("trk");

	foreach(const Activity* a, activities)
	{
		rval = writeLaps(xml, *a);

		if (!rval)
			break;
	}

	xml.writeEndElement();

	return false;
}

QString GpxExporter::fileFormat() const
{
	return QObject::tr("*.gpx|GPX Exchange Format (*.gpx)");
}
