<?php
/*
    Copyright (C) 2016 - 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;

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

class WebService
{
	function __construct()
	{
		$this->_requests = array();
		$this->_requests["data"] = "GnuBoat\ServiceData";
		$this->_requests["shutdown"] = "GnuBoat\ServiceShutdown";
		$this->_requests["logfiles"] = "GnuBoat\ServiceLogFiles";
		$this->_requests["map"] = "GnuBoat\ServiceMap";
	}

	public function process()
	{
		$debug = 0;
		if(is_array($_GET) && array_key_exists("debug", $_GET))
			$debug = $_GET["debug"];

		$request = array_keys($this->_requests)[0];
		if(is_array($_GET) && array_key_exists("request", $_GET)){
			$request = $_GET["request"];
		}

		if(!array_key_exists($request, $this->_requests)){
			$result["error"] = 1;
			$result["errormessage"] = "Request ". $request ." not implemented";
			
			header('Content-Type: application/json');
			echo json_encode($result) ."\n";
			return;
		}

		call_user_func(array(new $this->_requests[$request], "processRequest"), $debug);
	}

	private $_requests;
}

?>
