/* ====================================================================================================

  Chicken Run - Dreamcast

  ECTS Demo Menu

==================================================================================================== */

#include "fireworks.h"

#include "global.h"
#include "main.h"
#include "menu.h"
#include "map_draw.h"
#include "bff_load.h"
#include "collide.h"
#include "debris.h"
#include "lights.h"
#include "timer.h"
#include "maths.h"
#include "overlays.h"
#include "fx.h"
#include "powerbar.h"

#include "puzzles.h"
#include "map_view.h"
#include "fixed.h"
#include "dualshock.h"
#include "subgame.h"
#include "demo.h"
#include "gametext.h"
#include "deth.h"
#include "map_play.h"
#include "card.h"
#include "sound.h"
#include "startup.h"
#include "options.h"

#include "ectsmenu.h"
#include "DC_TIM.h"
#include "puzzles.h"
// #include "nme_mrst.h"

#define MAPNAME	"ECTS_MENU"

#define SEL_R0	32	
#define SEL_G0	32
#define SEL_B0	32

#define SEL_R1	128	
#define SEL_G1	128
#define SEL_B1	0

#define VAR_BUTTON1_COUNTDOWN	1
#define VAR_BUTTON2_COUNTDOWN	2
#define VAR_BUTTON1_LEAKTIME	3


#define NUM_MENU_ITEMS 2

char *menuStrs[] = 
{
	"ECTS  Demo",
	"Fireworks  Subgame",
	"Seesaw  Subgame",
};

TIM_HEADER *tim=NULL, *blitz, *logos, *aardman,*pathe,*dreamworks, *eidos;
int menuSel=0;
int blinkDir=0, blinkCount=0, rgbChgCount=0;
unsigned char blinkRed, blinkGreen, blinkBlue;
int fadeCount, exitFlag;

int walkStall=500, walkStallAmt=500;




void ectsmenuStart(void)
{
POSMARK *pos;

	// ============
	// Load the world map that we'll be using
	// ============

	scenicInit();	
	texanimInit();

	EXLIGHT_Initialise();

	mapviewLoadMap(MAPNAME);
	sound_XA_Stop();
	sound_XA_Start(TUNE_1_INTRO);
	

	setupCollision(MAPNAME);

	EXLIGHT_PostBFF();	

	debrisInit();

	dcTim_Load(&tim, "ECTS_MENU\\CRLOGO.TIM");
	dcTim_Load(&blitz, "ECTS_MENU\\BLITZ.TIM");
	dcTim_Load(&logos, "ECTS_MENU\\LOGOS.TIM");
	dcTim_Load(&eidos, "ECTS_MENU\\EIDOS.TIM");



	// Setup the draw and cycle routines
	draw_routine = ectsmenuDraw;
	cycle_routine = ectsmenuCycle;


	// Setup the camera position and look-at point
	pos = levinfoFindMarker(123);			// Camera position
	ASSERT(pos);							//

	mycamera.vpx		= pos->x;			//
	mycamera.vpy		= pos->y;			//
	mycamera.vpz		= pos->z;			//


	pos = levinfoFindMarker(456);			// Camera look-at point
	ASSERT(pos);							//

	mycamera.vrx		= pos->x;			//
	mycamera.vry		= pos->y;			//
	mycamera.vrz		= pos->z;			//


	// Need to do this in order to get Mr Tweedy to behave properly.....
	levinfo->level_number = LEVNUMBER_A4_PIEBARN;
	PuzzleVars.variables[VAR_BUTTON1_COUNTDOWN] = 100;
	PuzzleVars.variables[VAR_BUTTON1_LEAKTIME] = 70;

	fadeCount=255;
	exitFlag=0;

}

void ectsmenuEnd(void)
{
	dcTim_Unload(&tim);
	dcTim_Unload(&blitz);
	dcTim_Unload(&logos);
	dcTim_Unload(&eidos);


	scenicEnd();
	debrisFree();

	collideFree();
	mapplayReleaseMap();

	EXLIGHT_Shutdown();

	sound_XA_Stop();

	switch(menuSel)
	{
		case 0 : 
			fireworksStart();
			break;
		case 1 :
			seesawStart();
			break;
	}
}


void ectsDrawLogos(void)
{
int w, h, x;
	dcTim_Draw(64, 20, 64 + tim->timWidth, 20 + tim->timHeight, tim);

	w = eidos->timWidth; h = eidos->timHeight;	
	dcTim_Draw(10, 100, 10 + blitz->timWidth, 100 + blitz->timHeight, blitz);
	dcTim_Draw(630 - w, 100, 630, 100 + eidos->timHeight, eidos);

	w = logos->timWidth; h = logos->timHeight;
	x = (640 - w) / 2;
	dcTim_Draw(x, 470-h, x+w, 470, logos);

}



void ectsmenuDrawMenu(int y)
{
int xpos;
int ypos;
int i;


	optionsAlphaRectangle(-150, y, 300, 70, 0, 90);

//	xpos = -256 + ((512 - fontExtentW(font, menuStrs[0])) / 2);
//	fontPrint(font, xpos, 10, menuStrs[0], 128, 128, 128);

	ypos = y+30;

	for(i=1; i < NUM_MENU_ITEMS+1; i++)
	{
		xpos = -256 + ((512 - fontExtentW(font, menuStrs[i])) / 2);

		if ((i-1) == menuSel)		
			fontPrint(font, xpos, ypos, menuStrs[i], blinkRed, blinkGreen, blinkBlue);
		else
			fontPrint(font, xpos, ypos, menuStrs[i], SEL_R0, SEL_G0, SEL_B0);

		ypos += (font->height + 4);
	}
}


void ectsmenuDraw(void)
{
	mapCamSetRefView2L(&mycamera);
	MapDraw_DrawFMA_World((FMA_WORLD2 *)mapview_fma_world);

	scenicDrawAll();
	enemyDrawAll();
	debrisDraw();

	if (fadeCount)
	{
		deathScreenAdd(fadeCount, fadeCount, fadeCount, SEMITRANS_SUB, 0);
	}

	ectsDrawLogos();
	ectsmenuDrawMenu(10);
}
	


void ectsmenuUpdateBlink(void)
{
unsigned short tRed, tGreen, tBlue, fracRed, fracGreen, fracBlue;


	// ==== Check to see if it's time to update the RGB values of the
	if (blinkCount<1)
	{
		blinkCount++;
		return;
	} else {
		blinkCount=0;
	} // end-if-else
	
	if (blinkDir)
	{

		tRed	= SEL_R0 << 8;
		tGreen	= SEL_G0 << 8;
		tBlue	= SEL_B0 << 8;

		fracRed		= ((((short)(SEL_R1 - SEL_R0)) << 8) / 5) * rgbChgCount;
		fracGreen	= ((((short)(SEL_G1 - SEL_G0)) << 8) / 5) * rgbChgCount;
		fracBlue	= ((((short)(SEL_B1 - SEL_B0)) << 8) / 5) * rgbChgCount;

	} else { 

		tRed	= SEL_R1 << 8;
		tGreen	= SEL_G1 << 8;
		tBlue	= SEL_B1 << 8;

		fracRed		= ((((short)(SEL_R0 - SEL_R1)) << 8) / 5) * rgbChgCount;
		fracGreen	= ((((short)(SEL_G0 - SEL_G1)) << 8) / 5) * rgbChgCount;
		fracBlue	= ((((short)(SEL_B0 - SEL_B1)) << 8) / 5) * rgbChgCount;
	} // end-if-else

	tRed   += fracRed;
	tGreen += fracGreen;
	tBlue  += fracBlue;

	blinkRed	= ((unsigned char)(tRed >> 8));
	blinkGreen  = ((unsigned char)(tGreen >> 8));
	blinkBlue   = ((unsigned char)(tBlue >> 8));


	if (rgbChgCount < 5)
		rgbChgCount++;
	else {
		rgbChgCount=0;
		blinkDir = blinkDir ^ 1;
	}
}






extern ENEMYLIST enemyList;

void ectsmenuCycle(void)
{

ENEMY *pE;

	// ==== Take care of all the level and character stuff......

	if (exitFlag && (fadeCount>=254))
	{
		ectsmenuEnd();	
		return;
	}

	scenicCycle();
	enemyProcessAll();
	debrisCycle();

	ectsmenuUpdateBlink();

	if (!PuzzleVars.variables[VAR_BUTTON1_COUNTDOWN] && !PuzzleVars.variables[VAR_BUTTON1_LEAKTIME])
	{

		if (!walkStall--)
		{
			walkStall = walkStallAmt;
			PuzzleVars.variables[VAR_BUTTON1_COUNTDOWN] = 100;
			PuzzleVars.variables[VAR_BUTTON1_LEAKTIME] = 70;
		}
	}

	// ==== Handle the menu controls

	if (pReadPad->debounce[0] & PAD_DOWN)
		menuSel = (menuSel + 1) % NUM_MENU_ITEMS;

	if (pReadPad->debounce[0] & PAD_UP)
		menuSel = (menuSel - 1 + NUM_MENU_ITEMS) % NUM_MENU_ITEMS;

	if (pReadPad->debounce[0] & (PAD_CROSS | PAD_TRIANGLE | PAD_CROSS | PAD_CIRCLE))
	{
		exitFlag=1;
	} 

	for(pE=enemyList.head.next; pE != &enemyList.head; pE = pE->next)
	{
		mycamera.vrx = pE->readData.pActor->position.vx;
		mycamera.vry = pE->readData.pActor->position.vy;
		mycamera.vrz = pE->readData.pActor->position.vz;
	}

	if (exitFlag)
	{
		if (fadeCount < 255)
			fadeCount+=5;
	} else {
		if (fadeCount)
			fadeCount-=5;
	} 
}
