#include <xtl.h>
#include "xbinput.h"
extern int vSpace;
extern int screenOptions;
extern int screenResize;
extern int screenResizeP;
extern int screenResizeI;
extern int screenResizePB;
extern int screenResizeJ;
extern int screenResizeG;
extern int screenResizeB;
extern int screenResizeA;
extern int screenResizeA2;
extern int screenOptionsM;
extern int screenResizeS;
extern int screenControllerOptions;
extern int screenCheatOptions;
extern int screenResizeMPL;
extern int screensoundOptions;
extern int screenResizeGP;
extern int screenSoundOptions;
int screenMPOptions = 0;
extern int screenLightGunOptions;
extern int screenGeneralOptions;
extern WAVEFORMATEX wfx;
extern LPDIRECTSOUNDBUFFER     lpPrimaryBuffer;
extern int screenGUIOptions;
// Global instance of custom gamepad devices
extern int DisableMenuSounds;
extern void playEffect(int i);
extern  void writeConfig();
extern XBGAMEPAD g_Gamepads[4];
extern int GameNameColor;
extern LPDIRECTSOUND8  m_pDSound;
extern unsigned char* keys;
extern int GameNameSelectedColor;
extern int FolderColor;
extern int MenuOption;
extern int screenGeneralOptionsMP;
extern int MenuOptionSelected;
extern int FontSizeGUI;
extern int FullNameColor;
extern DWORD menuColor[5];
extern int GameNameColor;
extern int GameNameSelectedColor;
extern int FolderColor;
extern int MenuOption;
extern int MenuOptionSelected;
extern int FontSizeGUI;
extern int FullNameColor;
extern DWORD menuColor[5];

#include <string>
#include <vector>
using namespace std;

extern int musicMode;
extern int musicPlayer;
extern int FileMenuSelected;
extern HRESULT stInitialize();
extern void searchDirectory(std::string pathToSearch, bool mountTopLevel);
extern int favorites;
extern void releaseMP();
				
extern int fadeOption; 
extern int fadeColor; 
extern int fadeColor2;
extern int screenOptionsM ; 			
extern int screenOptions ; 
extern int currentStationaryFrame; 
extern int screenGUIOptions;
extern int screenShotSlotAuto;
extern int wasThere;
extern void changePreview();
extern float lastTimeScreenShotSwitch;
extern float currentTimeScreenShotSwitch;
extern LARGE_INTEGER tickScreenShotSwitch;
extern int FontSmallText(WCHAR *text,int dx,int dy, int maxWidth);









extern int firstVidRender;
extern int FlickerFilter;
extern "C" {
extern unsigned char FPSAtStart;
extern  unsigned char FPSOn;
extern unsigned char RevStereo;
extern unsigned char SPCDisable;

extern unsigned char KeyDisableSC0;
extern unsigned char KeyDisableSC1;
extern unsigned char KeyDisableSC2;
extern unsigned char KeyDisableSC3;
extern unsigned char KeyDisableSC4;
extern unsigned char KeyDisableSC5;
extern unsigned char KeyDisableSC6;
extern unsigned char KeyDisableSC7;


}
int loopModeMP = 0;
extern  int HardwareFilter;
extern unsigned char SoftwareFilter;
extern  int FlickerFilter;
extern  int ShowFPS;
extern  int Soften;
int mpOptionSelected = 0;
extern void setFlickerSoft();
extern int LowPassFilter;
extern int SoundFrequency;
extern int SoundInterpolation;
extern int SurroundSound;

extern "C" {
extern void SetReverseStereo();
extern void SetSPCEnable();
extern void setFrequency();
extern void setLowPassFilter();
extern void setSoundInterpolation();
extern void setSurroundSound();
extern void setSoundType();
}

extern int DisableMenuSounds;
extern "C" {
	extern int SPCEmulationEnabled;
}

extern void processOptions();

extern int inGame;

struct I3DL2ENVIRONMENT
{
    WCHAR*          strName;
    DSI3DL2LISTENER ds3dl;
};

extern void switchMP(int i);
extern int mpType;

void generalCheck() {
	 if(g_Gamepads[0].Event == XBGAMEPAD_B) {
      if(DisableMenuSounds == 0) playEffect(1);
	  screenSoundOptions = 0;
	  screenGeneralOptionsMP = 0;
	  mpOptionSelected = 0;
      screenOptionsM = 1;
	  screenResize = 0;
	  screenResizeP = 0;
	  screenResizeI = 0;
	  screenResizePB = 0;
	  screenResizeJ = 0;
	  screenResizeG = 0;
	  screenResizeB = 0;
	  screenResizeA = 0;
      screenResizeA2 = 0;
	  screenResizeS = 0;
	  screenControllerOptions = 0;
	  writeConfig();
      return;          
   } 
   
   if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_DOWN)  {  
		if(DisableMenuSounds == 0) playEffect(0);	
		mpOptionSelected++;

		if(mpOptionSelected > 1) mpOptionSelected = 0;
   }

   if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_UP)  {  
		if(DisableMenuSounds == 0) playEffect(0);	
		mpOptionSelected--; 
	 
		if(mpOptionSelected < 0) mpOptionSelected = 1;   
	
   }  

   if(g_Gamepads[0].Event == XBGAMEPAD_A) {
		 if(DisableMenuSounds == 0) playEffect(2);
	     if(mpOptionSelected == 0){ 
				musicMode = 0;
				musicPlayer = 1;
			    FileMenuSelected = 0;
				
				if(musicMode == 0) {
					stInitialize();
				}
				else {
					std::string path = "D:\\Music\\";
					searchDirectory(path, false);
				}

				favorites = 0;
				screenGeneralOptionsMP = 0;
				//fadeOption = 0; fadeColor = 255; fadeColor2 = 255;
				screenOptionsM = 0; 
				screenOptions = 0; currentStationaryFrame = 0; screenGUIOptions = 0;
				screenShotSlotAuto = 1;
				wasThere = 0;
				changePreview();
				lastTimeScreenShotSwitch = (FLOAT)tickScreenShotSwitch.QuadPart;
				switchMP(mpType); 
		 }



	     if(mpOptionSelected == 1){ 
			 musicMode = 1;
				musicPlayer = 1;
			    FileMenuSelected = 0;
				
				if(musicMode == 0) {
					stInitialize();
				}
				else {
					std::string path = "D:\\Music\\";
					searchDirectory(path, false);
				}

				screenGeneralOptionsMP = 0;
				favorites = 0;
				//fadeOption = 0; fadeColor = 255; fadeColor2 = 255;
				
				screenOptionsM = 0; 
				screenOptions = 0; currentStationaryFrame = 0; screenGUIOptions = 0;
				screenShotSlotAuto = 1;
				wasThere = 0;
				changePreview();
				lastTimeScreenShotSwitch = (FLOAT)tickScreenShotSwitch.QuadPart;
				switchMP(mpType); 
		 }	 
   }
   return;
}

void checkMPOptions() {
	if(screenGeneralOptionsMP == 1) { generalCheck(); return; }



   if(g_Gamepads[0].Event == XBGAMEPAD_B) {
      if(DisableMenuSounds == 0) playEffect(1);
	  screenSoundOptions = 0;
	  screenMPOptions = 0;
	  mpOptionSelected = 0;
      screenOptions     = 1;
	  screenResize = 0;
	  screenResizeP = 0;
	   screenResizeI = 0;
	  screenResizePB = 0;
	  screenResizeJ = 0;
	  screenResizeG = 0;
	   screenResizeB = 0;
	    screenResizeA = 0;
		screenResizeA2 = 0;
		screenOptionsM = 0;
		 screenResizeS = 0;
	  screenControllerOptions = 0;
	  writeConfig();
	  releaseMP();
      return;          
   } 
   
   if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_DOWN)  {  
		if(DisableMenuSounds == 0) playEffect(0);	
		mpOptionSelected++;

		if(mpOptionSelected > 3) mpOptionSelected = 0;
   }

   if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_UP)  {  
		if(DisableMenuSounds == 0) playEffect(0);	
		mpOptionSelected--; 
	 
		if(mpOptionSelected < 0) mpOptionSelected = 3;   
	
   }  

   if(g_Gamepads[0].Event == XBGAMEPAD_A) {
		 if(DisableMenuSounds == 0) playEffect(2);
	     if(mpOptionSelected == 0){ screenResizeMPL = 1; screenMPOptions  = 0; switchMP(mpType); return; }
	     if(mpOptionSelected == 1){ screenResizeGP = 1;  screenMPOptions  = 0; switchMP(mpType); return; }
		 if(mpOptionSelected == 2) { loopModeMP++; if (loopModeMP > 1) loopModeMP = 0; }
		 if(mpOptionSelected == 3){ mpType+=1; if(mpType > 4) mpType = 0; }//switchMP(mpType);}
		 
   }
   return;
} 

extern int FontSetColour(unsigned int colour);
extern int FontText(WCHAR *,int,int, int);

//*************************************************************************
//DRAW
int yMP = 0;
int drawMP_Count = 0;
extern  int zTopMP;
extern int zLeftMP;

extern  int zTopSM;
extern int zLeftSM;

extern  int zTopCD;
extern int zLeftCD;

void drawMPText(char* text) {
     DWORD color = menuColor[MenuOption];
   
	 if(drawMP_Count == mpOptionSelected) color = menuColor[MenuOptionSelected]; 
     
     //Draw the text string
     WCHAR text23456789[80]={0};
	 //wsprintfW(text2345678_, (LPCWSTR)text);

	 for(int i = 0; i < 1000; i++) {
		 if(text[i] == '\0') {
			text23456789[i] = '\0';
			break;
		 }
		 text23456789[i] = text[i];
	 }
	 FontSetColour(color);
	 if(screenGeneralOptionsMP == 1)
		FontSmallText(text23456789,340+zLeftSM,yMP+zTopSM, 600);
	 else
	    FontText(text23456789,340+zLeftSM,yMP+zTopSM, 600);

	 yMP += vSpace;
     drawMP_Count++;
}

void drawMPSText(char* text) {
     DWORD color = menuColor[MenuOption];
	 if(drawMP_Count == mpOptionSelected) color = menuColor[MenuOptionSelected]; 
     
     //Draw the text string
     WCHAR text23456789[80]={0};
	 //wsprintfW(text2345678_, (LPCWSTR)text);

	 for(int i = 0; i < 1000; i++) {
		 if(text[i] == '\0') {
			text23456789[i] = '\0';
			break;
		 }
		 text23456789[i] = text[i];
	 }
	 FontSetColour(color);

	 if(screenGeneralOptionsMP == 1)
		 FontSmallText(text23456789,125+zLeftSM,yMP+zTopSM, 600);
	 else
		 FontText(text23456789,125+zLeftSM,yMP+zTopSM, 600);
	 
	 //yMP += 20;
     //drawS_Count++;
}
extern int FontSmallText(WCHAR *,int,int, int);
extern int sWidth;

void drawMPOptionsMenu() {
		/*WCHAR text2345[280]={0};
		wsprintfW(text2345,L"Press \"B\" to exit.");
		FontSetColour(0xffff00);
		FontSmallText(text2345,sWidth/2,70, 600);	*/

	    drawMP_Count = 0;
        yMP = 130;
		
		if(screenGeneralOptionsMP == 1) {
			drawMPSText("Xbox Soundtracks"); drawMPText("");
			drawMPSText("Classic Video Game Music"); drawMPText("");
		}
		else {
			//drawMPSText("Adjust Music Player Position"); drawMPText("");
			drawMPSText("Adjust Frequency Bar Position"); drawMPText("");
			drawMPSText("Adjust Music Player Position"); drawMPText("");
			if(loopModeMP == 0) {
				drawMPSText("Xbox Soundtrack Mode: "); drawMPText("Auto Advance");
			} else {
				drawMPSText("Xbox Soundtrack Mode: "); drawMPText("Loop");
			}
			if(mpType == 0) {drawMPSText("Music Player Skin:"); drawMPText("Red Glass");}
			if(mpType == 1) {drawMPSText("Music Player Skin:"); drawMPText("Green Industrial");}
			if(mpType == 2) {drawMPSText("Music Player Skin:"); drawMPText("Black Palm");}
			if(mpType == 3) {drawMPSText("Music Player Skin:"); drawMPText("Green Palm");}
			if(mpType == 4) {drawMPSText("Music Player Skin:"); drawMPText("DVD");}
		}
		
}