/* $Id: helpqueue.src 798 2006-09-12 21:19:26Z austinheilman $
 *
 */

use uo;
use os;

include ":help:queue-gumps";

program HelpQueue(mobile, queue_name)

	var gump, input, page_id;
	var cur_menu := QUEUE_TYPES_BTN;
	if ( queue_name )
		if ( CanAccessQueue(queue_name, mobile.cmdlevel) )
			cur_menu := PAGE_LIST_BTN;
		endif
	endif

	while ( mobile.connected )
		if ( cur_menu == QUEUE_TYPES_BTN )
			gump := ShowQueueTypes(mobile.cmdlevel);
		elseif ( cur_menu == PAGE_LIST_BTN )
			gump := ShowPagesInQueue(queue_name, mobile.cmdlevel);
		elseif ( cur_menu == SHOW_PAGE_BTN )
			gump := ShowPageInQueue(queue_name, page_id, mobile.cmdlevel);
		endif

		input := GFSendGump(mobile, gump);
		if ( !input[0] || input[CLOSE_BTN] )
			SendSysMessage(mobile, "Cancelled.");
			break;
		elseif ( input[0] >= SHOW_PAGE_START )
			var key := input[0] - SHOW_PAGE_START;
			page_id := key;
			cur_menu := SHOW_PAGE_BTN;
		elseif ( input[0] >= SHOW_QUEUE_START )
			var key := input[0] - SHOW_QUEUE_START;
			var queues := GetQueueNamesAccess(mobile.cmdlevel);
			queue_name := queues[key];
			cur_menu := PAGE_LIST_BTN;
		else
			var result := PageButtons(mobile, input, cur_menu, queue_name, page_id);
			if ( result )
				break;
			endif
		endif

		SleepMS(2);
	endwhile

	return 1;
endprogram

function PageButtons(mobile, byref input, byref cur_menu, byref queue_name, byref page_id)
	if ( input[EMAIL_BTN] )
		var script := Start_Script(":email:email", array{mobile, mobile.serial, page_id, queue_name+" page response."});
		if ( script.errortext )
			GumpPrompt(mobile, "Error - "+script.errortext);
		endif
		return 1;
	elseif ( input[DELETE_PAGE_BTN] )
		cur_menu := PAGE_LIST_BTN;
		var data_file := GetHelpDataFile();
		var data_elem := DFFindElement(data_file, "Queue-"+queue_name, DF_NO_CREATE);
		data_elem.EraseProp(Hex(CInt(page_id)));
		SendSysMessage(mobile, "Page deleted.");
		return 0;
	elseif ( mobile.cmdlevel < 3 )
		return 0;
		// GM and above only buttons after this point.
	elseif ( input[GOTO_BTN] )
		var pager := SystemFindObjectBySerial(page_id);
		SendSysMessage(mobile, "Going to "+pager.name);
		MoveObjectToLocation(mobile, pager.x, pager.y, pager.z, pager.realm, MOVEOBJECT_FORCELOCATION);
		return 1;
	elseif ( input[SUMMON_BTN] )
		var pager := SystemFindObjectBySerial(page_id);
		SendSysMessage(mobile, "Bringing "+pager.name+" to your location.");
		MoveObjectToLocation(pager, mobile.x, mobile.y, mobile.z, mobile.realm, MOVEOBJECT_FORCELOCATION);
		return 1;
	elseif ( input[INFO_BTN] )
		var script := Start_Script(":commands:gm/info", array{mobile, page_id});
		if ( script.errortext )
			GumpPrompt(mobile, "Error - "+script.errortext);
		endif
		return 1;
	elseif ( input[ACCTINFO_BTN] )
		var pager := SystemFindObjectBySerial(page_id);
		var script := Start_Script(":accounts:commands/gm/acctinfo", array{mobile, pager.acctname});
		if ( script.errortext )
			GumpPrompt(mobile, "Error - "+script.errortext);
		endif
		return 1;
	endif

	return 0;
endfunction

function CanAccessQueue(byref queue_name, cmdlevel)
	queue_name := Lower(queue_name);
	foreach name in ( GetQueueNamesAccess(cmdlevel) )
		if ( Lower(name) == queue_name )
			// Sets it to the proper case.
			queue_name := name;
			return 1;
		endif
		SleepMS(2);
	endforeach

	return 0;
endfunction

function GetQueueNamesAccess(cmdlevel)
	var names := array;

	if ( cmdlevel >= 1 ) // Counselor +
		names.Append("Counselor");
	endif

	if ( cmdlevel >= 2 ) // Seer +
		names.Append("Staff");
	endif

	return names;
endfunction
