<?php
	class BreakdownComponent extends Component
	{
		function GetUUIDs()
		{
			$uuids[] = 'breakdown(\.do){0,1}$';
			return $uuids;
		}
		
		function getColorForPercentage($amount, $max)
		{
			$heatValue = intval($amount/$max/2000);
			
			$red = 0;
			$green = 0;
			$blue = 0;		
							
			if($heatValue < 25) {
				$pct = $heatValue/25;
				$blue = floor(255 - $pct*255);
				$green = floor($pct*255);			
			}
			elseif($heatValue < 125) {
				$pct = ($heatValue-25) / 100;
				$green = 255;					
				$red = floor($pct*255);
			}
			else {
				$pct = ($heatValue-125) / 875;
				$green = floor(255 - $pct*255);
				$red = 255;			
			}
			return sprintf("#%02X%02X%02X", abs($red), abs($green), abs($blue));
		} 

		function Run( $page, $matches )
		{
			// Get total from db
			$tsql = get_db_tuple(get_db_rows( 'SELECT SUM(amount) FROM country_stats' ));
			$total = intval($tsql['sum']);
			
			$stats = [];
			$rank = 1;
			$max = 0.00000000000000;
			$res = get_db_rows( 'SELECT * FROM country_stats ORDER BY amount DESC' );
			while( $row = get_db_tuple( $res ) )
			{
				$row['rank'] = $rank++;
				$row['percentage'] = floatval( $row['amount'] / $total);
				if ($row['percentage'] > $max)
					$max = $row['percentage'];
				$stats[] = $row;
			}
			
			if (isset($_GET['svg']))
			{ 	
				$data = '<?xml version="1.0" encoding="utf-8"?>'.PHP_EOL.
						'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 1006 663">'.PHP_EOL.
						tab().'<defs>'.PHP_EOL.
						tab(2).'<style type="text/css">'.PHP_EOL.
						tab(3).'.land{fill-opacity: 0.7;stroke:black;stroke-opacity: 1;stroke-width:0.1;cursor:pointer}'.PHP_EOL.
						tab(3).'.land:hover	{fill-opacity: 1}'.PHP_EOL.
						tab(2).'</style>'.PHP_EOL.
						tab().'</defs>'.PHP_EOL.
						tab().'<g>'.PHP_EOL;
				foreach ($stats AS $row)
				{
					if (!empty($row['path']))
						$data .= tab(2).'<path fill="'.$this->getColorForPercentage($row['amount'],$max).'" id="'.$row['country_code'].'" class="land" d="'.gzdecode(base64_decode($row['path'])).'"><title>'.htmlspecialchars(countryname($row['country_code'])).' ('.$row['amount'].' entries)</title></path>'.PHP_EOL;
				}
				$data .= tab().'</g>'.PHP_EOL.'</svg>';
				header('Content-Length: '.strlen($data));
				header('Content-Type: image/svg+xml');
				echo $data;
				exit;
			}
			_page_header( SITE_NAME . ' :: Statistical breakdown');
?>
<h2><?=SITE_NAME?> statistical overview</h2>
<h3>Country Statistics</h3>
<object class="svgimage" id="breakdown_countrymap" type="image/svg+xml" data="?svg=1">It seems your browser doesn't fully support SVG.</object>
<table class="listing2 stripe compact top10">
	<thead>
		<tr>
			<th>Rank</th>
			<th>Country</th>
			<th>Threat amount</th>
			<th>Percentage</th>
		</tr>
	</thead>
	<tbody>
<?php
			foreach($stats as $row)
			{
				if (!$row['amount']) continue;
?>
		<tr>
			<td align="right"><?= $row['rank'] ?></td>
			<td><?= countryname($row['country_code']) ?></td>
			<td align="right"><?= $row['amount'] ?></td>
			<td align="right"><?= sprintf("%01.2f", $row['percentage']*100).'%' ?></td>
		</tr>
<?php
			}
?>
	</tbody>
	<tfoot>
		<tr>
			<th>&nbsp;</th>
			<th align="right">Total:</th>
			<th align="right"><?=$total?></th>
			<th>&nbsp;</th>
		</tr>
	</tfoot>
</table>

<h3>Class Statistics</h3>
<table class="listing2 stripe compact">
	<thead>
		<tr>
			<th>Class ID</th>
			<th>Name of class</th>
			<th>Threat amount</th>
			<th>Percentage</th>
		</tr>
	</thead>
	<tbody>
<?php
			// Get total from db
			$tsql = get_db_tuple(get_db_rows( 'SELECT SUM(count) FROM classes' ));
			$total = $tsql[0];
			
			$stats = [];
			$res = get_db_rows( 'SELECT classid, count FROM classes ORDER BY classid ASC' );
			while( $row = get_db_tuple( $res ) )
			{
				$row['percentage'] = sprintf("%01.2f", ($row['count']/$total)*100).'%';
				$stats[] = $row;
			}
			
			foreach($stats as $row)
			{
?>
		<tr>
			<td align="right"><?= $row['classid'] ?></td>
			<td><?= ClassInfo( $row['classid'] ) ?></td>
			<td align="right"><?= $row['count'] ?></td>
			<td align="right"><?= $row['percentage'] ?></td>
		</tr>
<?php
			}
?>
	</tbody>
	<tfoot>
		<tr>
			<th>&nbsp;</th>
			<th align="right">Total:</th>
			<th align="right"><?=$total?></th>
			<th>&nbsp;</th>
		</tr>
	</tfoot>
</table>
<?php
			_page_footer();
		}
	};
	new BreakdownComponent;
?>
