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

/**
  * @brief One item in a track
  */
class TrackItem
{
	function __construct($timestamp = null, $latitude = null, $longitude = null)
	{
		$this->setTimestamp($timestamp);
		$this->setLatitude($latitude);
		$this->setLongitude($longitude);
		$this->_speedThroughWater = null;
		$this->_speedOverGround = null;
		$this->_courseOverGround = null;
		$this->_waterDepth = null;
		$this->_waterTemperature = null;
		$this->_gpsfix = null;
		
		$this->_properties = [
			"time" => ["timestamp", "setTimestamp"],
			"latitude" => ["latitude", "setLatitude"],
			"longitude" => ["longitude", "setLongitude"],
			"speedthroughwater" => ["speedThroughWater", "setSpeedThroughWater"],
			"speedoverground" => ["speedOverGround", "setSpeedOverGround"],
			"courseoverground" => ["courseOverGround", "setCourseOverGround"],
			"waterdepth" => ["waterDepth", "setWaterDepth"],
			"watertemperature" => ["waterTemperature", "setWaterTemperature"],
			"gpsfix" => ["gpsFix", "setGpsFix"],
		];
	}

	public function hasProperty($property)
	{
		return array_key_exists(strtolower($property), $this->_properties);
	}
	
	public function getProperty($property)
	{
		$property = strtolower($property);
		if(!$this->hasProperty($property))
			return null;
		return call_user_func(array($this, $this->_properties[$property][0]));
	}
	
	public function setProperty($property, $value)
	{
		$property = strtolower($property);
		if(!$this->hasProperty($property))
			return false;
		return call_user_func(array($this, $this->_properties[$property][1]), $value);
	}
	
	public function setTimestamp($time)
	{
		if(!is_numeric($time) || is_nan($time)){
			$this->_time = null;
			return false;
		}
		$this->_time = $time;
		return true;
	}
	
	public function setDateTime($dateTime)
	{
		$timezone = new \DateTimeZone("UTC");
		if($dateTime == null){
			$this->_time = null;
			return true;
		}
		$dateTime->setTimezone($timezone);
		$this->_time = $dateTime->getTimestamp();
		return true;
	}
	
	public function timestamp()
	{
		return $this->_time;
	}
	
	public function setLatitude($latitude)
	{
		if(!is_numeric($latitude) || is_nan($latitude)){
			$this->_latitude = null;
			return false;
		}
		$this->_latitude = $latitude;
		return true;
	}
	
	public function latitude()
	{
		return $this->_latitude;
	}
	
	public function setLongitude($longitude)
	{
		if(!is_numeric($longitude) || is_nan($longitude)){
			$this->_longitude = null;
			return false;
		}
		$this->_longitude = $longitude;
		return true;
	}
	
	public function longitude()
	{
		return $this->_longitude;
	}
	
	public function setSpeedThroughWater($speedThroughWater)
	{
		if(!is_numeric($speedThroughWater) || is_nan($speedThroughWater)){
			$this->_speedThroughWater = null;
			return false;
		}
		$this->_speedThroughWater = $speedThroughWater;
		return true;
	}
	
	public function speedThroughWater()
	{
		return $this->_speedThroughWater;
	}
	
	public function setSpeedOverGround($speedOverGround)
	{
		if(!is_numeric($speedOverGround) || is_nan($speedOverGround)){
			$this->_speedOverGround = null;
			return false;
		}
		$this->_speedOverGround = $speedOverGround;
		return true;
	}
	
	public function speedOverGround()
	{
		return $this->_speedOverGround;
	}
	
	public function setCourseOverGround($courseOverGround)
	{
		if(!is_numeric($courseOverGround) || is_nan($courseOverGround)){
			$this->_courseOverGround = null;
			return false;
		}
		$this->_courseOverGround = $courseOverGround;
		return true;
	}
	
	public function courseOverGround()
	{
		return $this->_courseOverGround;
	}
	
	public function setWaterDepth($waterDepth)
	{
		if(!is_numeric($waterDepth) || is_nan($waterDepth)){
			$this->_waterDepth = null;
			return false;
		}
		$this->_waterDepth = $waterDepth;
		return true;
	}
	
	public function waterDepth()
	{
		return $this->_waterDepth;
	}
	
	public function setWaterTemperature($waterTemperature)
	{
		if(!is_numeric($waterTemperature) || is_nan($waterTemperature)){
			$this->_waterTemperature = null;
			return false;
		}
		$this->_waterTemperature = $waterTemperature;
		return true;
	}
	
	public function waterTemperature()
	{
		return $this->_waterTemperature;
	}
	
	public function setGpsFix($gpsFix)
	{
		if(!is_numeric($gpsFix) || is_nan($gpsFix)){
			$this->_gpsFix = null;
			return false;
		}
		$this->_gpsFix = $gpsFix;
		return true;
	}
	
	public function gpsFix()
	{
		return $this->_gpsFix;
	}
	
	public function gpsFixString()
	{
		switch($this->gpsFix()){
			case 2:
				return "2D";
			case 3:
				return "3D";
			default:
				return "-";
		}
	}
	
	public function gpsFixed()
	{
		if($this->gpsFix() != null && $this->gpsFix() > 1)
			return true;
		return false;
	}
	
	public function isValid()
	{
		if($this->timestamp() == null)
			return false;
		if($this->latitude() == null)
			return false;
		if($this->longitude() == null)
			return false;
		return true;
	}
	
	public function toArray()
	{
		$speedThroughWater = ($this->speedThroughWater() != null) ? $this->speedThroughWater() : "-";
		$speedOverGround = ($this->speedOverGround() != null) ? $this->speedOverGround() : "-";
		$courseOverGround = ($this->courseOverGround() != null) ? $this->courseOverGround() : "-";
		$waterDepth = ($this->waterDepth() != null) ? $this->waterDepth() : "-";
		$waterTemperature = ($this->waterTemperature() != null) ? $this->waterTemperature() : "-";
		$gpsFix = $this->gpsFixString();
	
		return [
			"latitude" => $this->latitude(),
			"longitude" => $this->longitude(),
			"speedthroughwater" => $speedThroughWater,
			"speedoverground" => $speedOverGround,
			"courseoverground" => $courseOverGround,
			"waterdepth" => $waterDepth,
			"watertemperature" => $waterTemperature,
			"gpsfix" => $gpsFix,
		];
	}

	private $_time;
	private $_latitude, $_longitude;
	private $_speedThroughWater, $_speedOverGround, $_courseOverGround;
	private $_waterDepth, $_waterTemperature;
	private $_gpsFix;
}

?>
