/*============================================================*/
/*============================================================*/
/*==														==*/
/*==					Menu Routines						==*/
/*==														==*/
/*==				(menu handling routines)				==*/
/*==														==*/
/*============================================================*/
/*============================================================*/


/*========================================================  Includes  */

#include "Globals.h"
#include "UnivUtilities.h"
#include "Menu.h"
#include "IdleRoutines.h"
#include "About.h"
#include "PlayUtils.h"
#include "TeamSetUp.h"
#include "MainWindow.h"
#include "ConfigureSound.h"
#include "PlayerStats.h"
#include "AnimCursor.h"
#include "Show_help.h"
#include "Balloons.h"


/*========================================================  Variables  */

MenuHandle			appleMenuH, fileMenuH, gameMenuH, optionsMenuH;


/*========================================================  Functions  */
/*========================================================  CheckGameMenu  */

void CheckGameMenu (void)

{
	short		i;
	
	for (i = iPracticeBoardin; i <= iTournament; i++)
		CheckItem(gameMenuH, i, FALSE);
	#if kDemoVersion
		CheckItem(gameMenuH, iStandardGame, TRUE);
		DisableItem(gameMenuH, iPracticeBoardin);
		DisableItem(gameMenuH, iPracticeScoring);
		DisableItem(gameMenuH, iTournament);
	#else
		CheckItem(gameMenuH, iPracticeBoardin + whichGame - 1, TRUE);
	#endif
}


/*========================================================  SetMBarToPlaying  */

void SetMBarToPlaying (void)

{
	DisableItem(fileMenuH, iNewGame);
	EnableItem(fileMenuH, iResumeGame);
	EnableItem(fileMenuH, iEndGame);
	
	DisableItem(gameMenuH, iTeamsSetUp);
	DisableItem(gameMenuH, iChooseLeague);
	DisableItem(gameMenuH, iPracticeBoardin);
	DisableItem(gameMenuH, iPracticeScoring);
	DisableItem(gameMenuH, iStandardGame);
	DisableItem(gameMenuH, iTournament);
}


/*========================================================  SetMBarToIdle  */

void SetMBarToIdle (void)

{
	EnableItem(fileMenuH, iNewGame);
	DisableItem(fileMenuH, iResumeGame);
	DisableItem(fileMenuH, iEndGame);
	
	EnableItem(gameMenuH, iTeamsSetUp);
	EnableItem(gameMenuH, iChooseLeague);
	if (!netOnly)
	{
		#if kDemoVersion
			DisableItem(gameMenuH, iPracticeBoardin);
			DisableItem(gameMenuH, iPracticeScoring);
			DisableItem(gameMenuH, iTournament);
		#else
			EnableItem(gameMenuH, iPracticeBoardin);
			EnableItem(gameMenuH, iPracticeScoring);
			EnableItem(gameMenuH, iTournament);
		#endif
	}
	EnableItem(gameMenuH, iStandardGame);
}


/*========================================================  ValidateMenuBar  */

void ValidateMenuBar (void)

{
	Str255		miscString;
	
	if (netOnly)
	{
		DisableItem(gameMenuH, iPracticeBoardin);
		DisableItem(gameMenuH, iPracticeScoring);
		DisableItem(gameMenuH, iTournament);
		GetIndString(miscString, rMiscStrings, 19);
		if (miscString[0] == 0)
			PasStringCopy((StringPtr)"\pMake Net-OnlyÉ", (StringPtr)miscString);
		SetItem(optionsMenuH, iConvert, miscString);
	}
	else
	{
		#if kDemoVersion
			DisableItem(gameMenuH, iPracticeBoardin);
			DisableItem(gameMenuH, iPracticeScoring);
			DisableItem(gameMenuH, iTournament);
			DisableItem(optionsMenuH, iConvert);
		#else
			EnableItem(gameMenuH, iPracticeBoardin);
			EnableItem(gameMenuH, iPracticeScoring);
			EnableItem(gameMenuH, iTournament);
		#endif
		GetIndString(miscString, rMiscStrings, 20);
		if (miscString[0] == 0)
			PasStringCopy((StringPtr)"\pValidate PararenaÉ", (StringPtr)miscString);
		SetItem(optionsMenuH, iConvert, miscString);
	}
}


/*========================================================  DoAppleMenu  */

void DoAppleMenu (short theItem)

{
	Str255		daName;
	GrafPtr		wasPort;
	short		daNumber;
	
	switch (theItem)
	{
		case iAboutItem:
			DoAbout();
			break;
		default:
			GetItem(appleMenuH, theItem, daName);
			GetPort(&wasPort);
			daNumber = OpenDeskAcc(daName);
			SetPort((GrafPtr)wasPort);
			break;
	}
}


/*========================================================  DoFileMenu  */

void DoFileMenu (short theItem)

{
	switch (theItem)
	{
		case iNewGame:
			DoNew();
			break;
		case iResumeGame:
			pausing = FALSE;
			break;
		case iEndGame:
			DoEndGame();
			break;
		case iQuit:
			quitting = TRUE;
			break;
		default:
			break;
	}
}


/*========================================================  DoGameMenu  */

void DoGameMenu (short theItem)

{
	short			i;
	Boolean			sheSaid;
	
	switch (theItem)
	{
		case iTeamsSetUp:
			sheSaid = DoTeamsSetUp();
			if (sheSaid)
			{
				WhosOnFirst();
				UpdateGoalPicts(TRUE);
			}
			break;
		case iChooseLeague:
			sheSaid = ConfigureLeague();
			if (sheSaid)
			{
				WhosOnFirst();
				UpdateGoalPicts(TRUE);
			}
			break;
		case iPracticeBoardin:
			whichGame = kPracticeBoardin;
			break;
		case iPracticeScoring:
			whichGame = kPracticeScoring;
			break;
		case iStandardGame:
			whichGame = kStandardGame;
			break;
		case iTournament:
			whichGame = kTournament;
			break;
		default:
			break;
	}
	
	if ((theItem >= iPracticeBoardin) && (theItem <= iTournament))
	{
		#if kDemoVersion
			whichGame = kStandardGame;
		#endif
		CheckGameMenu();
	}
}


/*========================================================  DoOptionsMenu  */

void DoOptionsMenu (short theItem)

{
	Boolean			sheSaid;
	
	switch (theItem)
	{
		case iInstantReplay:
			sheSaid = ConfigureInstantReplay();
			break;
		case iSound:
			sheSaid = DoConfigureSound();
			break;
		case iNames:
			sheSaid = ConfigureNames();
			break;
		case iPreferences:
			sheSaid = ConfigurePreferences();
			break;
		case iPlayersStats:
			sheSaid = DoPlayerStats();
			break;
		case iRecords:
			sheSaid = DisplayRecords();
			break;
		case iMemory:
			sheSaid = DisplayMemory();
			break;
		case iConvert:
			sheSaid = ConvertPararena();
			if (sheSaid)
			{
				ValidateMenuBar();
				CheckGameMenu();
			}
			break;
		case iHelp:
			Show_help(rHelpDialogID, (StringPtr)"\pParaHelp", rHelpBasePictID, 
					(StringPtr)"\pPararena Help", (StringPtr)"\p");
			InitCursor();
			break;
		default:
			break;
	}
}


/*========================================================  DoMenuChoice  */

void DoMenuChoice (long menuChoice)

{
	short		theMenu, theItem;
	
	if (menuChoice != 0)
	{
		theMenu = HiWrd(menuChoice);
		theItem = LoWrd(menuChoice);
		switch (theMenu)
		{
			case rAppleMenuID:
				DoAppleMenu(theItem);
				break;
			case rFileMenuID:
				DoFileMenu(theItem);
				break;
			case rGameMenuID:
				DoGameMenu(theItem);
				break;
			case rOptionsMenuID:
				DoOptionsMenu(theItem);
				break;
			case kHMHelpMenuID:	
				if (theItem == system7HelpItem)
				{
					Show_help(rHelpDialogID, (StringPtr)"\pParaHelp", rHelpBasePictID, 
							(StringPtr)"\pPararena Help", (StringPtr)"\p");
					InitCursor();
				}
				break;
			default:
				break;
		}
		HiliteMenu(0);
	}
}


/*========================================================  MenuBarInit  */

void MenuBarInit (void)

{
	Str255			miscString, longHelpString;
	MenuHandle		tempMenu;
	OSErr			theErr;
	short			whichItem;
	
	SpinCursor(7);
	
	appleMenuH = GetMenu(rAppleMenuID);
	AddResMenu(appleMenuH, 'DRVR');
	InsertMenu(appleMenuH, 0);
	
	fileMenuH = GetMenu(rFileMenuID);
	InsertMenu(fileMenuH, 0);
	
	gameMenuH = GetMenu(rGameMenuID);
	InsertMenu(gameMenuH, 0);
	whichItem = whichGame + iPracticeBoardin - 1;
	CheckItem(gameMenuH, whichItem, TRUE);
	
	GetIndString(miscString, rMiscStrings, 17);
	if (miscString[0] == 0)
		PasStringCopy((StringPtr)"\pHelpÉ", (StringPtr)miscString);
	
	GetIndString(longHelpString, rMiscStrings, 18);
	if (longHelpString[0] == 0)
		PasStringCopy((StringPtr)"\pPararena HelpÉ", (StringPtr)longHelpString);
	
	optionsMenuH = GetMenu(rOptionsMenuID);
	InsertMenu(optionsMenuH, 0);
	if (thisMac.hasSystem7)
	{
		theErr = HMGetHelpMenuHandle(&tempMenu);
		if ((theErr == noErr) && (tempMenu != kNilPointer))
		{
			AppendMenu(tempMenu, longHelpString);
			system7HelpItem = CountMItems(tempMenu);
		}
		else
		{
			AppendMenu(optionsMenuH, miscString);
		}
	}
	else
	{
		AppendMenu(optionsMenuH, miscString);
	}
	
	ValidateMenuBar();
	
	DrawMenuBar();
}


