<?php
	include("config.php");
	// Returns numbers in string format as needed for file names
	function clipNumber($number, $length)
	{
		$number = abs($number) . "";
		while (strlen($number) < $length)
		{
			$number = "0" . $number;
		}
		return $number;
	}
	if (isset($_GET['tile']))
	{
		$tile = $_GET['tile'];
	}
	else
	{
		$tile = "None";
	}
?>
<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="style.css"/>
	</head>
	<body id="infobody">
	<?php
		if ($tile == "None")
		{
			echo "No tile selected";
		}
		else
		{
			preg_match_all('/([we])(\d{3})([sn])(\d{2})/m', $tile, $matches, PREG_SET_ORDER, 0);
			$we = $matches[0][1];
			$lon = (int)$matches[0][2];
			$sn = $matches[0][3];
			$lat = (int)$matches[0][4];
			if ($we == "w")
			{
				$lon *= -1;
			}
			if ($sn == "s")
			{
				$lat *= -1;
			}
			echo '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
			<svg width="450" height="450" version="1.1" xmlns="http://www.w3.org/2000/svg">
				<image width="450" height="450" href="https://ows.terrestris.de/osm/service?VERSION=1.3.0&SERVICE=WMS&REQUEST=GetMap&LAYERS=OSM-WMS&STYLES=&CRS=EPSG:4326&BBOX=' . (string)$lat . ',' . (string)$lon . ',' . (string)($lat + 10) . ',' . (string)($lon + 10) . '&WIDTH=450&HEIGHT=450&FORMAT=image/png"/>';
			// Connect to DB
			$con = new mysqli($SQL_SERVER, $SQL_USER, $SQL_PASSWORD, $SQL_DATABASE, $SQL_PORT);
			if ($con->connect_error)
			{
				quit();
			}
			$stmt = $con->prepare("SELECT secondLevel.name AS name, status.color AS color FROM secondLevel JOIN status ON secondLevel.status_id = status.id WHERE secondLevel.parent_id = (SELECT id FROM topLevel WHERE name = ?)");
			$stmt->bind_param("s", $tile);
			$stmt->execute();
			$result = $stmt->get_result();
			$ret = $result->fetch_all(MYSQLI_ASSOC);
			if ($ret != False)
			{
				$tiles = [];
				foreach($ret as $row)
				{
					$tiles[$row["name"]] = $row["color"];
				}
				$y = 405;
				for ($i = $lat; $i < $lat + 10; $i++)
				{
					$x = 0;
					for ($j = $lon; $j < $lon + 10; $j++)
					{
						$name = $we . clipNumber($j, 3) . $sn . clipNumber($i, 2);
						echo '<a href="progressinfo.php?tile=' . $tile . '&minor=' . $name . '" target="progressinfo"><rect x="' . $x . '" y="' . $y . '" width="45" height="45" style="fill: ' . $tiles[$name] . '; opacity: 0.5;"/></a>';
						$x += 45;
					}
					$y -= 45;
				}
			}
			for ($i = 45; $i < 450; $i += 45)
			{
				echo '<line x1="' . $i . '" y1="0" x2="' . $i . '" y2="450" style="stroke:rgb(0,255,0); stroke-width:1" />';
				echo '<line x1="0" y1="' . $i . '" x2="450" y2="' . $i . '" style="stroke:rgb(0,255,0); stroke-width:1" />';
			}
			echo '</svg>';
			$stmt = $con->prepare("SELECT status.name AS status, COUNT(*) AS sCount, status.color AS color FROM secondLevel JOIN status ON secondLevel.status_id = status.id WHERE secondLevel.parent_id = (SELECT id FROM topLevel WHERE name = ?) GROUP BY status.id ORDER BY priority");
			$stmt->bind_param("s", $tile);
			$stmt->execute();
			$result = $stmt->get_result();
			$ret = $result->fetch_all(MYSQLI_ASSOC);
			echo '<div id="tablecontainer"><table border="1"><tr><td>Tile Name</td><td>' . $tile . '</td></tr>';
			foreach ($ret as $row)
			{
				echo '<tr style="background-color: ' . $row["color"] . '"><td>' . $row["status"] . '</td><td>' . $row["sCount"] . '</td></tr>';
			}
			if (isset($_GET["minor"]))
			{
				$sql = 'SELECT * FROM information_schema.tables WHERE table_name = "tile" AND table_schema = "' . $SQL_DATABASE . '"';
				$ret = $con->query($sql)->fetch_all(MYSQLI_ASSOC);
				if (!empty($ret))
				{
					$stmt = $con->prepare("SELECT status.name AS status, COUNT(*) AS sCount, status.color AS color FROM tile JOIN status ON tile.status_id = status.id WHERE tile.parent_id = (SELECT id FROM secondLevel WHERE name = ?) GROUP BY status.id ORDER BY priority");
					$stmt->bind_param("s", $_GET["minor"]);
					$stmt->execute();
					$result = $stmt->get_result();
					$ret = $result->fetch_all(MYSQLI_ASSOC);
					echo '</table>';
					echo '<table border="1"><tr><td>Tile Name</td><td>' . $_GET["minor"] . '</td></tr>';
					foreach ($ret as $row)
					{
						echo '<tr style="background-color: ' . $row["color"] . '"><td>' . $row["status"] . '</td><td>' . $row["sCount"] . '</td></tr>';
					}
				}
			}
			echo '</table></div>';
		}
	?>
	</body>
</html>