<html>
<head>
<title>Weather</title>
<style type="text/css">
body {
	color:#CCCCCC;
	font-family:Arial,sans-serif;
	font-size:0.75em;
	line-height:1.54em;
	text-align:center;
}

div.weather_container_adv {
	margin: 5px;
}

div.weather_container_adv table th {
	background-color:#000000;
	color:#FFFFFF;
	font-weight:bold;
	height:24px;
	line-height:23px;
	margin:0;
	padding:0 5px;
	text-transform:uppercase;
}

div.weather_container_adv table td.ws_td {
	background-color:#ffffff;
	color:#000000;
	margin:0;
	padding:0;
	border-left: 1px solid #cccccc;
}

div.weather_container_adv table table td.para_name {
	color:#000000;
	font-size: 11px;
	padding: 2px 5px;	
}

div.weather_container_adv table table td.para_value {
	color:#000000;
	font-size: 12px;
	font-weight: bold;
	padding: 2px 5px;	
}

div.weather_container_adv table td.ws_1st_row {
	background-color:#ffffff;
	color:#000000;
	margin:0;
	padding:0;
	border-bottom: 1px solid #cccccc;
}

div.weather_container_adv table td.ws_1st_row_last {
	border-right: 1px solid #cccccc;
}


div.weather_container_adv table td.ws_2nd_row {
	background-color:#ffffff;
	color:#000000;
	font-size: 12px;
	font-weight: bold;
	margin:0;
	padding:3px 0px;
	border-bottom: 1px solid #cccccc;
}

div.weather_container_adv table td.ws_2nd_row_last {
	border-right: 1px solid #cccccc;
}

div.weather_container_adv table td.ws_3rd_row {
	background-color:#ffffff;
	color:#000000;
	margin:0;
	padding:0;
	border-bottom: 1px solid #cccccc;
}

div.weather_container_adv table td.ws_3rd_row_last {
	border-right: 1px solid #cccccc;
}

div.weather_container_adv table td.ws_4th_row {
	background-color:#efefef;
	color:#333333;
	font-size: 12px;
	margin:0;
	padding:0;
	border-bottom: 1px solid #cccccc;
}

div.weather_container_adv table td.ws_4th_row_last {
	border-right: 1px solid #cccccc;
}





div.weather_container_adv span.degree {
	font-size:30px;
	color: #696969;
}

</style>
</head>
<body>
<?php
// Pull in the NuSOAP code
require_once('../../lib/nusoap.php');
include_once("functions.php");

$webservices_url = "http://localhost/webservices/cyws/srvdispatcher.php?wsdl";
$weather = array();
$ws_request = array(
	'server_name' 		=> 'weather',
	'function_name'		=> 'getCityWeather',
	'function_params'	=> 'Limassol'
);
//pre_(getCurrentCityWeather($webservices_url, $ws_request));

//echo "<br/>Nicosia<br/>";
//pre_(getCurrentCityWeather($webservices_url, "Nicosia"));
$adv_weather_debug = false;
if(isset($_REQUEST['adv_weather_debug'])) $adv_weather_debug = true;
$general_path = ".";
$all_cache_folder = "$general_path/all_cache";
$all_cache_weather = "$all_cache_folder/weather";

$city_name_list = array("Limassol", "Nicosia", "Larnaca", "Paphos", "Famagusta");
$ws_request_current = array(
	'server_name' 		=> 'weather',
	'function_name'		=> 'getCitiesWeather',
	'function_params'	=> base64_encode(serialize($city_name_list))
);

$ws_request_fiveday = array(
	'server_name' 		=> 'weather',
	'function_name'		=> 'getCitiesWeatherFiveday',
	'function_params'	=> base64_encode(serialize($city_name_list))
);

/* Check if cache folders are in place ******************************************************/
if(!file_exists("$all_cache_folder") || !is_dir("$all_cache_folder")) {
	if( mkdir("$all_cache_folder", 0777) ) {
		if($adv_weather_debug) echo "<br/>$all_cache_folder created";
		
		if( mkdir("$all_cache_weather", 0777) ) {
			if($adv_weather_debug) echo "<br/>$all_cache_weather created";
		}
		else {
			if($adv_weather_debug) echo "<br/>Cannot create dir [$all_cache_weather]";
		}
	}
	else {
		if($adv_weather_debug) echo "<br/>Cannot create dir [$all_cache_folder]";
	}
}
else {
	if(!file_exists("$all_cache_weather") || !is_dir("$all_cache_weather")) {
		if( mkdir("$all_cache_weather", 0777) ) {
			//chmod("$general_path/all_cache/weather", 777);
			if($adv_weather_debug) echo "<br/>$all_cache_weather created";
		}
		else {
			if($adv_weather_debug) echo "<br/>Cannot create dir [$all_cache_weather]";
		}
	}
}
/* END Check for cache folders  *************************************************************/

$have_it_on_cache = false;
$md5 = md5(serialize($ws_request_current));
$cache_file = $all_cache_weather . "/" . $md5;
if(file_exists($cache_file)) {
	$cache_time = filemtime($cache_file);
	$current_time = time();
	if($current_time - $cache_time < 3600) {
		$have_in_on_cache = true;
	}
	else {
		unlink($cache_file);
	}
}

if($have_it_on_cache) {
	$cache_get = trim( file_get_contents($cache_file, false, stream_context_create([
		"ssl" => [
			"verify_peer"=>false,
			"verify_peer_name"=>false,
		]
	])) );
	if($cache_get != "") {
		$cache_get = unserialize($cache_get);
		if(isset($cache_get[0]) && is_array($cache_get[0])) {
			$weather_current = $cache_get[0];
		}
		
		if(isset($cache_get[1]) && is_array($cache_get[1])) {
			$weather_fiveday = $cache_get[1];
		}
	}
}
else {
	$weather_current = getCurrentCityWeather($webservices_url, $ws_request_current);
	$weather_fiveday = getCurrentCityWeather($webservices_url, $ws_request_fiveday);
	$cache_put = serialize(array($weather_current, $weather_fiveday));
	if( true == ($fc = fopen($cache_file, "w") ) ) {
		fwrite($fc, $cache_put, strlen($cache_put));
		fclose($fc);
	}
}

//pre_($weather_fiveday);

$c_to_f = "c";
if(isset($_REQUEST['show_scale']) && $_REQUEST['show_scale'] == "f") {
	$c_to_f = "f";
}



if(is_array($weather_current) && count($weather_current)>0) {
	foreach ($weather_current as $k=>$city) {
		?>
		<div class="weather_container_adv">
			<table cellpadding="0" cellspacing="0" border="0" width="50%">
				<tr>
					<th colspan="6" align="left"><?php echo tx($city['ws_city_name']);?></th>
				</tr>
				<tr>
					<td class="ws_td ws_1st_row ws_1st_row_first" align="center" valign="top">
						<strong><?php echo tx("CURRENT");?></strong><br/>
						<span class="degree"><?php echo convert_temp($city['temp'], $c_to_f) . '&deg; ' . show_scale($city['temp_units'], $c_to_f);?></span>
					</td>
					<td class="ws_td ws_1st_row" align="center" valign="top">
						<strong><?php echo tx($city['phrase']);?></strong><br/>
						<div class="m_icon"><img src="../images/weather_icons/<?php echo $city['icon'];?>.png" align="weather-icon" border="0"/></div>
					</td>
					<td class="ws_td ws_1st_row ws_1st_row_last" valign="middle" align="center"  colspan="3">
						<table cellpadding="0" cellspacing="0" border="0" class="ws_details">
							<tr>
								<td align="left" nowrap class="para_name"><?php echo tx("Visibility");?>:</td>
								<td align="left" nowrap class="para_value"><?php echo $city['vis'] . " " . $city['vis_unit'];?></td>
								<td align="left" nowrap class="para_name"><?php echo tx("Wind speed");?>:</td>
								<td align="left" nowrap class="para_value"><?php echo $city['windspeed'] . " " . $city['windspeed_unit'];?></td>
							</tr>
							<tr>
								<td align="left" nowrap class="para_name"><?php echo tx("Pressure");?>:</td>
								<td align="left" nowrap class="para_value"><?php echo $city['pres'] . " " . $city['pres_unit'];?></td>
								<td align="left" nowrap class="para_name"><?php echo tx("Wind direction");?>:</td>
								<td align="left" nowrap class="para_value"><?php echo $city['wind_dir'] ;?></td>
							</tr>
							<tr>
								<td align="left" nowrap class="para_name"><?php echo tx("Humidity");?>:</td>
								<td align="left" nowrap class="para_value"><?php echo $city['rhumid'] . " " . $city['rhumid_unit'];?></td>
								<td align="left" nowrap class="para_name"><?php echo tx("Wind Chill");?>:</td>
								<td align="left" nowrap class="para_value"><?php echo convert_temp($city['wndchl'], $c_to_f) . "&deg; " . show_scale($city['wndchl_unit'], $c_to_f);?></td>
							</tr>
						</table>
					</td>
				</tr>
				<?php
				if(isset($weather_fiveday[$city['ws_city_id']])) {
					asort($weather_fiveday[$city['ws_city_id']]);
					
					// Put the names of days of the week
					?>
					<tr>
					<?php
					$cnt = 1;
					foreach ($weather_fiveday[$city['ws_city_id']] as $id=>$fd) {
						$ext_cls = "";
						if($cnt == 1) $ext_cls = "ws_2nd_row_first";
						if($cnt == count($weather_fiveday[$city['ws_city_id']])) $ext_cls = "ws_2nd_row_last";
						?>
						<td width="20%" class="ws_td ws_2nd_row <?php echo $ext_cls;?>" align="center"><?php echo tx($fd['day_name']);?></td>
						<?php
						$cnt++;
					}
					?>
					</tr>
					<?php
					
					// Put the particular forecast data per day
					?>
					<tr>
					<?php
					$cnt = 1;
					foreach ($weather_fiveday[$city['ws_city_id']] as $id=>$fd) {
						$ext_cls = "";
						if($cnt == 1) $ext_cls = "ws_3rd_row_first";
						if($cnt == count($weather_fiveday[$city['ws_city_id']])) $ext_cls = "ws_3rd_row_last";
						?>
						<td width="20%" class="ws_td ws_3rd_row <?php echo $ext_cls;?>" align="center">
							<div class="m_icon"><img src="../images/weather_icons/<?php echo $fd['icon'];?>.png" border="0" alt="<?php echo tx($fd['phrase']);?>" title="<?php echo tx($fd['phrase']);?>"/></div>
							<table border="0" cellspacing="0" cellpadding="0" class="ws_details">
								<tr>
									<td nowrap class="para_name"><?php echo tx("High");?>:</td>
									<td nowrap class="para_value" align="right"><?php echo convert_temp($fd['high'], $c_to_f) . '&deg;' . show_scale($fd['high_unit'], $c_to_f); ?></td>
								</tr>
								<tr>
									<td nowrap class="para_name"><?php echo tx("Low");?>:</td>
									<td nowrap class="para_value" align="right"><?php echo convert_temp($fd['low'], $c_to_f) . '&deg;' . show_scale($fd['low_unit'], $c_to_f); ?></td>
								</tr>
							</table>
						</td>
						<?php
						$cnt++;
					}
					?>
					</tr>
					<?php
					
					// Put the particular forecast phrase
					?>
					<tr>
					<?php
					$cnt = 1;
					foreach ($weather_fiveday[$city['ws_city_id']] as $id=>$fd) {
						$ext_cls = "";
						if($cnt == 1) $ext_cls = "ws_4th_row_first";
						if($cnt == count($weather_fiveday[$city['ws_city_id']])) $ext_cls = "ws_4th_row_last";
						?>
						<td width="20%" class="ws_td ws_4th_row <?php echo $ext_cls;?>" align="center"><?php echo $fd['phrase'];?></td>
						<?php
						$cnt++;
					}
					?>
					</tr>
					<?php
				}
				?>
			</table>
		</div>
		<?php
	}
}
?>
</body>
</html>
