<?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;

require_once(__DIR__ ."/../../../autoloader.php");

class Parser extends \GnuBoat\Nmea\NmeaParser
{
	function __construct()
	{
		$this->_types = array();
		$this->_types["rmc"] = "GnuBoat\Nmea\Nmea0183\Nmea0183Rmc";
		$this->_types["dpt"] = "GnuBoat\Nmea\Nmea0183\Nmea0183Dpt";
		$this->_types["mtw"] = "GnuBoat\Nmea\Nmea0183\Nmea0183Mtw";
	}
	
	public function parseFile($file, $debug)
	{
		$result = array();
		if(!file_exists($file)){
			$message = "NMEA file to parse does not exists: ". $file ."\n";
			error_log($message);
			if($debug > 0)
				print $message;
			return $result;
		}
		
		$fileHandler = fopen($file, 'r');
		if(!$fileHandler){
			$message = "NMEA file to parse could not be opend for reading: ". $file ."\n";
			error_log($message);
			if($debug > 0)
				print $message;
			return $result;
		}
		
		// despite the standards uses CR LF line ends, we are more flexible with auto here
		ini_set('auto_detect_line_endings',true);
		
		$line = 1;
		while(!feof($fileHandler)){
			$sentence = fgets($fileHandler);
			$parsedSentence = $this->parseSentence($sentence, $debug);
			if($parsedSentence != null)
				$result[] = $parsedSentence;
			else{
				$message = "Skipped sentence on line ". $line .": ". $sentence ."\n";
				error_log($message);
				if($debug > 0)
					print $message;
			}
		}
		fclose($fileHandler);
		return $result;
	}
	
	public function parseSentence($sentence, $debug)
	{
		$sentence = trim($sentence);
		if(empty($sentence)){
			$message = "Empty sentence\n";
			error_log($message);
			if($debug > 0)
				print $message;
			return null;
		}
	
		$matches = array();
		$result = preg_match('/^[\$!]{1}([\w]{2})([\w]{3})([^\*]+)\*([\da-zA-Z]+)$/', $sentence, $matches);
		if($result === false || $result != 1 || count($matches) != 5){
			$message = "Could not detect talker, type and checksum from ". $sentence ."\n";
			error_log($message);
			if($debug > 0)
				print $message;
			return null;
		}

		$talker = $matches[1];
		$type = $matches[2];
		$checksum = $matches[4];
		
		if(!array_key_exists(strtolower($type), $this->_types)){
			$message = "Skipping unknown NMEA0183 sentence of type ". $type .": ". $sentence ."\n";
			error_log($message);
			if($debug > 0)
				print $message;
			return null;
		}
		
		$reflectionClass = new \ReflectionClass($this->_types[strtolower($type)]);
		return $reflectionClass->newInstanceArgs([$talker, $checksum, $sentence]);
	}
}

?>
