
#include "main.h"
#include "global.h"
//#include "text_ids.h"
#include "gametext.h"
#include "collide.h"
#include "enemies.h"
#include "scenics.h"
#include "charactr.h"
#include "card.h"

#include "help.h"

#if PALMODE==0
  #define HELP_PANEL_X (-220)
  #define HELP_PANEL_Y (-100)
#else
  #define HELP_PANEL_X (-220)
  #define HELP_PANEL_Y (-120)
#endif

static int panelY;
extern int inv_messagetimer;			// A timer for printing 'Too heavy / Not enough room' etc messages while trying to pick things up 
extern char *inv_currentmessage;


void helpPrintInventoryMessages(void);

void helpAddToPanel(CHARACTER *pC, char *s, int currentState, int queriedState)
{	
	if(((currentState!=queriedState) && (pC->pVTable[currentState][queriedState])) || (currentState==-1))
	{
		fontPrint(font, HELP_PANEL_X, panelY, s, 64, 128, 128);

		panelY+=16;
	}
}

char cGetControllerButton(unsigned short b)
{
	if(b & PAD_CROSS)
		return 'X';
	if(b & PAD_CIRCLE)
		return 'C';
	if(b & PAD_TRIANGLE)
		return 'T';
	if(b & PAD_SQUARE)
		return 'S';
	if(b & PAD_START)
		return 'A';
	return 'X';
}

char cGetCorrectButton(char b)
{
	switch(b)
	{
		case 'j':
			return cGetControllerButton(controlMaps[controlMap].jump);
		case 't':
			return cGetControllerButton(controlMaps[controlMap].chuck);
		case 'a':
			return cGetControllerButton(controlMaps[controlMap].action);
		case 'i':
			return cGetControllerButton(controlMaps[controlMap].inventory);
		case 'c':
			return cGetControllerButton(controlMaps[controlMap].swapRats);
	}

	return 0;
}

char *sGetHelpString(char *s)
{
	static char sOut[32];
	int i;
	char b, oldb = 0;

	for(i=0; i<32; i++)
	{
		b = s[i];

		if(oldb == '@')
			b = cGetCorrectButton(b);

		sOut[i] = b;

		oldb = b;
	}

	return(sOut);
}

int helpOn = FALSE;
int helpCurrentAction = 0;

void helpUpdate(int helpLevel)
{
	CHARACTER *pC;

	if(padData.debounce[0] & PAD_SELECT)
		helpOn = (!helpOn);

	helpPrintInventoryMessages();		// We want to print these whether help messages are switched on or not

	if(!helpOn)
		return;

	pC = characterGetMainCharacter();

	panelY = HELP_PANEL_Y;



	switch(helpLevel)
	{
	case HELPTEXT_RATS:
		// CPW: that's right, NO break here
	case HELPTEXT_CHICKENS:

		if(levinfo->level_number != LEVNUMBER_A2_COGCHASE)
		{
			if((pC->pEnemy->readData.nameCRC == CRC_NICK) || (pC->pEnemy->readData.nameCRC == CRC_FETCHER))
			{
				helpAddToPanel(pC, sGetHelpString(TEXTSTR(TEXTSTR_HELP_RATS_CHANGECHARACTER)), -1, -1);
			}
			else
			{
				if(gamePlayData->sprouts_carried>0)
					helpAddToPanel(pC, sGetHelpString(TEXTSTR(TEXTSTR_HELP_STANDARD_THROW)), pC->pEnemy->state, STATE_CRAWLING);
			}
		}

		if(pC->pVTable[pC->pEnemy->state][STATE_JUMPING])
		{
			if(pC->pEnemy->state == STATE_JUMPING)
			{
				if(pC->pEnemy->runFlags & RUNFLAGS_DJUMPALLOWED)
					helpAddToPanel(pC, sGetHelpString(TEXTSTR(TEXTSTR_HELP_STANDARD_DOUBLEJUMP)), -1, -1);
			}
			else
				helpAddToPanel(pC, sGetHelpString(TEXTSTR(TEXTSTR_HELP_STANDARD_JUMP)), -1, -1);
		}

		if(levinfo->level_number != LEVNUMBER_A2_COGCHASE)
		{
			if(helpCurrentAction)
				helpAddToPanel(pC, sGetHelpString(TEXTSTR(helpCurrentAction)), -1, -1);
			else
				helpAddToPanel(pC, sGetHelpString(TEXTSTR(TEXTSTR_HELP_STANDARD_ACTION)), -1, -1);
	
			helpAddToPanel(pC, sGetHelpString(TEXTSTR(TEXTSTR_HELP_STANDARD_INVENTORY)), -1, -1);
		}

		break;

	case HELPTEXT_ACT1BOSS:
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A1CHASE_BALANCE), -1, -1);
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A1CHASE_STRAFE), -1, -1);
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A1CHASE_SPEEDUP), -1, -1);
		break;

	case HELPTEXT_ACT2SUBGAME:
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A2SUBGAME_POWER), -1, -1);
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A2SUBGAME_TRAJECTORY), -1, -1);
		break;

	case HELPTEXT_ACT2BOSS:
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A2BOSS_PEDALFASTER), -1, -1);
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A2BOSS_STRAFE), -1, -1);
		break;

	case HELPTEXT_ACT3SUBGAME:
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A3SUBGAME_ACTIVATE), -1, -1);
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A3SUBGAME_DIRECTION), -1, -1);
		break;

	case HELPTEXT_ACT3BOSS:
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A3BOSS_STRAFE), -1, -1);
		break;

	case HELPTEXT_ACT4ENGINE:
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A4ENGINE_SWITCH), -1, -1);
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A4ENGINE_ACTION), -1, -1);
		break;

	case HELPTEXT_ACT4EGG:
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A4EGGLAY_SWITCH), -1, -1);
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A4EGGLAY_FEED), -1, -1);
		break;

	case HELPTEXT_ACT4CONSTRUCTION:
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A4CONSTRUCTION_SWITCH), -1, -1);
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A4CONSTRUCTION_ACTION), -1, -1);
		break;

	case HELPTEXT_ACT4BOSS:
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A4BOSS_FOLDWINGS), -1, -1);
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A4BOSS_DROP), -1, -1);
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A4BOSS_FASTER), -1, -1);
		helpAddToPanel(pC, TEXTSTR(TEXTSTR_HELP_A4BOSS_SLOWER), -1, -1);
		break;

	default:
		DB("Sacred cow cannot be harmed!\n");
		ASSERT(0);
	}
}

// BP: 
void helpPrintInventoryMessages(void)
{
	if(!inv_messagetimer)
		return;
	
	inv_messagetimer--;
	fontCentredMultiLine(font, 0, 20, 480,inv_currentmessage,  64, 64, 128);
	//fontCentredMultiLine(psFont *font, int x, int y, int width, char *text, unsigned char r, unsigned char g, unsigned char b)
}
