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

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301  USA.
*/

namespace SiWeather\Webservice;

class WebserviceConfig {

	function __construct(){
		require(__DIR__ ."/../../../config/config.php");
		$this->_config = $config;
		$this->loadAdditionalConfiguration();

		if($this->getConfig("mode") == "development"){
			error_reporting(E_ALL);
		}
	}
	
	private function loadAdditionalConfiguration(){
		$this->_config["dataDirectory"] = "data";
		
		require(__DIR__ ."/../../inc/internalconfig.php");
		foreach(array_keys($internalconfig) as $key){
			if(array_key_exists($key, $this->_config)){
				error_log("Internal config entry ". $key ." already exists --> Keeping existing value and discard internal config value");
			}
			else{
				$this->_config[$key] = $internalconfig[$key];
			}
		}
	}

	public function getConfig($key){
		if(!array_key_exists($key, $this->_config)){
			error_log("Expected config value ". $key ." does not exists");
			return FALSE;
		}
		
		if($key == "dataDirectory"){
// 			error_log("Use dataDirectory() function to access this value");
			return $this->dataDirectory();
		}
		if($key == "webserviceBaseUrl"){
// 			error_log("Use dataDirectory() function to access this value");
			return $this->webserviceBaseUrl();
		}
		
		return $this->_config[$key];
	}

	public function dataDirectory(){
		$directory = __DIR__ ."/../../../". $this->_config["dataDirectory"];
		if(substr($directory, strlen($directory) - 1, 1) != "/"){
			$directory .= "/";
		}
		return $directory;
	}
	
	public function webserviceBaseUrl(){
		$baseUrl = $this->_config["webserviceBaseUrl"];
		if(substr($baseUrl, strlen($baseUrl) - 1, 1) != "/"){
			$baseUrl .= "/";
		}
		return $baseUrl;
	}

	private $_config;
}

?>
