/*============================================================*/
/*============================================================*/
/*==														==*/
/*==				Player Statistics Routines				==*/
/*==														==*/
/*============================================================*/
/*============================================================*/


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

#include "Globals.h"
#include "UnivUtilities.h"
#include "PlayerStats.h"


/*========================================================  Functions  */
/*========================================================  EraseStats  */

void EraseStats (short whoBitesIt)

{
	theStats[whoBitesIt - 1].tournamentTime = 0L;
	theStats[whoBitesIt - 1].bestPlatinumTime = 0L;
	theStats[whoBitesIt - 1].copperTitles = 0;
	theStats[whoBitesIt - 1].bronzeTitles = 0;
	theStats[whoBitesIt - 1].silverTitles = 0;
	theStats[whoBitesIt - 1].goldTitles = 0;
	theStats[whoBitesIt - 1].platinumTitles = 0;
	
	theStats[whoBitesIt - 1].played = 0;
	theStats[whoBitesIt - 1].won = 0;
	theStats[whoBitesIt - 1].goals = 0;
	theStats[whoBitesIt - 1].fouls = 0;
	theStats[whoBitesIt - 1].criticals = 0;
	theStats[whoBitesIt - 1].oppGoals = 0;
	theStats[whoBitesIt - 1].oppFouls = 0;
	theStats[whoBitesIt - 1].oppCriticals = 0;
	theStats[whoBitesIt - 1].georgeState = kUnplayedState;
	theStats[whoBitesIt - 1].maraState = kUnplayedState;
	theStats[whoBitesIt - 1].ottoState = kUnplayedState;
	theStats[whoBitesIt - 1].claireState = kUnplayedState;
	theStats[whoBitesIt - 1].eazeState = kUnplayedState;
	theStats[whoBitesIt - 1].teakState = kUnplayedState;
	
	teaksThreshholds[whoBitesIt - 1] = 50;
}


/*========================================================  DrawStatsPopUpItem  */

void DrawStatsPopUpItem (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);
}


/*========================================================  DrawStatsPopUp  */

void DrawStatsPopUp (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);
	
	DrawStatsPopUpItem();
}


/*========================================================  RedrawStatValues  */

void RedrawStatValues (void)

{
	Str255			tempStr;
	Rect			tempRect;
	PicHandle		checkPict, xPict;
	short			tempShort, i;
	
	if (!isColor)
		TextMode(srcXor);
	
	PenMode(patOr);
	PenPat(gray);
	SetRect(&tempRect, 0, 0, 60, 35);
	OffsetRect(&tempRect, 60, 72);
	
	if (theStats[popUpItemSelected - 1].copperTitles == 0)
		PaintRect(&tempRect);
	else
	{
		ForeColor(yellowColor);
		MoveTo(tempRect.right - 16, tempRect.bottom - 2);
		NumToString((long)theStats[popUpItemSelected - 1].copperTitles, tempStr);
		DrawString(tempStr);
		ForeColor(blackColor);
	}
	OffsetRect(&tempRect, 66, 0);
	if (theStats[popUpItemSelected - 1].bronzeTitles == 0)
		PaintRect(&tempRect);
	else
	{
		ForeColor(yellowColor);
		MoveTo(tempRect.right - 16, tempRect.bottom - 2);
		NumToString((long)theStats[popUpItemSelected - 1].bronzeTitles, tempStr);
		DrawString(tempStr);
		ForeColor(blackColor);
	}
	OffsetRect(&tempRect, 66, 0);
	if (theStats[popUpItemSelected - 1].silverTitles == 0)
		PaintRect(&tempRect);
	else
	{
		ForeColor(yellowColor);
		MoveTo(tempRect.right - 16, tempRect.bottom - 2);
		NumToString((long)theStats[popUpItemSelected - 1].silverTitles, tempStr);
		DrawString(tempStr);
		ForeColor(blackColor);
	}
	OffsetRect(&tempRect, 66, 0);
	if (theStats[popUpItemSelected - 1].goldTitles == 0)
		PaintRect(&tempRect);
	else
	{
		ForeColor(yellowColor);
		MoveTo(tempRect.right - 16, tempRect.bottom - 2);
		NumToString((long)theStats[popUpItemSelected - 1].goldTitles, tempStr);
		DrawString(tempStr);
		ForeColor(blackColor);
	}
	OffsetRect(&tempRect, 66, 0);
	if (theStats[popUpItemSelected - 1].platinumTitles == 0)
		PaintRect(&tempRect);
	else
	{
		ForeColor(yellowColor);
		MoveTo(tempRect.right - 16, tempRect.bottom - 2);
		NumToString((long)theStats[popUpItemSelected - 1].platinumTitles, tempStr);
		DrawString(tempStr);
		ForeColor(blackColor);
	}
	
	PenNormal();
	
	SetRect(&tempRect, 0, 0, 76, 17);
	
	OffsetRect(&tempRect, 148, 120);
	FillRect(&tempRect, black);
	MoveTo(tempRect.left + 6, tempRect.bottom - 4);
	NumToString((long)theStats[popUpItemSelected - 1].played, tempStr);
	ForeColor(cyanColor);
	DrawString(tempStr);
	ForeColor(blackColor);
	
	OffsetRect(&tempRect, 0, 18);
	FillRect(&tempRect, black);
	MoveTo(tempRect.left + 6, tempRect.bottom - 4);
	NumToString((long)theStats[popUpItemSelected - 1].won, tempStr);
	ForeColor(cyanColor);
	DrawString(tempStr);
	ForeColor(blackColor);
	
	OffsetRect(&tempRect, 0, 18);
	FillRect(&tempRect, black);
	MoveTo(tempRect.left + 6, tempRect.bottom - 4);
	tempShort = theStats[popUpItemSelected - 1].played - 
			theStats[popUpItemSelected - 1].won;
	NumToString((long)tempShort, tempStr);
	ForeColor(cyanColor);
	DrawString(tempStr);
	ForeColor(blackColor);
	
	OffsetRect(&tempRect, 0, 18);
	FillRect(&tempRect, black);
	MoveTo(tempRect.left + 6, tempRect.bottom - 4);
	if (theStats[popUpItemSelected - 1].played != 0)
		tempShort = (short)((long)theStats[popUpItemSelected - 1].won * 100L / 
				(long)theStats[popUpItemSelected - 1].played);
	else
		tempShort = 0;
	NumToString((long)tempShort, tempStr);
	ForeColor(cyanColor);
	DrawString(tempStr);
	ForeColor(blackColor);
	
	OffsetRect(&tempRect, 0, 22);
	FillRect(&tempRect, black);
	MoveTo(tempRect.left + 6, tempRect.bottom - 4);
	NumToString((long)theStats[popUpItemSelected - 1].goals, tempStr);
	ForeColor(redColor);
	DrawString(tempStr);
	ForeColor(blackColor);
	
	OffsetRect(&tempRect, 0, 18);
	FillRect(&tempRect, black);
	MoveTo(tempRect.left + 6, tempRect.bottom - 4);
	NumToString((long)theStats[popUpItemSelected - 1].fouls, tempStr);
	ForeColor(redColor);
	DrawString(tempStr);
	ForeColor(blackColor);
	
	OffsetRect(&tempRect, 0, 18);
	FillRect(&tempRect, black);
	MoveTo(tempRect.left + 6, tempRect.bottom - 4);
	NumToString((long)theStats[popUpItemSelected - 1].criticals, tempStr);
	ForeColor(redColor);
	DrawString(tempStr);
	ForeColor(blackColor);
	
	OffsetRect(&tempRect, 0, 22);
	FillRect(&tempRect, black);
	MoveTo(tempRect.left + 6, tempRect.bottom - 4);
	NumToString((long)theStats[popUpItemSelected - 1].oppGoals, tempStr);
	ForeColor(magentaColor);
	DrawString(tempStr);
	ForeColor(blackColor);
	
	OffsetRect(&tempRect, 0, 18);
	FillRect(&tempRect, black);
	MoveTo(tempRect.left + 6, tempRect.bottom - 4);
	NumToString((long)theStats[popUpItemSelected - 1].oppFouls, tempStr);
	ForeColor(magentaColor);
	DrawString(tempStr);
	ForeColor(blackColor);
	
	OffsetRect(&tempRect, 0, 18);
	FillRect(&tempRect, black);
	MoveTo(tempRect.left + 6, tempRect.bottom - 4);
	NumToString((long)theStats[popUpItemSelected - 1].oppCriticals, tempStr);
	ForeColor(magentaColor);
	DrawString(tempStr);
	ForeColor(blackColor);
	
	if (isColor)
	{
		checkPict = GetPicture(kCheck4PictID);
		xPict = GetPicture(kX4PictID);
	}
	else
	{
		checkPict = GetPicture(kCheck1PictID);
		xPict = GetPicture(kX1PictID);
	}
	
	if ((checkPict != kNilPointer) && (xPict != kNilPointer))
	{
		HLock((Handle)checkPict);
		tempRect = (**checkPict).picFrame;
		HUnlock((Handle)checkPict);
		OffsetRect(&tempRect, -tempRect.left, -tempRect.top);
		OffsetRect(&tempRect, 370, 158);
		
		if (theStats[popUpItemSelected - 1].georgeState == kLostState)
			DrawPicture(xPict, &tempRect);
		else if (theStats[popUpItemSelected - 1].georgeState >= kWonState)
			DrawPicture(checkPict, &tempRect);
		else
			FillRect(&tempRect, black);
		OffsetRect(&tempRect, 0, 18);
		
		if (theStats[popUpItemSelected - 1].maraState == kLostState)
			DrawPicture(xPict, &tempRect);
		else if (theStats[popUpItemSelected - 1].maraState >= kWonState)
			DrawPicture(checkPict, &tempRect);
		else
			FillRect(&tempRect, black);
		OffsetRect(&tempRect, 0, 18);
		
		if (theStats[popUpItemSelected - 1].ottoState == kLostState)
			DrawPicture(xPict, &tempRect);
		else if (theStats[popUpItemSelected - 1].ottoState >= kWonState)
			DrawPicture(checkPict, &tempRect);
		else
			FillRect(&tempRect, black);
		OffsetRect(&tempRect, 0, 18);
		
		if (theStats[popUpItemSelected - 1].claireState == kLostState)
			DrawPicture(xPict, &tempRect);
		else if (theStats[popUpItemSelected - 1].claireState >= kWonState)
			DrawPicture(checkPict, &tempRect);
		else
			FillRect(&tempRect, black);
		OffsetRect(&tempRect, 0, 18);
		
		if (theStats[popUpItemSelected - 1].eazeState == kLostState)
			DrawPicture(xPict, &tempRect);
		else if (theStats[popUpItemSelected - 1].eazeState >= kWonState)
			DrawPicture(checkPict, &tempRect);
		else
			FillRect(&tempRect, black);
		OffsetRect(&tempRect, 0, 18);
		
		if (theStats[popUpItemSelected - 1].teakState == kLostState)
			DrawPicture(xPict, &tempRect);
		else if (theStats[popUpItemSelected - 1].teakState >= kWonState)
			DrawPicture(checkPict, &tempRect);
		else
			FillRect(&tempRect, black);
		
		ReleaseResource((Handle)checkPict);
		ReleaseResource((Handle)xPict);
	}
}


/*========================================================  UpdateStatsDialog  */

void UpdateStatsDialog (DialogPtr theDialog)

{
	PicHandle		thePict;
	Handle			iHandle;
	Rect			iRect;
	Str255			theTitle;
	short			iType;
	
	DrawDialog(theDialog);
	
	GetDItem(theDialog, kStatsTitles, &iType, &iHandle, &iRect);
	if (isColor)
		thePict = GetPicture(kTitles4PictID);
	else
		thePict = GetPicture(kTitles1PictID);
	if (thePict != kNilPointer)
	{
		DrawPicture(thePict, &iRect);
		ReleaseResource((Handle)thePict);
	}
	
	GetDItem(theDialog, kStatsTallys, &iType, &iHandle, &iRect);
	if (isColor)
		thePict = GetPicture(kTallys4PictID);
	else
		thePict = GetPicture(kTallys1PictID);
	if (thePict != kNilPointer)
	{
		DrawPicture(thePict, &iRect);
		ReleaseResource((Handle)thePict);
	}
	
	GetDItem(theDialog, kStatsDefeated, &iType, &iHandle, &iRect);
	if (isColor)
		thePict = GetPicture(kDefeated4PictID);
	else
		thePict = GetPicture(kDefeated1PictID);
	if (thePict != kNilPointer)
	{
		DrawPicture(thePict, &iRect);
		ReleaseResource((Handle)thePict);
	}
	
	GetDItem(theDialog, kStatsBalloon, &iType, &iHandle, &iRect);
	if (isColor)
		thePict = GetPicture(kBalloon4PictID);
	else
		thePict = GetPicture(kBalloon1PictID);
	if (thePict != kNilPointer)
	{
		DrawPicture(thePict, &iRect);
		ReleaseResource((Handle)thePict);
	}
	
	GetIndString(theTitle, rMiscStrings, 5);
	if (theTitle[0] == 0)
		PasStringCopy((StringPtr)"\pPlayer StatsÉ", (StringPtr)theTitle);
	MoveTo(kStatsTitleLeft, kStatsTitleTop);
	DrawString(theTitle);
	
	RedrawStatValues();
	DrawStatsPopUp();
	DrawDefaultButton(theDialog, kStatsOkay);
}


/*========================================================  DoStatsPopUpSelect  */

void DoStatsPopUpSelect (DialogPtr theDialog)

{
	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);
		DrawStatsPopUpItem();
		UpdateStatsDialog(theDialog);
		RedrawStatValues();
	}
}


/*========================================================  StatsFilter  */

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

{
	switch (theEvent->what)
	{
		case keyDown:
			switch ((theEvent->message) & charCodeMask)
			{
				case kReturnKeyASCII:
				case kEnterKeyASCII:
					FlashDialogButton(theDialog, kStatsOkay);
					*itemHit = kStatsOkay;
					return(TRUE);
					break;
				default:
					return(FALSE);
			}
			break;
		case updateEvt:
			BeginUpdate((GrafPtr)theDialog);
			UpdateStatsDialog(theDialog);
			EndUpdate((GrafPtr)theDialog);
			theEvent->what = nullEvent;
			return(FALSE);
			break;
		default:
			return(FALSE);
			break;
	}
}


/*========================================================  DoPlayerStats  */

Boolean DoPlayerStats (void)

{
	DialogPtr		statsDialog;
	Handle			iHandle;
	Rect			iRect;
	short			itemHit, iType, i, sheSaid;
	Boolean			leaveDialog, userOkayed, wasState;
	
	CenterDialog(rStatsDialogID);
	if (doZooms)
		ZoomOutDialogRect(rStatsDialogID);
	statsDialog = GetNewDialog(rStatsDialogID, kNilPointer, kPutInFront);
	
	SetPort((GrafPtr)statsDialog);
	ShowWindow((GrafPtr)statsDialog);
	
	teamsPopUp = GetMenu(kStatsPopMenuID);
	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]);
	}
	
	GetDItem(statsDialog, kStatsPopUp, &iType, &iHandle, &teamsPopUpRect);
	
	popUpItemSelected = whichHumanNumber;
	
	UpdateStatsDialog(statsDialog);
	
	leaveDialog = FALSE;
	
	while (!leaveDialog)
	{
		ModalDialog(StatsFilter, &itemHit);
		
		switch (itemHit)
		{
			case kStatsOkay:
				whichHumanNumber = popUpItemSelected;
				userOkayed = TRUE;
				leaveDialog = TRUE;
				break;
			
			case kStatsPopUp:
				DoStatsPopUpSelect(statsDialog);
				break;
			case kStatsErase:
				CenterAlert(rEraseStatsAlertID);
				sheSaid = Alert(rEraseStatsAlertID, kNilPointer);
				if (sheSaid != kEraseStatsOkay)
					EraseStats(popUpItemSelected);
				UpdateStatsDialog(statsDialog);
				break;
			default:
				break;
		}
	}
	
	DisposDialog(statsDialog);
	return (userOkayed);
}


/*========================================================  ResetWorldRecords  */

void ResetWorldRecords (void)

{
	PasStringCopy((StringPtr)"\pNo one", (StringPtr)mostTitlesName);
	GetDateTime((unsigned long *)&mostTitlesDate);
	mostTitles = 0;
	PasStringCopy((StringPtr)"\pNo one", (StringPtr)mostPointsName);
	GetDateTime((unsigned long *)&mostPointsDate);
	mostPoints = 0;
	PasStringCopy((StringPtr)"\pNo one", (StringPtr)mostFoulsName);
	GetDateTime((unsigned long *)&mostFoulsDate);
	mostFouls = 0;
	PasStringCopy((StringPtr)"\pNo one", (StringPtr)mostCritsName);
	GetDateTime((unsigned long *)&mostCritsDate);
	mostCrits = 0;
}


