package com.github.fluidsonic.jetpack


public data class GeoCoordinate(
	public val latitude: Double,
	public val longitude: Double
) {
	/**
	 * Computes the distance between two geo coordinates in meters.

	 * @param coordinate the other coordinate
	 * *
	 * @return the distance between this and the other geo coordinates in meters.
	 */
	public fun distanceTo(coordinate: GeoCoordinate): Double {
		val sinHalfLatitudeDistance = Math.sin(Math.toRadians(coordinate.latitude - latitude) * 0.5)
		val sinHalfLongitudeDistance = Math.sin(Math.toRadians(coordinate.longitude - longitude) * 0.5)
		val a = sinHalfLatitudeDistance * sinHalfLatitudeDistance + Math.cos(Math.toRadians(latitude)) * Math.cos(Math.toRadians(coordinate.latitude)) * sinHalfLongitudeDistance * sinHalfLongitudeDistance
		val b = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))

		return EARTH_RADIUS * b
	}


	public val isInNorthernHemisphere: Boolean
		get() = latitude > 0.0


	public val isInSouthernHemisphere: Boolean
		get() = latitude < 0.0


	public val isOnEquator: Boolean
		get() = latitude == 0.0


	public override fun toString()
		= "$latitude,$longitude"


	public companion object {

		private val EARTH_RADIUS = 6378137.0 // in meters
	}
}
