<?php
	class RegionManager
	{
		private $allRegions			=	array();

		function __construct($startRegion, $totalRegionCount) 
		{
			global $RUNTIME;

			$topRegionStatement = $RUNTIME['PDO']->prepare("SELECT regions.RegionName AS Name, regions.RegionUUID AS UUID, regions.RegionImage AS Image, regions.RegionOwner AS OwnerID, users.UserName AS OwnerName, onlinetimes.RegionLastSeen AS LastSeen, onlinetimes.RegionOnlineUser AS OnlineCount, regions.RegionVersion AS RegionVersion, regions.RegionHostname AS Hostname, regions.RegionPort AS Port FROM regions INNER JOIN onlinetimes ON regions.RegionUUID = onlinetimes.RegionUUID INNER JOIN users ON regions.RegionOwner = users.UserUUID WHERE onlinetimes.RegionLastSeen > ? ORDER BY OnlineCount DESC LIMIT ".$startRegion.", ".$totalRegionCount);
			$topRegionStatement->execute(array(time() - 900)); 

			while($row = $topRegionStatement->fetch())
			{
				array_push($this->allRegions, $row);
			}
		}

		function getRegionCount()
		{
			return count($this->allRegions);
		}

		function getNextRegion()
		{
			return array_shift($this->allRegions);
		}

		public static function setOnlineTime($uuid)
		{
			global $RUNTIME;

			$setOnlineTimeStatement = $RUNTIME['PDO']->prepare("UPDATE onlinetimes SET RegionLastSeen = ? WHERE RegionUUID = ?");
			$setOnlineTimeStatement->execute(array(time(), $uuid)); 
		}

		public static function removeRegion($uuid)
		{
			global $RUNTIME;

			$removeRegionStatement = $RUNTIME['PDO']->prepare("DELETE regions, onlinetimes FROM regions INNER JOIN onlinetimes ON regions.RegionUUID = onlinetimes.RegionUUID WHERE regions.RegionUUID = ?");
			$removeRegionStatement->execute(array($uuid)); 
		}
    }
?>