<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/SuperChat.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyUser.inc.php" );

	class ChatService extends Widget {
		protected $cm = null;
		protected $user_id = 0;

		protected function commandLogin() {
			if (!isset( $this->aP["chatroom_id"] )) return false;
			if (!$this->user_id) return false;

			$loginretval = $this->cm->loginUser( $this->aP["chatroom_id"] , $this->user_id );
			if (!$loginretval) return false;
			$retval = array();
			$retval["login"]=true;
			$retval["chatroom_users"]=$this->cm->getChatroomUsersOnMultiRooms( array( $this->aP["chatroom_id"] ) );
			$retval["to_eval"]=sprintf( "RefreshChatroomUsersCallback( '%s' , %s );",
				$this->aP["id"],
				json_encode( $retval["chatroom_users"] ) );
			return $retval;
		}

		protected function commandRefreshUsers() {
			$retval = array();
			$retval["chatroom_users"]=$this->cm->getChatroomUsersOnMultiRooms(
				$this->cm->getVisibleChatroomids() );
			$retval["to_eval"]=sprintf( "RefreshChatroomUsersCallback( '%s' , %s );",
				$this->aP["id"],
				json_encode( $retval["chatroom_users"] ) );
			return $retval;
		}

		protected function commandMessage() {
			$target_user_id = 0;

			$this->cm->postChatroomMessage(
				$this->aP["message"],
				$this->aP["chatroom_id"],
				0,
				$this->user_id,
				$target_user_id );

			$retval = array();
			return $retval;
		}

		protected function commandWaitMessages() {
			$this->cm->keepaliveUser( $this->user_id );
			$timeout = 40;
			$retval=array( "to_eval" => "" );
			$messages = $this->cm->getLatestMessagesForUser( $this->user_id , $this->aP["last_chat_message_id"] );
			while (!$messages && $timeout) {
				$timeout--;
				usleep( 500000 );
				$messages = $this->cm->getLatestMessagesForUser( $this->user_id , $this->aP["last_chat_message_id"] );
			}
			if ($messages) {
				// $retval["messages"]=$messages;
				$retval["to_eval"].=sprintf( "RefreshChatMessagesCallback( '%s' , %s );",
					$this->aP["id"],
					json_encode( $messages ) );
			}
			$retval["to_eval"].=sprintf( "ChatNewMessages( '%s' );" , $this->aP["id"] );
			return $retval;
		}

		public function handleEvents() {
			if (!isset( $this->aP["action"] )) return parent::handleEvents();
			if ($this->aP["action"]!="chat") return parent::handleEvents();
			if (!isset( $this->aP["command"] )) return parent::handleEvents();
			// this is a chat related event
			$methodname = "command".$this->aP["command"];
			if (!method_exists( $this , $methodname )) return parent::handleEvents();
			$retval = $this->$methodname();
			$err = error_get_last();
			if (!isset( $err )) {
				FNTH_echo_json_response( $retval , 0 , "fo3_exit" );
			} else {
				$err["error"]=true;
				FNTH_echo_json_response(
					sprintf( "console.log( %s );" , json_encode( $err ) ), 0 , "fo3_exit" );
			}
		}

		public function __construct( $id = "ChatService" , $parent = NULL ) {
			parent::__construct( $id , $parent );
			$this->cm = new SuperChatModel();
			$mum = new MyUserModel();
			$this->user_id = $mum->getLoggedInUserId();
		}

		public function render() {
			return "error!";
		}

	}

?>
