//----------------------------------------------------------------------------------
// <copyright file="LqrSMACLatLon.cpp" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
//   Implemetation of the LatLon class, which encapsulates a latitude/longitude pair
//   and provides methods for distance calculations and conversions.
// </summary>
//
// <owner>Mike Cookson</owner>
//----------------------------------------------------------------------------------

#include <sstream>
#include <stdexcept>
#include "LqrSMACLatLon.h"
#include "LqrSMACGeodetic.h"

using namespace std;

namespace LiquidRobotics {
namespace SMACFramework {

IMPLEMENT_INNER_EXCEPTION(LatLon, InvalidFix, std::runtime_error);

LatLon::LatLon()
: lat(true)
, lon(false)
{
}

LatLon::LatLon(FCCNumeric _latitude, FCCNumeric _longitude)
: lat(_latitude, true)
, lon(_longitude, false)
{
}

LatLon::LatLon(const Component& _latitude, const Component& _longitude)
: lat(_latitude, true)
, lon(_longitude, true)
{
	if (!lat.IsLatitude())	throw invalid_argument(string("The latitude component is not configured as latitude"));
	if (!lon.IsLongitude())	throw invalid_argument(string("The longitude component is not configured as longitude"));
}

LatLon::~LatLon()
{
}

bool LatLon::IsValid() const
{
	return lat.IsValid() && lon.IsValid();
}

void LatLon::Invalidate()
{
	lat.Invalidate();
	lon.Invalidate();
}

const LatLon::Component& LatLon::GetLatitude() const
{
	return lat;
}

void LatLon::SetLatitude(const Component& newValue)
{
	if (!newValue.IsLatitude())	throw invalid_argument(string("SetLatitude was passed a non-latitude value"));
	lat = newValue;
}

const LatLon::Component& LatLon::GetLongitude() const
{
	return lon;
}

void LatLon::SetLongitude(const Component& newValue)
{
	if (!newValue.IsLongitude())	throw invalid_argument(string("SetLongitude was passed a non-longitude value"));
	lon = newValue;
}

std::string LatLon::ToString() const
{
	stringstream stm;
	stm << *this;
	return stm.str();
}

double LatLon::ComputeDistance(const LatLon& other) const
{
	if (!IsValid() || !other.IsValid())
		throw invalid_argument(string("One or more end points are invalid"));

	Geodetic::Location pt1(GetLatitude().GetAsDouble(), GetLongitude().GetAsDouble());
	Geodetic::Location pt2(other.GetLatitude().GetAsDouble(), other.GetLongitude().GetAsDouble());
	Geodetic::BearingAndDistance bd = pt1.CalculateBearingAndDistanceTo(pt2);
	const double result = 1000.0*bd.distance;
	return result;
}

SHARED_LIB_PUBLIC std::ostream& operator<<(std::ostream& out, const LatLon& position)
{
	out << "(" << position.GetLatitude() << "," << position.GetLongitude() << ")";
	return out;
}

}
}
