<?php
	define( 'MESSAGE_FLAG_BOLD' , 1 );

	class ChatModel {
		
		public function getVisibleChatrooms() {
			// CHECKED
			$query = sprintf( "select * from chatrooms where is_visible order by chatroom_id" );
			return FNTH_db_get_resultset( $query );
		}
		
		public function getVisibleChatroomIds() {
			// CHECKED
			$query = sprintf( "select chatroom_id from chatrooms where is_visible order by chatroom_id" );
			return FNTH_db_get_column( $query , "chatroom_id" );
		}
		
		public function getLastChatMessageId() {
			// CHECKED
			$query = sprintf( "select chat_message_id from chat_messages order by chat_message_id desc limit 1" );
			$retval = FNTH_db_get_field( $query , "chat_message_id" );
			return ($retval) ? $retval : 0;
		}

		public function postChatroomSystemMessage( $message , $chatroom_id , $message_flags = 0 , $poster_user_id = 0 , $target_user_id = 0 , $system = true ) {
			// CHECKED
			$this->postChatroomMessage( $message , $chatroom_id , $message_flags , $poster_user_id , $target_user_id , $system );
			return true;

		}

		public function postChatroomMessage( $message , $chatroom_id , $message_flags , $poster_user_id = 0 , $target_user_id = 0 , $system = false ) {
			// CHECKED
			$ins = array();
			$ins["chatroom_id"]=sprintf( "%d" , $chatroom_id );
			if ($message_flags) $ins["message_flags"]=sprintf( "%d" , $message_flags );
			if ($target_user_id) $ins["target_user_id"]=sprintf( "%d" , $target_user_id );
			if ($poster_user_id) $ins["poster_user_id"]=sprintf( "%d" , $poster_user_id );
			$ins["is_system"]=($system) ? "true" : "false";
			$ins["message_text"]=sprintf( "'%s'" , FNTH_db_addslashes( $message ) );
			FNTH_db_free_result( FNTH_db_query( FNTH_db_generate_insert_query( "chat_messages" , $ins ) ) );
			return true;
		}

		public function loginUser( $chatroom_id , $user_id ) {
			// CHECKED
			$query = sprintf( "select * from chatroom_users where chatroom_id=%d and user_id = %d" , $chatroom_id , $user_id );
			$row = FNTH_db_get_row( $query );
			if ($row) return true;		// already logged in

			// insert user into chatroom
			$ins = array();
			$ins["chatroom_id"]=sprintf( "%d" , $chatroom_id );
			$ins["user_id"]=sprintf( "%d" , $user_id );
			FNTH_db_free_result(
				FNTH_db_query(
					FNTH_db_generate_insert_query( "chatroom_users" , $ins )
				)
			);

			// TODO now insert system message
			$query = sprintf( "select * from users where user_id=%d" , $user_id );
			$user = FNTH_db_get_row( $query );

			$this->postChatroomMessage(
				sprintf( "%s %s" , $user["user_name"] ,  __("logged_in") ), // message
				$chatroom_id,												// chatroom_id
				MESSAGE_FLAG_BOLD,											// message flags
				0,															// poster_user (system)
				0 );														// target user (everybody)
			$this->postChatroomSystemMessage( "refresh_users", $chatroom_id );
			return true;
		}

		public function logoutUser( $chatroom_id , $user_id ) {
			// CHECKED
			$query = sprintf( "select * from chatroom_users where chatroom_id=%d and user_id = %d" , $chatroom_id , $user_id );
			$row = FNTH_db_get_row( $query );
			if (!$row) return false;		// not in the room
			FNTH_db_free_result(
				FNTH_db_query(
					sprintf( "delete from chatroom_users where chatroom_id=%d and user_id=%d", $chatroom_id , $user_id ) ) );

			// TODO now insert system message
			$query = sprintf( "select * from users where user_id=%d" , $user_id );
			$user = FNTH_db_get_row( $query );
			$this->postChatroomMessage(
				sprintf( "%s %s" , $user["user_name"] ,  __("logged_out") ),// message
				$chatroom_id,												// chatroom_id
				MESSAGE_FLAG_BOLD,											// message flags
				0,															// poster_user (system)
				0 );														// target user (everybody)
			$this->postChatroomSystemMessage( "refresh_users", $chatroom_id );
			return true;
		}

		public function purgeChatrooms() {
			// CHECKED
			$timeout = 40;	// $GLOBALS["session_timeout"];
			$query = sprintf( "select chatroom_id,user_id from chatroom_users where extract( epoch from mtstamp ) < %d",
				fo3_time()-$timeout );
			$ds = FNTH_db_get_resultset( $query );
			if (empty( $ds )) return;
			foreach ($ds as $r)
				$this->logoutUser( $r["chatroom_id"] , $r["user_id"] );
		}

		public function keepaliveUser( $user_id ) {
			// CHECKED
			$query = sprintf( "select * from chatroom_users where user_id = %d", $user_id );
			$row = FNTH_db_get_row( $query );
			if (!$row) return false;
			$query = sprintf( "update chatroom_users set mtstamp=now() where user_id = %d" , $user_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
			return true;
		}

		public function getChatroomUsers( $chatroom_id ) {
			// CHECKED
			$query = sprintf( "select cu.user_id,u.user_name
				from chatroom_users as cu,users as u
				where cu.user_id=u.user_id and cu.chatroom_id=%d" , $chatroom_id );
			return FNTH_db_get_resultset( $query );
		}
		
		public function getChatroomUsersOnMultiRooms( $chatroom_ids = array() ) {
			// CHECKED
			$query = sprintf( "select cu.chatroom_id, cu.user_id,u.user_name
				from chatroom_users as cu,users as u
				where cu.user_id=u.user_id and cu.chatroom_id in (%s)" , implode( "," , $chatroom_ids ) );
			$ds = FNTH_db_get_resultset( $query );
			if (empty( $ds )) return array();
			$retval = array();
			foreach ($ds as $row) {
				$cr_id = $row["chatroom_id"];
				unset( $row["chatroom_id"] );
				if (!isset( $retval[$cr_id] )) $retval[$cr_id]=array();
				$retval[$cr_id][$row["user_id"]]=$row;
			}
			return $retval;
		}
	}

?>
