<?php

Template::set('title', 'Visualisation page');
global $_database;


//
// load sensors ids
//
$query = "SELECT DISTINCT sensor_id AS id, name FROM configuration WHERE sensor_id > 0 ORDER BY sensor_id DESC";
$_database->setQuery($query);
$_sensors = $_database->loadObjectList();

if (empty($_sensors))
{
	Template::addMessage('No sensors for Visualisation', 'warning');
}
else
{
	$results_count = isset($_POST['results_count']) ? (integer) $_POST['results_count'] : 0;
	$results_count = $results_count > 0 && $results_count <= 80 ? $results_count : 20;

	// select results count
	echo '<form method="POST">';
		echo '<label for="count">Results count:</label>';
		echo '<select id="count" class="form-control" name="results_count" onchange="this.form.submit()">';
			echo '<option value="20"' . ($results_count == 20 ? ' selected' : '') . '>20</option>';
			echo '<option value="40"' . ($results_count == 40 ? ' selected' : '') . '>40</option>';
			echo '<option value="60"' . ($results_count == 60 ? ' selected' : '') . '>60</option>';
			echo '<option value="80"' . ($results_count == 80 ? ' selected' : '') . '>80</option>';
		echo '</select>';
	echo '</form>';
	echo '<br/>';

	require(DIR_ROOT . DS . 'extensions' . DS . 'graph.php');
	foreach ($_sensors as $i => $sensor)
	{
		$query = "SELECT * FROM received_data WHERE sensor_id = " . (int) $sensor->id . ' ORDER BY timestamp DESC LIMIT ' . $results_count;
		$_database->setQuery($query);
		$results = $_database->loadObjectList();

		if (empty($results))
		{
			echo '<h1>' . $sensor->name  . ' (#' . $sensor->id . ')' . '</h1>';
			echo '<p>No data to display.</p>';
			continue;
		}

		$results = array_reverse($results);


		//
		// Battery
		//
		$last = end($results);
		$vltg = (int) $last->vltg;
		$battery = '';
		switch ($vltg) {
			case $vltg > 3500:
				$battery = '<strong style="color: #00ff00;">REALY GOOD</strong>';
				break;
			case $vltg > 3200:
				$battery = '<strong style="color: #00ff00;">GOOD</strong>';
				break;
			case $vltg > 3000:
				$battery = '<strong style="color: #00ff00;">POOR</strong>';
				break;
			default:
				$battery = '<strong style="color: #ff0000;">BAD</strong>';
				break;
		}
		$battery .= ' (' . ($vltg / 1000) . ' V)';

		//
		// LABEL
		//
		echo '<h1>' . $sensor->name  . ' <small>(#' . $sensor->id . ')</small>' . '</h1>';
		echo 'Battery: ' . $battery;

		//
		// tmp
		//
		echo '<h2><small>Temperature</small></h2>';
		$labels = array();
		$data 	= array();
		foreach ($results as $i => $values)
		{
			$labels[] = $values->timestamp;
			$data[] = $values->tmp / 100;
		}
		$graph = new LineGraph($labels);
     	$graph->setDatasetName('Temperature');
		$graph->setDatasetFillcolor('184, 54, 54, 0.8');
		$graph->insertData($data);

		echo '<div style="max-width: 100%; margin: 5px auto 40px;">';
			$graph->render();
		echo '</div>';

		//
		// hum
		//
		echo '<h2><small>Humidity</small></h2>';
		$labels = array();
		$data 	= array();
		foreach ($results as $i => $values)
		{
			$labels[] = $values->timestamp;
			$data[] = $values->hum /100;
		}
		$graph = new LineGraph($labels);
     	$graph->setDatasetName('Humidity');
		$graph->setDatasetFillcolor('18,176,222,0.4');
		$graph->insertData($data);

		echo '<div style="max-width: 100%; margin: 5px auto 40px;">';
			$graph->render();
		echo '</div>';
	}
}
?>