//----------------------------------------------------------------------------------
// <copyright file="LqrSMACLatLon.h" 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>
//   Declarations for the LatLon class, which encapsulates a latitude/longitude pair
//   and provides methods for distance calculations and conversions.
// </summary>
//
// <owner>Mike Cookson</owner>
//----------------------------------------------------------------------------------

#ifndef __LqrSMACLatLon_h__
#define __LqrSMACLatLon_h__

#include <iostream>
#include <string>
#include "LqrSMACSharedLib.h"
#include "LqrSMACDeclareException.h"

namespace LiquidRobotics {
namespace SMACFramework {

class SHARED_LIB_PUBLIC LatLon
{
public:

	typedef long FCCNumeric;

	typedef enum EHemisphere
	{
		NORTH,
		SOUTH,
		EAST,
		WEST
	} Hemisphere;

	DECLARE_EXCEPTION(InvalidFix, std::runtime_error);

	class SHARED_LIB_PUBLIC Component
	{
	public:

		Component(bool latitude = true, bool locked = true);
		Component(FCCNumeric numeric, bool latitude = true, bool locked = true);
		Component(unsigned _degrees, unsigned _minutes, float _seconds, Hemisphere _hemisphere, bool locked = true);
		Component(const Component& right, bool locked = true);

		virtual ~Component();

		Component& operator=(const Component& right);

		bool IsValid() const;
		void Invalidate();

		bool IsLatitude() const;
		bool IsLongitude() const;

		Hemisphere GetHemisphere() const;
		void SetHemisphere(Hemisphere newValue);

		FCCNumeric GetNumeric() const;
		void SetNumeric(FCCNumeric newValue);

		unsigned GetDegrees() const;
		void SetDegrees(unsigned newValue);

		unsigned GetMinutes() const;
		void SetMinutes(unsigned newValue);

		float GetMinutesAsFloat() const;
		void SetMinutesAsFloat(float newValue);

		float GetSeconds() const;
		void SetSeconds(float newValue);

		void SetAsDouble(double newValue);
		double GetAsDouble() const;

		std::string ToString() const;

		static void ComputeComponents(FCCNumeric numeric, bool islatitude, unsigned& degrees, unsigned& minutes, float& seconds, Hemisphere& hemisphere);
		static void ComputeNumeric(unsigned degrees, unsigned minutes, float seconds, Hemisphere hemisphere, FCCNumeric& numeric);

	private:

		bool isValid;
		bool isLatitude;
		bool lockedHemisphere;
		Hemisphere hemisphere;
		unsigned degrees;
		unsigned minutes;
		float seconds;
		FCCNumeric numeric;
	};

	LatLon();
	LatLon(FCCNumeric _latitude, FCCNumeric _longitude);
	LatLon(const Component& _latitude, const Component& _longitude);

	virtual ~LatLon();

	// computes the distance to another point (in meters)
	double ComputeDistance(const LatLon& other) const;

	bool IsValid() const;
	void Invalidate();

	const Component& GetLatitude() const;
	void SetLatitude(const Component& newValue);

	const Component& GetLongitude() const;
	void SetLongitude(const Component& newValue);

	std::string ToString() const;

private:

	Component lat;
	Component lon;
};

SHARED_LIB_PUBLIC std::ostream& operator<<(std::ostream& out, const LatLon::Component& comp);
SHARED_LIB_PUBLIC std::ostream& operator<<(std::ostream& out, const LatLon& position);

}
}

#endif
