<?php

class weather extends Module {
	//TODO rajouter les params en détail
	protected static $paramsList = array(
		'visibility',
		'city',
		'x',
		'y'
	);
	
	public $params = array();

	public function __construct($params){
		$this->setParams($params);
		$ville = $params['city'];
		include 'GoogleMeteo.php';
		echo '<script type="text/javascript" src="js/jquery.weather.js"></script>
				<link rel="stylesheet" type="text/css" href="css/weather.css">';
		echo '<div id="weather" style="left:'.$params['x'].'; top:'.$params['y'].';">
				'.$city.'<br/>
				<div class="weatherpic"></div>
				<strong>'.$present_weather.'</strong><br/>
				'.$future_weather1.'<br/>
				'.$future_weather2.'
			</div>';
	}

	private function setParams($params){
		$this->params = $params;
	}

	public static function start($params){
		$weather = new weather($params);
	}
	
	/**
	 * 
	 * Modifie la ville
	 * @param String $city la ville.
	 * @return String "ok" ou message d'erreur (effectue aussi l'enregistrement en XML)
	 */
	public function setCity($city){
		// Saving the position and z-index of the note:
		$xmla = simplexml_load_file('../'.AccueilModules::CONFIG_FILE);

		$path = $xmla->xpath("//item[@id='weather']"); 
		$path[0]->city = $city;
		
		$xmla->asXML('../'.AccueilModules::CONFIG_FILE);
		
		echo "ok";
	}
	
	public function setX($x){
		// Saving the position and z-index of the note:
		$xmla = simplexml_load_file('../'.AccueilModules::CONFIG_FILE);

		$path = $xmla->xpath("//item[@id='weather']"); 
		$path[0]->x = $x;
		
		$xmla->asXML('../'.AccueilModules::CONFIG_FILE);
		
		echo "ok";
	}
	
	public function setY($y){
		// Saving the position and z-index of the note:
		$xmla = simplexml_load_file('../'.AccueilModules::CONFIG_FILE);

		$path = $xmla->xpath("//item[@id='weather']"); 
		$path[0]->y = $y;
		
		$xmla->asXML('../'.AccueilModules::CONFIG_FILE);
		
		echo "ok";
	}
	
	public function setVisibility($visibility){
		// Saving the position and z-index of the note:
		$xmla = simplexml_load_file('../'.AccueilModules::CONFIG_FILE);

		$path = $xmla->xpath("//item[@id='weather']"); 
		$path[0]->visibility = $visibility;
		
		$xmla->asXML('../'.AccueilModules::CONFIG_FILE);
		
		echo "ok";
	}
	
	public static function updateConfig($updated){
		foreach ($updated as $what=>$withWhat){
			if(in_array($what, self::$paramsList)){
				call_user_func(array(get_class(), "set".ucfirst($what)), $withWhat);
			}
		}
	}
}