<?php
/*
    Copyright (C) 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.

    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 3 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, see <http://www.gnu.org/licenses/>.
*/

namespace GnuBoat\Nmea\Nmea0183;

/**
 * @brief NMEA0183 RMC Naviation sentence
 *
 * Keep in mind, that the latitude() and longitude() is converted to +/- values
 */
class Nmea0183Rmc extends Nmea0183Sentence
{
	function __construct($talker, $checksum, $sentence)
	{
		parent::__construct("RMC", $talker, $checksum, $sentence);
		
		// init default values
		$this->_dateTimeUtc = null;
		$this->_status = Nmea0183RmcStatus::V;
		$this->_latitude = 0;
		$this->_latitudeDirection = Nmea0183Direction::Invalid;
		$this->_longitude = 0;
		$this->_longitudeDirection = Nmea0183Direction::Invalid;
		$this->_speedOverGround = 0;
		$this->_courseOverGround = 0;
		$this->_magneticDeclination = 0;
		$this->_magneticDeclinationDirection = Nmea0183Direction::Invalid;
		$this->_signalIntegrity = Nmea0183RmcSignal::DataNotValid;
		
		$content = explode(",", $sentence);
		if(count($content) != 13)
			return;
		
		if(strlen($content[1]) > 6)
			$this->_dateTimeUtc = \DateTime::createFromFormat('dmy His.u', $content[9] ." ". $content[1], new \DateTimeZone("UTC"));
		else
			$this->_dateTimeUtc = \DateTime::createFromFormat('dmy His', $content[9] ." ". $content[1], new \DateTimeZone("UTC"));
		
		if($content[2] == 'A')
			$this->_status = Nmea0183RmcStatus::A;
		
		$this->_latitude = Nmea0183Rmc::nmeaToDecimalDegrees($content[3]);
		if(Nmea0183Rmc::directionToEnum($content[4]) == Nmea0183Direction::South)
			$this->_latitude = $this->_latitude * -1;
		
		$this->_longitude = Nmea0183Rmc::nmeaToDecimalDegrees($content[5]);
		if(Nmea0183Rmc::directionToEnum($content[6]) == Nmea0183Direction::West)
			$this->_longitude = $this->_longitude * -1;
		
		$this->_speedOverGround = (float)$content[7];
		
		$this->_courseOverGround = (float)$content[8];
		
		$this->_magneticDeclination = (float)$content[10];
		$this->_magneticDeclinationDirection = Nmea0183Rmc::directionToEnum($content[11]);
		
		$tmp = explode("*", $content[12]);
		$this->_signalIntegrity = Nmea0183Rmc::signalIntegrityToEnum($tmp[0]);
	}
	
	public function dateTime($timezone = "UTC")
	{
		$timezone = new \DateTimeZone($timezone);
		$date = $this->_dateTimeUtc;
		if($date == null)
			return null;
		$date->setTimezone($timezone);
		return $date;
	}
	
	public function status()
	{
		return $this->_status;
	}
	
	/**
	 * @brief Latitude with North (+) and South (-)
	 */
	public function latitude()
	{
		return $this->_latitude;
	}
	
	/**
	 * @brief Longitude with East (+) and West (-)
	 */
	public function longitude()
	{
		return $this->_longitude;
	}
	
	public function speedOverGround()
	{
		return $this->_speedOverGround;
	}
	
	public function courseOverGround()
	{
		return $this->_courseOverGround;
	}
	
	public function magneticDeclination()
	{
		return $this->_magneticDeclination;
	}
	
	public function magneticDeclinationDirection()
	{
		return $this->_magneticDeclinationDirection;
	}
	
	public function signalIntegrity()
	{
		return $this->_signalIntegrity;
	}
	
	private static function directionToEnum($direction)
	{
		switch($direction){
			case 'N':
				return Nmea0183Direction::North;
			case 'E':
				return Nmea0183Direction::East;
			case 'S':
				return Nmea0183Direction::South;
			case 'W':
				return Nmea0183Direction::West;
		}
		return Nmea0183Direction::Invalid;
	}
	
	private static function signalIntegrityToEnum($integrity)
	{
		switch($integrity){
			case 'A':
				return Nmea0183RmcSignal::AutonomousMode;
			case 'D':
				return Nmea0183RmcSignal::DifferentialMode;
			case 'E':
				return Nmea0183RmcSignal::EstimatedMode;
			case 'M':
				return Nmea0183RmcSignal::ManualInputMode;
			case 'S':
				return Nmea0183RmcSignal::SimulatedMode;
		}
		return Nmea0183RmcSignal::DataNotValid;
	}
	
	private static function nmeaToDecimalDegrees($nmeaValue)
	{
		if((float)$nmeaValue == 0)
			return 0;

		$matches = array();
		$result = preg_match('/^([0-9]+)([0-9]{2}\.[0-9]+)$/', $nmeaValue, $matches);
		if($result === false || $result != 1 || count($matches) != 3){
			$message = "Could not convert nmea position into decimal degress ". $nmeaValue;
			error_log($message);
			if($debug > 0)
				print $message ."\n";
			return 0;
		}
		$decimalDegrees = (float)$matches[1];
		$decimalMinutes = (float)$matches[2];
		return $decimalDegrees + ($decimalMinutes / 60.0);
	}
	
	private $_dateTimeUtc;
	private $_status;
	private $_latitude;
	private $_longitude;
	private $_speedOverGround;
	private $_courseOverGround;
	private $_magneticDeclination;
	private $_magneticDeclinationDirection;
	private $_signalIntegrity;
}

?>
