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

#include "util/gpsutil.h"

#include <QDebug>

const double Position::Invalid = -999;

Position::Position() :
	m_Valid(false),
	m_Latitude(Invalid),
	m_Longitude(Invalid)
{
}

Position::Position(double lat, double lon) :
	m_Valid(lat != Invalid && lon != Invalid),
	m_Latitude(lat),
	m_Longitude(lon)
{
}

qint32 Position::distanceTo(const Position& other) const
{
	Q_ASSERT(isValid());
	Q_ASSERT(other.isValid());

	if (!isValid() || !other.isValid())
	{
		qWarning() << "not all positions valid: " << isValid() << other.isValid();
		return 0;
	}

	double lat1 = deg2Rad(latitude());
	double lon1 = deg2Rad(longitude());

	double lat2 = deg2Rad(other.latitude());
	double lon2 = deg2Rad(other.longitude());

	return rad2Meters(gcDist(lat1, lon1, lat2, lon2)) * 1000.0;
}
