#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;
int musicType = 0;
extern int screenResizeS;
extern int screenControllerOptions;
extern int screenCheatOptions;
extern int screensoundOptions;
extern int screenSoundOptions;
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 MenuOptionSelected;
extern int FontSizeGUI;
int Environment = 0;
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];

extern int firstVidRender;
extern int FlickerFilter;
extern "C" {
extern unsigned char StereoSound;
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;

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

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

int sc0 = 1;
int sc1 = 1;
int sc2 = 1;
int sc3 = 1;
int sc4 = 1;
int sc5 = 1;
int sc6 = 1;
int sc7 = 1;
/*
NEWSYM KeyDisableSC0, dd 63
NEWSYM KeyDisableSC1, dd 64
NEWSYM KeyDisableSC2, dd 65
NEWSYM KeyDisableSC3, dd 66
NEWSYM KeyDisableSC4, dd 67
NEWSYM KeyDisableSC5, dd 68
NEWSYM KeyDisableSC6, dd 87
NEWSYM KeyDisableSC7, dd 88

*/
extern int DisableMenuSounds;
int ReverseStereo = 0;
extern "C" {
	extern int SPCEmulationEnabled;
}

extern void processOptions();

extern int inGame;

struct I3DL2ENVIRONMENT
{
    WCHAR*          strName;
    DSI3DL2LISTENER ds3dl;
};
    
I3DL2ENVIRONMENT g_aEnvironments[] =
{
    { L"Default",           { DSI3DL2_ENVIRONMENT_PRESET_DEFAULT }        },
    { L"Generic",           { DSI3DL2_ENVIRONMENT_PRESET_GENERIC }        },
    { L"Padded Cell",       { DSI3DL2_ENVIRONMENT_PRESET_PADDEDCELL }     },
    { L"Room",              { DSI3DL2_ENVIRONMENT_PRESET_ROOM }           },
    { L"Bathroom",          { DSI3DL2_ENVIRONMENT_PRESET_BATHROOM }       },
    { L"Living Room",       { DSI3DL2_ENVIRONMENT_PRESET_LIVINGROOM }     },
    { L"Stone Room",        { DSI3DL2_ENVIRONMENT_PRESET_STONEROOM }      },
    { L"Auditorium",        { DSI3DL2_ENVIRONMENT_PRESET_AUDITORIUM }     },
    { L"Concert Hall",      { DSI3DL2_ENVIRONMENT_PRESET_CONCERTHALL }    },
    { L"Cave",              { DSI3DL2_ENVIRONMENT_PRESET_CAVE }           },
    { L"Arena",             { DSI3DL2_ENVIRONMENT_PRESET_ARENA }          },
    { L"Hangar",            { DSI3DL2_ENVIRONMENT_PRESET_HANGAR }         },
    { L"Carpeted Hallway",  { DSI3DL2_ENVIRONMENT_PRESET_CARPETEDHALLWAY }},
    { L"Hallway",           { DSI3DL2_ENVIRONMENT_PRESET_HALLWAY }        },
    { L"Stone Corridor",    { DSI3DL2_ENVIRONMENT_PRESET_STONECORRIDOR }  },
    { L"Alley",             { DSI3DL2_ENVIRONMENT_PRESET_ALLEY }          },
    { L"Forest",            { DSI3DL2_ENVIRONMENT_PRESET_FOREST }         },
    { L"City",              { DSI3DL2_ENVIRONMENT_PRESET_CITY }           },
    { L"Mountains",         { DSI3DL2_ENVIRONMENT_PRESET_MOUNTAINS }      },
    { L"Quarry",            { DSI3DL2_ENVIRONMENT_PRESET_QUARRY }         },
    { L"Plain",             { DSI3DL2_ENVIRONMENT_PRESET_PLAIN }          },
    { L"Parking Lot",       { DSI3DL2_ENVIRONMENT_PRESET_PARKINGLOT }     },
    { L"Sewer Pipe",        { DSI3DL2_ENVIRONMENT_PRESET_SEWERPIPE }      },
    { L"Underwater",        { DSI3DL2_ENVIRONMENT_PRESET_UNDERWATER }     },
};
static const DWORD NUM_ENVIRONMENTS = sizeof( g_aEnvironments ) / sizeof( g_aEnvironments[0] );


extern int screenMPOptions;


void checkSoundOptions() {
   if(g_Gamepads[0].Event == XBGAMEPAD_B) {
      if(DisableMenuSounds == 0) playEffect(1);
	  screenSoundOptions = 0;
	  screenMPOptions = 0;
	  soundOptionSelected = 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();
      return;          
   } 
   
   if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_DOWN)  {  
		if(DisableMenuSounds == 0) playEffect(0);	
		soundOptionSelected++;
		
		if(inGame == 0) {
			if(soundOptionSelected > 5) soundOptionSelected = 0;
			
		}  
		else {
			if(soundOptionSelected > 14) soundOptionSelected = 0;
		}
   }

   if (g_Gamepads[0].Event == XBGAMEPAD_DPAD_UP)  {  
		if(DisableMenuSounds == 0) playEffect(0);	
		soundOptionSelected--; 
	    if(inGame == 0) {
		
			if(soundOptionSelected < 0) soundOptionSelected = 5;   
		}  
		else {
			if(soundOptionSelected < 0) soundOptionSelected = 14;   
		}  
   }  

   if(g_Gamepads[0].Event == XBGAMEPAD_A) {
		 if(DisableMenuSounds == 0) playEffect(2);
	     if(soundOptionSelected == 0){ SoundFrequency++; if(SoundFrequency > 2) SoundFrequency = 0; if(inGame == 1) setFrequency(); else processOptions(); }
		 if(soundOptionSelected == 1){ SoundInterpolation++; if(SoundInterpolation > 2) SoundInterpolation = 0; if(inGame == 1) setSoundInterpolation(); else processOptions(); }
	     if(soundOptionSelected == 2) {LowPassFilter++; if(LowPassFilter > 3) LowPassFilter = 0; if(inGame == 1) setLowPassFilter(); else processOptions(); }
		 if(soundOptionSelected == 3) {SurroundSound = (SurroundSound + 1) & 1; if(inGame == 1) setSurroundSound(); else processOptions(); }
		 if(soundOptionSelected == 4) {DisableMenuSounds = (DisableMenuSounds + 1) & 1;}
		 if(soundOptionSelected == 5) {ReverseStereo = (ReverseStereo + 1) & 1; if(inGame == 1) RevStereo = ReverseStereo; else processOptions();}
		 //if(soundOptionSelected == 6) {SPCEmulationEnabled = (SPCEmulationEnabled + 1) & 1; if(inGame == 1) SPCDisable = SPCEmulationEnabled; else processOptions();}


		 if(inGame == 1) {
			if(soundOptionSelected == 6) { keys[KeyDisableSC0] = 1; sc0 ^= 1; }
			if(soundOptionSelected == 7) {keys[KeyDisableSC1] = 1;  sc1 ^= 1;}
			if(soundOptionSelected == 8) { keys[KeyDisableSC2] = 1; sc2 ^= 1;}
			if(soundOptionSelected == 9) {keys[KeyDisableSC3] = 1;  sc3 ^= 1;}
			if(soundOptionSelected == 10) {keys[KeyDisableSC4] = 1;  sc4 ^= 1;}
			if(soundOptionSelected == 11) {keys[KeyDisableSC5] = 1;  sc5 ^= 1;}
			if(soundOptionSelected == 12) {keys[KeyDisableSC6] = 1;  sc6 ^= 1;}
			if(soundOptionSelected == 13) {keys[KeyDisableSC7] = 1;  sc7 ^= 1;}
			if(soundOptionSelected == 14) {musicType++; if(musicType > 2) musicType = 0; setSoundType();}
			//StereoSound = 0;
		
		 }
   }
   return;
} 







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

//*************************************************************************
//DRAW
int yS = 0;
int drawS_Count = 0;
extern  int zTopSM;
extern int zLeftSM;

extern  int zTopGM;
extern int zLeftGM;

extern  int zTopCD;
extern int zLeftCD;

void drawSDText(char* text) {
     DWORD color = menuColor[MenuOption];
     
	// WCHAR text2345678_[80]={0};
	// wsprintfW(text2345678_,  L"Option Selected: %d, ShowFPS: %d", controllerConfigSelected, B[0].Button_ShowFPS);
	// FontSetColour(0xffff00);
	// FontText(text2345678_,50,300, 600);
	 
	 
	 
	 if(drawS_Count == soundOptionSelected) 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);
	 FontText(text23456789,340+zLeftSM,yS+zTopSM, 600);
	 yS += vSpace;
     drawS_Count++;
}

void drawSText(char* text) {
     DWORD color = menuColor[MenuOption];
     
	// WCHAR text2345678_[80]={0};
	// wsprintfW(text2345678_,  L"Option Selected: %d, ShowFPS: %d", controllerConfigSelected, B[0].Button_ShowFPS);
	// FontSetColour(0xffff00);
	// FontText(text2345678_,50,300, 600);
	 
	 
	 
	 if(drawS_Count == soundOptionSelected) 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);
	 FontText(text23456789,125+zLeftSM,yS+zTopSM, 600);
	 //yS += 20;
     //drawS_Count++;
}
extern int FontSmallText(WCHAR *,int,int, int);
extern int sWidth;

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

		//WCHAR atext2345[280]={0};
		//wsprintfW(atext2345,L"Select an option and press \"A\" to change the value.");
		//FontSetColour(0xffff00);
		//FontSmallText(atext2345,sWidth/2,100, 600);	

	    drawS_Count = 0;
        yS = 110;
		
		if(SoundFrequency == 0) {  drawSText("Sound Frequency: "); drawSDText("32000"); }
		if(SoundFrequency == 1) {  drawSText("Sound Frequency: "); drawSDText("44100"); }
		if(SoundFrequency == 2) {  drawSText("Sound Frequency: "); drawSDText("48000"); }
		


		if(SoundInterpolation == 0) {  drawSText("Sound Interpolation: ");  drawSDText("Gaussian"); }
		if(SoundInterpolation == 1) {  drawSText("Sound Interpolation: ");  drawSDText("Cubic Spline"); }
		if(SoundInterpolation == 2) {  drawSText("Sound Interpolation: ");  drawSDText("8-Point"); }
		
		if(LowPassFilter == 0) {  drawSText("Low Pass Filter Type: ");  drawSDText("None"); }
		if(LowPassFilter == 1) {  drawSText("Low Pass Filter Type: ");  drawSDText("Simple"); }
		if(LowPassFilter == 2) {  drawSText("Low Pass Filter Type: ");  drawSDText("Dynamic"); }
		if(LowPassFilter == 3) {  drawSText("Low Pass Filter Type: ");  drawSDText("High Quality"); }
		
		if(SurroundSound == 0) {  
			drawSText("Surround Sound: "); drawSDText("Off");
		}
		else {
			drawSText("Surround Sound: "); drawSDText("On");
		}

		if(DisableMenuSounds == 0) {  
			drawSText("Disable Sound Effects: "); drawSDText("Off");
		}
		else {
			drawSText("Disable Sound Effects: "); drawSDText("On");
		}

		if(ReverseStereo == 0) {  
			drawSText("Reverse Stereo: "); drawSDText("Off");
		}
		else {
			drawSText("Reverse Stereo: "); drawSDText("On");
		}


		if(inGame == 1) {
			if(sc0 == 1) {
				drawSText("Sound Channel 1:"); drawSDText("Enabled");
			} else {
				drawSText("Sound Channel 1:"); drawSDText("Disabled");
			}

			if(sc1 == 1) {
				drawSText("Sound Channel 2:"); drawSDText("Enabled");
			} else {
				drawSText("Sound Channel 2:"); drawSDText("Disabled");
			}

			if(sc2 == 1) {
				drawSText("Sound Channel 3:"); drawSDText("Enabled");
			} else {
				drawSText("Sound Channel 3:"); drawSDText("Disabled");
			}
			if(sc3 == 1) {
				drawSText("Sound Channel 4:"); drawSDText("Enabled");
			} else {
				drawSText("Sound Channel 4:"); drawSDText("Disabled");
			}
			if(sc4 == 1) {
				drawSText("Sound Channel 5:"); drawSDText("Enabled");
			} else {
				drawSText("Sound Channel 5:"); drawSDText("Disabled");
			}
		
			if(sc5 == 1) {
				drawSText("Sound Channel 6:"); drawSDText("Enabled");
			} else {
				drawSText("Sound Channel 6:"); drawSDText("Disabled");
			}

			if(sc6 == 1) {
				drawSText("Sound Channel 7:"); drawSDText("Enabled");
			} else {
				drawSText("Sound Channel 7:"); drawSDText("Disabled");
			}

			if(sc7 == 1) {
				drawSText("Sound Channel 8:"); drawSDText("Enabled");
			} else {
				drawSText("Sound Channel 8:"); drawSDText("Disabled");
			}
		
			if(musicType  == 0) {
				drawSText("Sound Type:"); drawSDText("Default");
			} 
			if(musicType  == 1) {
				drawSText("Sound Type:"); drawSDText("Ambience");
			}
			if(musicType  == 2) {
				drawSText("Sound Type:"); drawSDText("Cathedral");
			}

			
		}

		/*
		if(inGame == 0) {
			if(SPCEmulationEnabled == 1) {  
				drawSText("Sound Emulation: "); drawSDText("On");
			}
			else {
				drawSText("Sound Emulation:"); drawSDText("Off");
			}
		}
		*/
}