<?php

require("Outspark/Platform/AvatarProfile/AvatarProfile.php");
require_once("Outspark/Common/KeyMaster/KeyMaster.php");
require_once("Outspark/Common/ConfigRegistry/ConfigRegistry.php");
require("../OSKSignedRestResource.inc");
require_once("Outspark/3rdparty/Restafarian/restafarian_common.php");

require_once("Log.php");	// debugging

$GLOBALS['ARLog'] = Log::factory("file" , "/tmp/AvatariumResource.log", "AvatariumResource");

/**
 * @package AvatariumResource
 * @copyright Outspark, Inc. 2007
 * @author Victor Choy
 * 
 * Rest API resource which facilitates in executing requests dealing with the Outspark avatarium
 * library.
 *
 * @TODO
 * 
 * + ADD ADDITIONAL CALLS TO AVATARBASICPROFILE FUNCTIONS
 * 
 */
class AvatariumResource extends OSKRestResource {
	
	public function dispatch ($req) {
		$next = $this;
		$gamesyncSegment = strtolower($this->getNextRawSegment());
		$this->incrementNextIndex();
		$action = $this->getNextRawSegment();
		if (($gamesyncSegment != "gamestatus" && $gamesyncSegment != "characterinfo") || empty($action)) {
			return $this;
		}
		switch ($gamesyncSegment) {
			case "gamestatus" :
					$next = new AvatariumGameStatus($action);
				break;
			case "characterinfo" :
					$next = new AvatariumCharacterInfo($action);
				break;
			default :
				return $this;
		}
		return $next;
	}

	public function GET ($req, $resp) {
		$resp->setStatusCode(REST_STATUS_BAD_REQUEST);
		$resp->makeError($this, $resp->getStatusCode(), "Bad Request");
	}
}

class AvatariumGameStatus extends OSKSignedRestResource {
	
	private $req, $resp;	// request/response objects
	private $AvatarProfile;	// AvatarProfile object
	private $args;			// class prop array that holds query params and misc data
	
	private $errorFlag;
	
	public function __construct($action) {
		parent::__construct();
		$this->args = array("action" => $action);
		try {
			$this->AvatarProfile = new AvatarProfile();
		} catch (Exception $e) {
			$this->errorFlag = true;
		}
	}
	
	public function triggerFailure() {
		$this->resp->setStatusCode(REST_STATUS_BAD_REQUEST);
		$this->resp->makeError($this, $this->resp->getStatusCode(), "Bad Request");
		return;
	}
	
	public function signedGET ($req, $resp) {
		
		$this->req = $req;
		$this->resp = $resp;
		
		if ($this->errorFlag) {
			$this->triggerFailure();
			return false;
		}

		// retrieve query params
		$this->args = array_merge($this->args, $req->getUri()->getQueryParams()->asArray());
		// set default for output
		$output = (empty($this->args['output'])) ? "json" : "text";
		// prime ret val
		$result = false;
		
		if ($this->args['userID'] === false || empty($this->args['gameHandle'])) {
			$this->triggerFailure();
			return false;
		}

		// execute gamestatus request
		switch ($this->args['action']) {
			case "updateGameStatusLogin" :
				$result = $this->AvatarProfile->updateGameStatusLogin($this->args['userID'], $this->args['gameHandle']);
				break;
			case "updateGameStatusLogout" :
				$result = $this->AvatarProfile->updateGameStatusLogout($this->args['userID'], $this->args['gameHandle']);
				break;
			case "getGameStatusAll" :
				$result = $this->AvatarProfile->getGameStatusAll($this->args['userID']);
				break;
			case "getGameStatus" :
				$result = $this->AvatarProfile->getGameStatus($this->args['userID'], $this->args['gameHandle']);
				break;
			case "getPlayCount" :
				$result = $this->AvatarProfile->getPlayCount($this->args['userID'], $this->args['gameHandle']);
				break;
			case "getPlayTime" :
				$result = $this->AvatarProfile->getPlayTime($this->args['userID'], $this->args['gameHandle']);
				break;
			case "getAveragePlayTime" :
				$result = $this->AvatarProfile->getAveragePlayTime($this->args['userID'], $this->args['gameHandle']);
				break;
			default:
				$this->triggerFailure();
				return false;
		}
		
		// log info
		$infoStr = "userID: {$this->args['userID']}; game: {$this->args['gameHandle']}; time: ".date("Y-m-d G:i:s");
		$GLOBALS['ARLog']->info("AvatariumResource -> gamestatus action: {$this->args['action']}; action result: $result; $infoStr");
		
		// make json or text response
		$json = json_encode(array("result" => $result));
		if ($output == "text") {
			$resp->setContent(CTYPE_TEXT, $json);
		} else {
			$resp->setContent(CTYPE_JSON, $json);
		}
		$resp->setStatusCode(REST_STATUS_OK);
		return;
	}
}

class AvatariumCharacterInfo extends OSKSignedRestResource {
	
	private $req, $resp;
	private $AvatarProfile;
	private $args;
	
	private $errorFlag;
	
	public function __construct($action) {
		parent::__construct();
		$this->args = array("action" => $action);
		try {
			$this->AvatarProfile = new AvatarProfile();
		} catch (Exception $e) {
			$this->errorFlag = true;
		}
	}
	
	public function triggerFailure() {
		$this->resp->setStatusCode(REST_STATUS_BAD_REQUEST);
		$this->resp->makeError($this, $this->resp->getStatusCode(), "Bad Request");
		return;
	}
	
	public function signedGET ($req, $resp) {
		
		$this->req = $req;
		$this->resp = $resp;
		
		if ($this->errorFlag) {
			$this->triggerFailure();
			return false;
		}

		// retrieve query params
		$this->args = array_merge($this->args, $req->getUri()->getQueryParams()->asArray());
		// set default output
		$output = (empty($this->args['output'])) ? "json" : "text";
		// prime retval
		$result = false;
		
		if ($this->args['userID'] === false || empty($this->args['gameHandle']) || $this->args['worldID'] === false || empty($this->args['charName'])) {
			$this->triggerFailure();
			return;
		}
		
		$this->args['charLevel'] = (isset($this->args['charLevel'])) ? $this->args['charLevel'] : 0;
		$this->args['charClass'] = (isset($this->args['charClass'])) ? $this->args['charClass'] : "";
		
		// process request
		switch ($this->args['action']) {
			case "addCharacterInfo" :
				$result = $this->AvatarProfile->addCharacterInfo($this->args['userID'], $this->args);
				break;
			case "getCharacterInfo" :
				$result = $this->AvatarProfile->getCharacterInfo($this->args['userID']);
				break;
			case "updateCharacterInfo" :
				$result = $this->AvatarProfile->updateCharacterInfo($this->args['userID'], $this->args);
				break;
			case "deleteCharacterInfo" :
				$result = $this->AvatarProfile->deleteCharacterInfo($this->args['userID'], $this->args);
				break;
			default:
				$this->triggerFailure();
				return false;
		}
		
		$infoStr = "userID: {$this->args['userID']}; game: {$this->args['gameHandle']}; worldID: {$this->args['worldID']}; charName: {$this->args['charName']}";
		$GLOBALS['ARLog']->info("AvatariumResource -> characterinfo action: {$this->args['action']}; action result: $result; $infoStr");
		
		$json = json_encode(array("result" => $result));
		if ($output == "text") {
			$resp->setContent(CTYPE_TEXT, $json);
		} else {
			$resp->setContent(CTYPE_JSON, $json);
		}
		$resp->setStatusCode(REST_STATUS_OK);
	}	
}

?>
