/******************************************************************************************
	CRTEST PS   (c) 1999-2001 ISL

	pause.c:			Very minimalist pause mode
******************************************************************************************/
#include "global.h"
#include "main.h"
#include "menu.h"
#include "gametext.h"
#include "map_draw.h"
#include "overlays.h"
#include "maths.h"
#include "card.h"
#include "inventory.h"
#include "scenics.h"
#include "sptstream.h"
#include "demo.h"
#include "enemies.h"
#include "charactr.h"
#include "map_play.h"
#include "puzzles.h"
#include "fma.h"
#include "charactr.h"
#include <libgpu.h>
#include "pause.h"
#include "map_play.h"
#include "overlays.h"
#include "options.h"
#include "sound.h"
#include "dualshock.h"

extern void mapplayEnd(void);

int options_colour;
int options_colouradd;

int pause_option;
extern VOID_SUBROUTINE old_draw_routine;
extern VOID_SUBROUTINE old_cycle_routine;
unsigned char quit_confirmation;
TextureType *pause_wood;

void pauseCycle(void)
{
	if(!quit_confirmation)
	{
		if(pReadPad->debounce[0] & PAD_DOWN)
		{
			pause_option++;
			if(pause_option>1)
				pause_option = 0;
		}

		if(pReadPad->debounce[0] & PAD_UP)
		{
			pause_option--;
			if(pause_option<0)
				pause_option = 1;
		}

		if(pReadPad->debounce[0] & (PAD_START | PAD_CROSS))
		{
			switch(pause_option)
			{
				case 0:
					pauseEndReturnToGame();
					break;
				case 1:
					pReadPad->debounce[0] = 0;
					quit_confirmation = 1;
					pause_option = 1;
					break;
				default:
					break;
			}
		}
	}

	else
	{	
		debugPrintf("D");
		if(pReadPad->debounce[0] & PAD_DOWN)
		{
			pause_option++;
			if(pause_option>1)
				pause_option = 0;
		}

		if(pReadPad->debounce[0] & PAD_UP)
		{
			pause_option--;
			if(pause_option<0)
				pause_option = 1;
		}
		if(pReadPad->debounce[0] & (PAD_START | PAD_CROSS))
		{
			debugPrintf("!!!");
			switch(pause_option)
			{
				case 0:
					pauseEndQuitGame();
					break;
				case 1:
					pauseEndReturnToGame();
					break;
				default:
					break;
			}
		}
	}

	options_colour+=options_colouradd;
	if((options_colour<0) ||(options_colour>64))
		options_colouradd=-options_colouradd;
}

void pauseDraw(void)
{
	mapplayDraw();
   	mapplayCycle();

	if(!quit_confirmation)
	{
		fontPrintCentred(font,0,-58,fontUpper(TEXTSTR(TEXTSTR_MENU_PAUSED)),64,64,128);

		switch(pause_option)
		{
			case 0:
				fontPrintCentred(font,0,-36,fontUpper(TEXTSTR(TEXTSTR_PAUSED_RETURN_TO_GAME)),64+options_colour,64+options_colour,64+options_colour);
				fontPrintCentred(font,0,-20,TEXTSTR(TEXTSTR_PAUSED_QUIT_GAME),48,48,48);
				break;
			case 1:
				fontPrintCentred(font,0,-36,fontUpper(TEXTSTR(TEXTSTR_PAUSED_RETURN_TO_GAME)),48,48,48);
				fontPrintCentred(font,0,-20,TEXTSTR(TEXTSTR_PAUSED_QUIT_GAME),64+options_colour,64+options_colour,64+options_colour);
				break;
			default:
				break;
		}
	}

	else
	{
		fontPrintCentred(font,0,-58,TEXTSTR(TEXTSTR_PAUSED_QUIT_GAME),64,64,128);
		switch(pause_option)
		{
			case 0:
				fontPrintCentred(font,0,-36,fontUpper(TEXTSTR(TEXTSTR_YES)),64+options_colour,64+options_colour,64+options_colour);
				fontPrintCentred(font,0,-20,fontUpper(TEXTSTR(TEXTSTR_NO)),48,48,48);
				break;
			case 1:
				fontPrintCentred(font,0,-36,fontUpper(TEXTSTR(TEXTSTR_YES)),48,48,48);
				fontPrintCentred(font,0,-20,fontUpper(TEXTSTR(TEXTSTR_NO)),64+options_colour,64+options_colour,64+options_colour);
				break;
			default:
				break;
		}
	}

	// Need something to cover the background where the text is, as it aint being redrawn

	overlaySprite(pause_wood,-128,-64, 4, 0x404040);
	overlaySprite(pause_wood,0,-64, 4, 0x404040);

/*	overlayFilledRectangle(-128, -64,256,64, 0x440000);
	optionsDrawBoxOutline(-129, -65,257,65,0x888888);*/

}


void pauseStart(void)
{	
	globalPause=1;
	sound_XA_PauseForLoading();
	old_draw_routine = draw_routine;
	old_cycle_routine = cycle_routine;
	pause_option = 0;
	draw_routine = pauseDraw;
	cycle_routine = pauseCycle;
	displayPage[0].drawenv.isbg = 0;
	displayPage[1].drawenv.isbg = 0;
	dsEvent_Init();
	pause_wood = textureFindInAllBanks("WOODSTRIP");
	ASSERT(pause_wood);

	options_colour=0;
	options_colouradd = 8;

	// Copy page 0 over page 1 to stop that annoying flickeriness
	{
		static RECT rect = {0,256,512,256};
	   //	MoveImage(&rect,0,0);	
		DrawSync(0);
	}

	quit_confirmation = 0;
}

void pauseEndReturnToGame(void)
{
	globalPause=0;

	draw_routine = old_draw_routine;
	cycle_routine = old_cycle_routine;
	displayPage[0].drawenv.isbg = 1;
	displayPage[1].drawenv.isbg = 1;
	sound_XA_Resume(-1);
}

void pauseEndQuitGame(void)
{
	globalPause=0;

	displayPage[0].drawenv.isbg = 1;
	displayPage[1].drawenv.isbg = 1;
	mapplayEnd();
	optionsStart(0);		// Take the player back to Fowler's hut
}




void pauseEndRestartAct(void)
{
	displayPage[0].drawenv.isbg = 1;	
	displayPage[1].drawenv.isbg = 1;
	draw_routine = old_draw_routine;
	cycle_routine = old_cycle_routine;

	//mapplayEnd();

	// Erm, we also need to (probably) reset a whole load of variables here... all the objects you've collected in this act etc etc
	// Reset all the FMA flags for this act, re-spot items, re-set puzzles, replace sprouts / map pieces / film reels / pictures 

/*	if(levinfo->level_number<=LEVNUMBER_A1_DOGCHASE)
	{
		int i;
		

		// Remove any and all act 1 items from inventory

		for(i=0;i<NIGHTDRESS;i++)
		{
			if(gameSaveData.inventory_item_status[i]!=INV_STATUS_NOTCOLLECTED)
			{
				scenicInventoryNotification(i,SCENIC_INVENTORY_RESPOT);
				gameSaveData.inventory_item_status[i] = INV_STATUS_NOTCOLLECTED;
			}
		}

		for(i=0;i<6;i++)
			currentInv->slot_contents[i] = EMPTY;

		currentInv->selected_item = EMPTY;


		// Clear the task completed flags

		gameSaveData.tasks_done[TASK_CUT_COOP1_FENCE] = 0;
		gameSaveData.tasks_done[TASK_CUT_CLOTHES_LINE] = 0;


		// Clear the FMA flags

		for(i=0;i<MAX_ACT1_FMAS;i++)
		{
			A1fmaStatus[i].playedFlag = 0;
			A1fmaStatus[i].playedTime = 0;
		}
		
		gameSaveData.tasks_done[TASK_A1_BRIEF_FMA] = 0;


		// Rid ourselves of sprouts

		gameSaveData.sprouts_carried = 0;
		for(i=0;i<LEVNUMBER_A1_DOGCHASE;i++)
			gameSaveData.pickups_collected[i] = 0;

		// Respot any map pieces, film reels, pictures etc you might have picked up in Act 1
		// How??? All we know is that you've picked them up, we dont know which act you picked them up in... 
		// Since this is Act 1, we can just re-spot everything, but for a restart act2 or act3, we're
		// gonna have to store more information :(

		// Replace all the map pieces
		for(i=0;i<14;i++)
		{
			if(gameSaveData.map_pieces_got[i])
			{
				scenicInventoryNotification(MAP1+i,SCENIC_INVENTORY_RESPOT);
				gameSaveData.map_pieces_got[i]=0;
			}
		}

		gameSaveData.map_pieces_got[8] = 1;		// Always got piece 8


		// Replace all the stills
		for(i=0;i<30;i++)
		{
			if(gameSaveData.stills_available[i])
			{
				gameSaveData.stills_available[i] = 0;
				scenicInventoryNotification(PIC1+i,SCENIC_INVENTORY_RESPOT);
			}
		}

		
				
		// And send the player back to Act 1 Hut 17 

		inventoryEnsureStreamBank(STREAMBANK_INVENTORYACT1);
		mapplayStart("A1_HUT17", 0);
	}
	



	else if(levinfo->level_number<=LEVNUMBER_A2_COGCHASE)
	{
		int i;
	

		// Remove any and all act 1 items from inventory

		for(i=SPOON;i<BONE+1;i++)
		{
			if(gameSaveData.inventory_item_status[i]!=INV_STATUS_NOTCOLLECTED)
			{
				scenicInventoryNotification(i,SCENIC_INVENTORY_RESPOT);
				gameSaveData.inventory_item_status[i] = INV_STATUS_NOTCOLLECTED;
			}
		}

		for(i=0;i<6;i++)
			currentInv->slot_contents[i] = EMPTY;

		currentInv->selected_item = EMPTY;


		// Clear the task completed flags

		gameSaveData.tasks_done[TASK_DUG_COOP2_FENCE] = 0;
		

		// Clear the FMA flags

		for(i=0;i<MAX_ACT2_FMAS;i++)
		{
			A2fmaStatus[i].playedFlag = 0;
			A2fmaStatus[i].playedTime = 0;
		}
		
		gameSaveData.tasks_done[TASK_A2_BRIEF_FMA] = 0;


		// Rid ourselves of sprouts

		gameSaveData.sprouts_carried = 0;
		for(i=0;i<LEVNUMBER_A2_COGCHASE;i++)
			gameSaveData.pickups_collected[i] = 0;

		// Respot any map pieces, film reels, pictures etc you might have picked up in Act 1
		// How??? All we know is that you've picked them up, we dont know which act you picked them up in... 
		// Since this is Act 1, we can just re-spot everything, but for a restart act2 or act3, we're
		// gonna have to store more information :(

		// Replace all the map pieces
		for(i=0;i<14;i++)
		{
			if(gameSaveData.map_pieces_got[i])
			{
				scenicInventoryNotification(MAP1+i,SCENIC_INVENTORY_RESPOT);
				gameSaveData.map_pieces_got[i]=0;
			}
		}

		gameSaveData.map_pieces_got[8] = 1;		// Always got piece 8


		// Replace all the stills
		for(i=0;i<30;i++)
		{
			if(gameSaveData.stills_available[i])
			{
				gameSaveData.stills_available[i] = 0;
				scenicInventoryNotification(PIC1+i,SCENIC_INVENTORY_RESPOT);
			}
		}


		inventoryEnsureStreamBank(STREAMBANK_INVENTORYACT2);
		mapplayStart("A2_HUT17",0);
	}



	else if(levinfo->level_number<=LEVNUMBER_A4_FLYMACHINE)
	{
		int i;
	

		// Remove any and all act 1 items from inventory

		for(i=EGG;i<GNOME+1;i++)
		{
			if(gameSaveData.inventory_item_status[i]!=INV_STATUS_NOTCOLLECTED)
			{
				scenicInventoryNotification(i,SCENIC_INVENTORY_RESPOT);
				gameSaveData.inventory_item_status[i] = INV_STATUS_NOTCOLLECTED;
			}
		}

		for(i=0;i<6;i++)
			currentInv->slot_contents[i] = EMPTY;

		currentInv->selected_item = EMPTY;


		// Clear the task completed flags

		gameSaveData.tasks_done[TASK_REVEALED_SEWER] = 0;
		gameSaveData.tasks_done[TASK_FINISHED_A4_TRACTOR] = 0;
		

		// Clear the FMA flags

		for(i=0;i<MAX_ACT4_FMAS;i++)
		{
			A4fmaStatus[i].playedFlag = 0;
			A4fmaStatus[i].playedTime = 0;
		}
		
		gameSaveData.tasks_done[TASK_A4_BRIEF_FMA] = 0;


		// Rid ourselves of sprouts

		gameSaveData.sprouts_carried = 0;
		for(i=0;i<LEVNUMBER_A4_FLYMACHINE;i++)
			gameSaveData.pickups_collected[i] = 0;

		// Respot any map pieces, film reels, pictures etc you might have picked up in Act 1
		// How??? All we know is that you've picked them up, we dont know which act you picked them up in... 
		// Since this is Act 1, we can just re-spot everything, but for a restart act2 or act3, we're
		// gonna have to store more information :(

		// Replace all the map pieces
		for(i=0;i<14;i++)
		{
			if(gameSaveData.map_pieces_got[i])
			{
				scenicInventoryNotification(MAP1+i,SCENIC_INVENTORY_RESPOT);
				gameSaveData.map_pieces_got[i]=0;
			}
		}

		gameSaveData.map_pieces_got[8] = 1;		// Always got piece 8


		// Replace all the stills
		for(i=0;i<30;i++)
		{
			if(gameSaveData.stills_available[i])
			{
				gameSaveData.stills_available[i] = 0;
				scenicInventoryNotification(PIC1+i,SCENIC_INVENTORY_RESPOT);
			}
		}

		inventoryEnsureStreamBank(STREAMBANK_INVENTORYACT4);	
		mapplayStart("A4_HUT17",0);
	}		*/


}

// Reset all the variables n stuff in Act 1
void pauseResetAct1(void)
{
}
