/*============================================================*/
/*============================================================*/
/*==														==*/
/*==				Team Set-Up Routines					==*/
/*==														==*/
/*============================================================*/
/*============================================================*/


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

#include "Globals.h"
#include "UnivUtilities.h"
#include "TeamSetUp.h"
#include "MainWindow.h"
#include "Menu.h"


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

Rect			leftStandRect, rightStandRect, teamsLabelRect, teamsPopUpRect;
Rect			iconRects[8];
MenuHandle		teamsPopUp;
short			leftSelected, rightSelected, teamsPopUpWidth, popUpItemSelected;


/*========================================================  Functions  */
/*========================================================  WhosOnFirst  */

void WhosOnFirst (void)

{
	if (leftPlayerNumber == kHumanPlayer)
	{
		leftGoalIsPlayers = TRUE;
		thePlayer.persona = kHumanPlayer;
		theOpponent.persona = rightPlayerNumber;
		thePlayer.whichGoal = kLeftGoal;
		theOpponent.whichGoal = kRightGoal;
		disableBoardCursor = FALSE;
		leftGoalLeague = isLeague;
		if (netGameInSession)
			rightGoalLeague = theirLeague;
		else
			rightGoalLeague = kProfessional;
		
		if (arenaSize == kLargeArena)
		{
			thePlayer.initXPos = kLgInitLeftXPos;
			thePlayer.initZPos = kLgInitLeftZPos;
			theOpponent.initXPos = kLgInitRightXPos;
			theOpponent.initZPos = kLgInitRightZPos;
		}
		else
		{
			thePlayer.initXPos = kSmInitLeftXPos;
			thePlayer.initZPos = kSmInitLeftZPos;
			theOpponent.initXPos = kSmInitRightXPos;
			theOpponent.initZPos = kSmInitRightZPos;
		}
	}
	else if (rightPlayerNumber == kHumanPlayer)
	{
		leftGoalIsPlayers = FALSE;
		thePlayer.persona = kHumanPlayer;
		theOpponent.persona = leftPlayerNumber;
		thePlayer.whichGoal = kRightGoal;
		theOpponent.whichGoal = kLeftGoal;
		disableBoardCursor = FALSE;
		
		rightGoalLeague = isLeague;
		if (netGameInSession)
			leftGoalLeague = theirLeague;
		else
			leftGoalLeague = kProfessional;
		
		if (arenaSize == kLargeArena)
		{
			thePlayer.initXPos = kLgInitRightXPos;
			thePlayer.initZPos = kLgInitRightZPos;
			theOpponent.initXPos = kLgInitLeftXPos;
			theOpponent.initZPos = kLgInitLeftZPos;
		}
		else
		{
			thePlayer.initXPos = kSmInitRightXPos;
			thePlayer.initZPos = kSmInitRightZPos;
			theOpponent.initXPos = kSmInitLeftXPos;
			theOpponent.initZPos = kSmInitLeftZPos;
		}
	}
	else
	{
		leftGoalIsPlayers = TRUE;
		thePlayer.persona = leftPlayerNumber;
		theOpponent.persona = rightPlayerNumber;
		thePlayer.whichGoal = kLeftGoal;
		theOpponent.whichGoal = kRightGoal;
		leftGoalLeague = kProfessional;
		rightGoalLeague = kProfessional;
		disableBoardCursor = TRUE;
		
		if (arenaSize == kLargeArena)
		{
			thePlayer.initXPos = kLgInitLeftXPos;
			thePlayer.initZPos = kLgInitLeftZPos;
			theOpponent.initXPos = kLgInitRightXPos;
			theOpponent.initZPos = kLgInitRightZPos;
		}
		else
		{
			thePlayer.initXPos = kSmInitLeftXPos;
			thePlayer.initZPos = kSmInitLeftZPos;
			theOpponent.initXPos = kSmInitRightXPos;
			theOpponent.initZPos = kSmInitRightZPos;
		}
	}
}


/*========================================================  UpdateLeftStand  */

void UpdateLeftStand (DialogPtr theDialog)

{
	Rect				theRect;
	CIconHandle			theCIcon;
	Handle				itemHandle, theIcon;
	short				itemType;
	
	GetDItem(theDialog, kLeftStandItem, &itemType, &itemHandle, &theRect);
	if (isColor)
	{
		if ((leftSelected == kNetHuman) && (!canNetwork))
			theCIcon = GetCIcon(1009);
		else
			theCIcon = GetCIcon(1001 + leftSelected);
		PlotCIcon(&theRect, theCIcon);
		DisposCIcon(theCIcon);
	}
	else
	{
		if ((leftSelected == kNetHuman) && (!canNetwork))
			theIcon = GetIcon(1009);
		else
			theIcon = GetIcon(1001 + leftSelected);
		PlotIcon(&theRect, theIcon);
	}
}


/*========================================================  UpdateRightStand  */

void UpdateRightStand (DialogPtr theDialog)

{
	Rect				theRect;
	CIconHandle			theCIcon;
	Handle				itemHandle, theIcon;
	short				itemType;
	
	GetDItem(theDialog, kRightStandItem, &itemType, &itemHandle, &theRect);
	if (isColor)
	{
		if ((rightSelected == kNetHuman) && (!canNetwork))
			theCIcon = GetCIcon(1009);
		else
			theCIcon = GetCIcon(1001 + rightSelected);
		PlotCIcon(&theRect, theCIcon);
		DisposCIcon(theCIcon);
	}
	else
	{
		if ((rightSelected == kNetHuman) && (!canNetwork))
			theIcon = GetIcon(1009);
		else
			theIcon = GetIcon(1001 + rightSelected);
		PlotIcon(&theRect, theIcon);
	}
}


/*========================================================  DrawTeamsPopUpItem  */

void DrawTeamsPopUpItem (void)

{
	#define			kBottomMargin	5
	Str255			menuItem;
	short			itemLeftMargin;
	
	InsetRect(&teamsPopUpRect, 1, 1);
	EraseRect(&teamsPopUpRect);
	InsetRect(&teamsPopUpRect, -1, -1);
	
	GetItem(teamsPopUp, popUpItemSelected, (StringPtr)&menuItem);
	itemLeftMargin = (teamsPopUpRect.right - teamsPopUpRect.left - 
			StringWidth(menuItem)) / 2;
	MoveTo(teamsPopUpRect.left + itemLeftMargin, teamsPopUpRect.bottom - kBottomMargin);
	DrawString(menuItem);
}


/*========================================================  DrawTeamsPopUp  */

void DrawTeamsPopUp (void)

{
	#define			kLeftMargin		5
	#define			kRightMargin	5
	
	FrameRect(&teamsPopUpRect);
	MoveTo(teamsPopUpRect.left + 1, teamsPopUpRect.bottom);
	LineTo(teamsPopUpRect.right, teamsPopUpRect.bottom);
	LineTo(teamsPopUpRect.right, teamsPopUpRect.top + 1);
	
	teamsLabelRect.top = teamsPopUpRect.top + 1;
	teamsLabelRect.left = teamsPopUpRect.left - teamsPopUpWidth - 
			kLeftMargin - kRightMargin;
	teamsLabelRect.right = teamsPopUpRect.left;
	teamsLabelRect.bottom = teamsPopUpRect.bottom;
	
	DrawTeamsPopUpItem();
}


/*========================================================  RedrawStandNames  */

void RedrawStandNames (DialogPtr theDialog)

{
	Str255			theName;
	Rect			theRect, eraseArea;
	Handle			itemHandle;
	short			itemType, i, isWide;
	
	TextFont(geneva);
	TextSize(9);
	
	GetDItem(theDialog, kLeftStandItem, &itemType, &itemHandle, &theRect);
	SetRect(&eraseArea, 0, 0, 64, 12);
	OffsetRect(&eraseArea, theRect.left - 16, theRect.bottom + 2);
	EraseRect(&eraseArea);
	GetIndString(theName, rNamesStringID, leftSelected + 1);
	isWide = StringWidth(theName);
	MoveTo(theRect.left + 16 - (isWide / 2), theRect.bottom + 12);
	DrawString(theName);
	
	GetDItem(theDialog, kRightStandItem, &itemType, &itemHandle, &theRect);
	SetRect(&eraseArea, 0, 0, 64, 12);
	OffsetRect(&eraseArea, theRect.left - 16, theRect.bottom + 2);
	EraseRect(&eraseArea);
	GetIndString(theName, rNamesStringID, rightSelected + 1);
	isWide = StringWidth(theName);
	MoveTo(theRect.left + 16 - (isWide / 2), theRect.bottom + 12);
	DrawString(theName);
	
	TextFont(systemFont);
	TextSize(12);
}


/*========================================================  RedrawNetIcon  */

void RedrawNetIcon (DialogPtr theDialog)

{
	Rect			theRect;
	CIconHandle		theCIcon;
	Handle			itemHandle, theIcon;
	short			itemType;
	
	GetDItem(theDialog, kNetIconItem, &itemType, &itemHandle, &theRect);
	if (!canNetwork)
	{
		if (isColor)
		{
			theCIcon = GetCIcon(1009);
			PlotCIcon(&theRect, theCIcon);
			DisposCIcon(theCIcon);
		}
		else
		{
			theIcon = GetIcon(1009);
			PlotIcon(&theRect, theIcon);
		}
	}
	else
	{
		if (isColor)
		{
			theCIcon = GetCIcon(1008);
			PlotCIcon(&theRect, theCIcon);
			DisposCIcon(theCIcon);
		}
		else
		{
			theIcon = GetIcon(1008);
			PlotIcon(&theRect, theIcon);
		}
	}
}


/*========================================================  GrayOutIcons  */

void GrayOutIcons (DialogPtr theDialog)

{
	Rect			theRect;
	Handle			itemHandle;
	short			itemType, i;
	
	PenNormal();
	PenPat(gray);
	PenMode(srcBic);
	#if kDemoVersion
		for (i = 2; i <= 6; i++)
		{
			GetDItem(theDialog, kUserIconItem + i, &itemType, &itemHandle, &theRect);
			PaintRect(&theRect);
		}
	#else
		for (i = 1; i <= 6; i++)
		{
			GetDItem(theDialog, kUserIconItem + i, &itemType, &itemHandle, &theRect);
			PaintRect(&theRect);
		}
	#endif
	PenNormal();
}


/*========================================================  UpdateTeamsDialog  */

void UpdateTeamsDialog (DialogPtr theDialog)

{
	Str255			theName;
	Rect			theRect;
	PicHandle		thePict;
	CIconHandle		theCIcon;
	Handle			itemHandle, theIcon;
	short			itemType, i, isWide;
	
	DrawDialog((GrafPtr)theDialog);
	RedrawNetIcon(theDialog);
	
	GetDItem(theDialog, kPowBalloonItem, &itemType, &itemHandle, &theRect);
	if (isColor)
		thePict = GetPicture(kBalloon4PictID);
	else
		thePict = GetPicture(kBalloon1PictID);
	if (thePict != kNilPointer)
	{
		DrawPicture(thePict, &theRect);
		ReleaseResource((Handle)thePict);
	}
	
	GetDItem(theDialog, kStandsItem, &itemType, &itemHandle, &theRect);
	if (isColor)
		thePict = GetPicture(rTeamsStandPictID4);
	else
		thePict = GetPicture(rTeamsStandPictID1);
	if (thePict != kNilPointer)
	{
		DrawPicture(thePict, &theRect);
		ReleaseResource((Handle)thePict);
	}
	
	GetDItem(theDialog, kSeperatorItem, &itemType, &itemHandle, &theRect);
	if (isColor)
		thePict = GetPicture(rTeamsSeperatorPictID4);
	else
		thePict = GetPicture(rTeamsSeperatorPictID1);
	if (thePict != kNilPointer)
	{
		DrawPicture(thePict, &theRect);
		ReleaseResource((Handle)thePict);
	}
	
	UpdateLeftStand(theDialog);
	UpdateRightStand(theDialog);
	DrawTeamsPopUp();
	
	GetDItem(theDialog, kArenaFrameItem, &itemType, &itemHandle, &theRect);
	FrameRect(&theRect);
	
	GetIndString(theName, rMiscStrings, 13);
	if (theName[0] == 0)
		PasStringCopy((StringPtr)"\pSelect OpponentÉ", (StringPtr)theName);
	MoveTo(kTeamsTitleLeft, kTeamsTitleTop);
	DrawString(theName);
	
	TextFont(geneva);
	TextSize(9);
	for (i = 1; i < 9; i++)
	{
		GetDItem(theDialog, i + 3, &itemType, &itemHandle, &theRect);
		GetIndString(theName, rNamesStringID, i);
		isWide = StringWidth(theName);
		MoveTo(theRect.left + 16 - (isWide / 2), theRect.bottom + 12);
		DrawString(theName);
	}
	TextFont(systemFont);
	TextSize(12);
	
	if (netOnly)
		GrayOutIcons(theDialog);
	
	#if kDemoVersion
		GrayOutIcons(theDialog);
	#endif
	
	RedrawStandNames(theDialog);
	
	DrawDefaultButton(theDialog, kButtItemOkay);
}


/*========================================================  DoDragIcon  */

void DoDragIcon (DialogPtr theDialog, Point mousePt, short whichIcon)

{
	Rect			srcRect, dragRect;
	Point			dragPt;
	
	srcRect = iconRects[whichIcon];
	
	PenMode(patOr);
	PenPat(gray);
	PaintRect(&srcRect);
	
	dragRect = srcRect;
	PenMode(patXor);
	FrameRect(&dragRect);
	
	while (Button())
	{
		GetMouse(&dragPt);
		if ((dragPt.h != mousePt.h) || (dragPt.v != mousePt.v))
		{
			FrameRect(&dragRect);
			if (PtInRect(mousePt, &leftStandRect) != 
					PtInRect(dragPt, &leftStandRect))
			{
				PenPat(black);
				PenSize(2, 2);
				InsetRect(&leftStandRect, -3, -3);
				FrameRect(&leftStandRect);
				InsetRect(&leftStandRect, 3, 3);
				PenSize(1, 1);
				PenPat(gray);
			}
			else if (PtInRect(mousePt, &rightStandRect) != 
					PtInRect(dragPt, &rightStandRect))
			{
				PenPat(black);
				PenSize(2, 2);
				InsetRect(&rightStandRect, -3, -3);
				FrameRect(&rightStandRect);
				InsetRect(&rightStandRect, 3, 3);
				PenSize(1, 1);
				PenPat(gray);
			}
			OffsetRect(&dragRect, dragPt.h - mousePt.h, dragPt.v - mousePt.v);
			FrameRect(&dragRect);
			mousePt = dragPt;
		}
	}
	
	FrameRect(&dragRect);
	DrawDialog(theDialog);
	if (netOnly)
		GrayOutIcons(theDialog);
	#if kDemoVersion
		GrayOutIcons(theDialog);
	#endif
	RedrawNetIcon(theDialog);
	
	if (PtInRect(dragPt, &leftStandRect))
	{
		PenPat(black);
		PenSize(2, 2);
		InsetRect(&leftStandRect, -3, -3);
		FrameRect(&leftStandRect);
		InsetRect(&leftStandRect, 3, 3);
		PenNormal();
		leftSelected = whichIcon;
		UpdateLeftStand(theDialog);
	}
	else if (PtInRect(dragPt, &rightStandRect))
	{
		PenPat(black);
		PenSize(2, 2);
		InsetRect(&rightStandRect, -3, -3);
		FrameRect(&rightStandRect);
		InsetRect(&rightStandRect, 3, 3);
		PenNormal();
		rightSelected = whichIcon;
		UpdateRightStand(theDialog);
	}
	else
	{
		if (rightSelected == kHumanPlayer)
		{
			if (whichIcon != kHumanPlayer)
			{
				leftSelected = whichIcon;
				UpdateLeftStand(theDialog);
			}
		}
		else if (leftSelected == kHumanPlayer)
		{
			if (whichIcon != kHumanPlayer)
			{
				rightSelected = whichIcon;
				UpdateRightStand(theDialog);
			}
		}
		else
		{
			if (whichIcon == kHumanPlayer)
			{
				leftSelected = whichIcon;
				UpdateLeftStand(theDialog);
			}
			else
			{
				rightSelected = whichIcon;
				UpdateRightStand(theDialog);
			}
		}
	}
	
	PenNormal();
	RedrawStandNames(theDialog);
}


/*========================================================  SwapLeftAndRightSelected  */

void SwapLeftAndRightSelected (DialogPtr theDialog)

{
	short		tempShort;
	
	tempShort = leftSelected;
	leftSelected = rightSelected;
	rightSelected = tempShort;
	
	UpdateLeftStand(theDialog);
	UpdateRightStand(theDialog);
	RedrawStandNames(theDialog);
}


/*========================================================  DoTeamsPopUpSelect  */

void DoTeamsPopUpSelect (void)

{
	Point			popUpperLeft;
	long			theChoice;
	
	popUpperLeft.v = teamsPopUpRect.top + 1;
	popUpperLeft.h = teamsPopUpRect.left;
	LocalToGlobal(&popUpperLeft);
	theChoice = PopUpMenuSelect(teamsPopUp, popUpperLeft.v, popUpperLeft.h, 
			popUpItemSelected);
	if (LoWrd(theChoice) > 0)
	{
		popUpItemSelected = LoWrd(theChoice);
		DrawTeamsPopUpItem();
	}
}


/*========================================================  TeamsFilter  */

pascal Boolean TeamsFilter (DialogPtr theDialog, EventRecord *theEvent, short *itemHit)

{
	Point			mousePt;
	Handle			itemHandle;
	short			i, iconSelected, itemType;
	Boolean			eventHandled;
	
	eventHandled = FALSE;
	
	switch (theEvent->what)
	{
		case keyDown:
			switch ((theEvent->message) & charCodeMask)
			{
				case kReturnKeyASCII:
					FlashDialogButton(theDialog, kButtItemOkay);
					*itemHit = kButtItemOkay;
					eventHandled = TRUE;
					break;
				default:
					eventHandled = FALSE;
			}
			break;
		case mouseDown:
			mousePt = theEvent->where;
			GlobalToLocal(&mousePt);
			
			if (PtInRect(mousePt, &teamsPopUpRect))
			{
				DoTeamsPopUpSelect();
				eventHandled = TRUE;
			}
			else if ((PtInRect(mousePt, &leftStandRect)) || 
					(PtInRect(mousePt, &rightStandRect)))
			{
				SwapLeftAndRightSelected(theDialog);
			}
			else
			{
				iconSelected = -1;
				for (i = 0; i < 8; i++)
				{
					if (PtInRect(mousePt, &iconRects[i]))
						iconSelected = i;
				}
				if ((iconSelected == 7) && (!canNetwork))
					iconSelected = -1;
				if ((netOnly) && (iconSelected >= 1) && (iconSelected <= 6))
					iconSelected = -1;
				#if kDemoVersion
					if ((iconSelected >= 2) && (iconSelected <= 6))
						iconSelected = -1;
				#endif
				if (iconSelected != -1)
				{
					DoDragIcon(theDialog, mousePt, iconSelected);
					eventHandled = TRUE;
				}
				else
					eventHandled = FALSE;
			}
			break;
		case mouseUp:
			mousePt = theEvent->where;
			GlobalToLocal(&mousePt);
			break;
		case updateEvt:
			BeginUpdate((GrafPtr)theDialog);
			UpdateTeamsDialog(theDialog);
			EndUpdate((GrafPtr)theDialog);
			theEvent->what = nullEvent;
			eventHandled = FALSE;
			break;
		case nullEvent:
			mousePt = theEvent->where;
			GlobalToLocal(&mousePt);
			iconSelected = -1;
			for (i = 0; i < 8; i++)
			{
				if (PtInRect(mousePt, &iconRects[i]))
					iconSelected = i;
			}
			if ((iconSelected == 7) && (!canNetwork))
				iconSelected = -1;
			if ((netOnly) && (iconSelected >= 1) && (iconSelected <= 6))
				iconSelected = -1;
			#if kDemoVersion
				if ((iconSelected >= 2) && (iconSelected <= 6))
					iconSelected = -1;
			#endif
			if (iconSelected != -1)
				SetCursor(&handCursor);
			else
				SetCursor(&arrow);
			eventHandled = FALSE;
			break;
		default:
			eventHandled = FALSE;
			break;
	}
	
	return (eventHandled);
}


/*========================================================  DoTeamsSetUp  */

Boolean DoTeamsSetUp (void)

{
	#define				kSelectString		15
	#define				kNetOnlyString		16
	#define				kDemoSelectString	25
	Str255				tempString;
	DialogPtr			teamsDialog;
	Handle				iHandle;
	Rect				iRect;
	short				iType, itemHit, i;
	Boolean				leaveDialog, userOkayed, tempBool;
	
	#if kDemoVersion
		GetIndString(tempString, rMiscStrings, kDemoSelectString);
	#else
		if (netOnly)
			GetIndString(tempString, rMiscStrings, kNetOnlyString);
		else
			GetIndString(tempString, rMiscStrings, kSelectString);
	#endif
	if (tempString[0] != 0)
		ParamText(tempString, "\p", "\p", "\p");
	else
		ParamText(tempString, "\pSelect two players to play one another", "\p", "\p");
	
	CenterDialog(rTeamsDialogID);
	if (doZooms)
		ZoomOutDialogRect(rTeamsDialogID);
	teamsDialog = GetNewDialog(rTeamsDialogID, kNilPointer, kPutInFront);
	if (teamsDialog == kNilPointer)
		DeathError(kErrNoResource);
	
	teamsPopUp = GetMenu(kTeamsPopMenuID);
	if (teamsPopUp == kNilPointer)
		DeathError(kErrNoResource);
	
	InsertMenu(teamsPopUp, -1);
	HLock((Handle)teamsPopUp);
	teamsPopUpWidth = StringWidth((**teamsPopUp).menuData);
	HUnlock((Handle)teamsPopUp);
	
	for (i = 0; i < 10; i++)
	{
		SetItem(teamsPopUp, i + 1, (StringPtr)theNames[i]);
	}
	
	popUpItemSelected = whichHumanNumber;
	
	leftSelected = leftPlayerNumber;
	rightSelected = rightPlayerNumber;
	
	GetDItem(teamsDialog, kCheckTeamsAuto, &iType, &iHandle, &iRect);
	SetCtlValue((ControlHandle)iHandle, (short)autoTeamsDialog);
	
	GetDItem(teamsDialog, kUserPopUpItem, &iType, &iHandle, &teamsPopUpRect);
	
	GetDItem(teamsDialog, kLeftStandItem, &iType, &iHandle, &leftStandRect);
	GetDItem(teamsDialog, kRightStandItem, &iType, &iHandle, &rightStandRect);
	for (i = 0; i < 8; i++)
		GetDItem(teamsDialog, kUserIconItem + i, &iType, &iHandle, &iconRects[i]);
	
	SetPort((GrafPtr)teamsDialog);			/* Bring up the dialog */
	ShowWindow((GrafPtr)teamsDialog);
	
	UpdateTeamsDialog(teamsDialog);
	
	leaveDialog = FALSE;
	
	while (leaveDialog == FALSE)
	{
		ModalDialog(TeamsFilter, &itemHit);
		
		switch (itemHit)
		{
			case kButtItemOkay:
				if ((leftSelected == kHumanPlayer) && (rightSelected == kHumanPlayer))
				{
					MinorError(kMinor2HumanPlayers);
				}
				else if (((leftSelected == kNetHuman) && (rightSelected != kHumanPlayer)) ||
						((leftSelected != kHumanPlayer) && (rightSelected == kNetHuman)))
				{
					MinorError(kMinor2NetPlayers);
				}
				else
				{
					userOkayed = TRUE;
					leaveDialog = TRUE;
					whichHumanNumber = popUpItemSelected;
					leftPlayerNumber = leftSelected;
					rightPlayerNumber = rightSelected;
					GetDItem(teamsDialog, kCheckTeamsAuto, &iType, &iHandle, &iRect);
					autoTeamsDialog = (Boolean)GetCtlValue((ControlHandle)iHandle);
				}
				break;
			case kButtItemCancel:
				userOkayed = FALSE;
				leaveDialog = TRUE;
				break;
			case kCheckTeamsAuto:
				GetDItem(teamsDialog, itemHit, &iType, &iHandle, &iRect);
				tempBool = (Boolean)GetCtlValue((ControlHandle)iHandle);
				tempBool = !tempBool;
				SetCtlValue((ControlHandle)iHandle, (short)tempBool);
				break;
			default:
				break;
		}
	}
	
	InitCursor();
	DisposDialog(teamsDialog);
	return (userOkayed);
}

