<?php
	class OSGrid
	{
        private $GridID				=	0;
		private $GridName			=	"";
		private	$GridHomeURL		=	"";
		private	$GridAssetServer	=	"";
		private	$GridIMServer		=	"";

		function __construct($gridName, $gridHomeURL, $gridAssetServer, $gridIMServer, $gridID = 0) 
		{
			global $RUNTIME;

			$this->GridID			=	$gridID;
			$this->GridName			=	$gridName;
			$this->GridHomeURL		=	$gridHomeURL;
			$this->GridAssetServer	=	$gridAssetServer;
			$this->GridIMServer		=	$gridIMServer;

			if($gridID == 0)
			{
				$addRegionStatement = $RUNTIME['PDO']->prepare("INSERT IGNORE INTO grids (GridName, GridHomeURL, GridAssetServer, GridIMServer) VALUES (:GridName, :GridHomeURL, :GridAssetServer, :GridIMServer)");
				$addRegionStatement->execute(array("GridName" => $gridName, "GridHomeURL" => $gridHomeURL, "GridAssetServer" => $gridAssetServer, "GridIMServer" => $gridIMServer)); 
				$this->GridID 		= $RUNTIME['PDO']->lastInsertId();

				if($this->GridID == 0)
				{
					$getRegionStatement = $RUNTIME['PDO']->prepare("SELECT * FROM grids WHERE GridName = :GridName");
					$getRegionStatement->execute(array("GridName" => $gridName)); 
	
					if($row = $getRegionStatement->fetch())
						$this->GridID = $row['GridID'];
				}
			}
		}

		//Static functions
		public static function getGridByName($gridName)
		{
			global $RUNTIME;

			$getRegionStatement = $RUNTIME['PDO']->prepare("SELECT * FROM grids WHERE GridName = :GridName");
			$getRegionStatement->execute(array("GridName" => $gridName)); 

			if($row = $getRegionStatement->fetch())
				return new OSGrid($row['GridName'], $row['GridHomeURL'], $row['GridAssetServer'], $row['GridIMServer'], $row['GridID']);

			return null;
		}

		public static function getGridByID($gridID)
		{
			global $RUNTIME;

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

			if($row = $getRegionStatement->fetch())
				return new OSGrid($row['GridID'], $row['GridName'], $row['GridHomeURL'], $row['GridAssetServer'], $row['GridIMServer']);

			return null;
		}

		public function getAssetConnector(){ 
			return new OSAssetConecctor($this->GridAssetServer);
		}

		public function getGridID(){ 
			return $this->GridID;
		}
		
		public function getRegions(){ 
			global $RUNTIME;

			$allRegionsFromThisGrid	=	array();
	
			$getRegionStatement = $RUNTIME['PDO']->prepare("SELECT * FROM regions WHERE GridID = :GridID");
			$getRegionStatement->execute(array("GridID" => $this->GridID)); 
	
			while($row = $getRegionStatement->fetch())
			{
				$regionData		=	new OSRegion($row['RegionImage'], $row['RegionHostname'], $row['RegionPort'], $row['RegionVersion'], $row['RegionID'], $row['GridID']);

				array_push($allRegionsFromThisGrid, $regionData);
			}

			return $allRegionsFromThisGrid;
		}

		public function addRegion($regionName, $regionUUID, $userUUID, $regionImage, $regionHostname, $regionPort){
			return new OSRegion($regionName, $regionUUID, $userUUID, $regionImage, $regionHostname, $regionPort, $this);	
		}

		public function addUser($userUUID, $userName, $userMail){
			return new OSUser($userUUID, $userName, $userMail, $this);	
		}
    }
?>