<?php
	class OSRegion
	{
        private $RegionID			=	0;
		private	$RegionPort			=	0;
		private $RegionName			=	"";
		private $RegionUUID			=	"";
		private $UserUUID			=	"";
		private $RegionImage		=	"";
		private	$RegionHostname		=	"";

		private $Grid				=	null;

		function __construct($regionName, $regionUUID, $userUUID, $regionImage, $regionHostname, $regionPort, $grid, $regionID = 0) 
		{
			global $RUNTIME;

			$this->Grid				=	$grid;

			$this->RegionName		=	$regionName;
			$this->RegionImage		=	$regionImage;
			$this->RegionUUID		=	$regionUUID;
			$this->UserUUID			=	$userUUID;
			$this->RegionHostname	=	$regionHostname;
			$this->RegionPort		=	$regionPort;
			$this->RegionID			=	$regionID;

			if($regionID == 0)
			{
				$addRegionStatement = $RUNTIME['PDO']->prepare("INSERT IGNORE INTO regions (GridID, RegionName, RegionUUID, UserUUID, RegionImage, RegionHostname, RegionPort) VALUES (:GridID, :RegionName, :RegionUUID, :UserUUID, :RegionImage, :RegionHostname, :RegionPort)");
				$addRegionStatement->execute(array("GridID" => $this->Grid->getGridID(), "RegionName" => $regionName, "RegionUUID" => $regionUUID, "UserUUID" => $userUUID, "RegionImage" => $regionImage, "RegionHostname" => $regionHostname, "RegionPort" => $regionPort)); 
				$this->RegionID		= $RUNTIME['PDO']->lastInsertId();

				if($this->RegionID == 0)
				{
					$getRegionStatement = $RUNTIME['PDO']->prepare("SELECT * FROM regions WHERE RegionName = :RegionName AND GridID = :GridID");
					$getRegionStatement->execute(array("RegionName" => $regionName, "GridID" => $this->Grid->getGridID())); 
	
					if($row = $getRegionStatement->fetch())
						$this->RegionID = $row['RegionID'];
				}
			}
		}

		public static function getRegionForOnlineCheck($count)
		{
			global $RUNTIME;

			$regionList			=	array();

			$getRegionStatement = $RUNTIME['PDO']->prepare("SELECT * FROM regions ORDER BY LastSeen DESC LIMIT 15");
			$getRegionStatement->execute(array()); 

			while($regionRow = $getRegionStatement->fetch())
			{
				$getGridStatement = $RUNTIME['PDO']->prepare("SELECT * FROM grids WHERE GridID = :GridID");
				$getGridStatement->execute(array("GridID" => $regionRow['GridID'])); 

				if($gridRow = $getGridStatement->fetch())
				{
					$gridData = new OSGrid($gridRow['GridName'], $gridRow['GridHomeURL'], $gridRow['GridAssetServer'], $gridRow['GridIMServer'], $gridRow['GridID']);

					$regionList[] = new OSRegion($regionRow['RegionName'], $regionRow['RegionImage'], $regionRow['RegionUUID'], $regionRow['UserUUID'], $regionRow['RegionHostname'], $regionRow['RegionPort'], $gridData, $regionRow['RegionID']);
				}
			}

			return $regionList;
		}

		public function setOnline($online){
			global $RUNTIME;

			$currentDateTime = new DateTime("NOW");
			$setOnlineStatement = $RUNTIME['PDO']->prepare("UPDATE regions SET Online = :OnlineStatus, LastSeen = :LastSeen WHERE RegionID = :RegionID");
			//$setOnlineStatement->debugDumpParams();
			$setOnlineStatement->execute(array("OnlineStatus" => $online, "RegionID" => $this->RegionID, "LastSeen" => $currentDateTime->format('Y-m-d H:i:s'))); 	
			
			echo "Set Online State '".$online."' for region ".$this->RegionName;
		}

		public function getHostname(){ 
			return $this->RegionHostname;
		}

		public function getPort(){ 
			return $this->RegionPort;
		}
    }
?>