import pygame
import sys

from pygame.locals import *
from DefFrontEndGameMapFile import *
from DefFrontEndEmuFile import *
from ConfAllPath import *


import os
from ConfAllPath import *
from KeyMapKeyBoard import *

def DefFrontEndEmu(Resolution,emu,MapingEmu,li,CptEmu,MAX_EMU,MAX,Orientation,JOY_TEST):

	#one_debug = 0
	MAX_EMU = MAX_EMU -1
	pygame.key.set_repeat(400, 50)
	Img,BACKGROUNG_ORIG,BACKGROUNG,IMG_EMU,BACKGROUNG_EMU=DefConfPathEmu(Orientation)
	SIZE_BIN_1,SIZE_BIN_2,SIZE_BIN_3,WHERE_BIN_1,WHERE_BIN_2,WHERE_BIN_3=MapingEmu
	Resolution=Resolution
	pygame.init()
	PyrScreen =   pygame.display.set_mode(Resolution,FULLSCREEN)
	fond = pygame.transform.scale(pygame.image.load(BACKGROUNG_EMU).convert(),Resolution)
	PyrScreen.blit(fond, (0,0))
	#pygame.joystick.init()
	if JOY_TEST == 1:
		mon_joystick = pygame.joystick.Joystick(0)
		mon_joystick.init
	if(CptEmu > MAX_EMU) or (CptEmu < - MAX_EMU):
		CptEmu = 0

	if (CptEmu + 1) > MAX_EMU:
		CptRight = 0
	else:
		CptRight = CptEmu + 1

	if (CptEmu - 1) < - MAX_EMU:
		CptLeft = 0
	else:
		CptLeft = CptEmu - 1

	pygame.display.flip()
	KeepInEmu = 1
	FrontEndEmu = 1
	ExitStatus = 0
	while KeepInEmu == 1:
		BeepFile=Img + "../SOUND/blip.wav"
		BeepFileLoad=pygame.mixer.music.load(BeepFile)
		for event in pygame.event.get():
			if JOY_TEST == 0 :
				if event.type == KEYDOWN:
	#				if event.key == K_LEFT:
					if event.key == LEFT_J1:
						CptEmu = CptEmu - 1
					#	print "DEBUG : LEFT : " + str(LEFT_J1)
					#	one_debug = 0
				#	if event.key == K_RIGHT:
					if event.key == RIGHT_J1:
						CptEmu = CptEmu + 1
					#	print "DEBUG : RIGHT : " + str(RIGHT_J1)
					#	one_debug = 0
					if event.key == BTN_J1_1:
						FrontEndEmu = 0
						ExitStatus = 0
						KeepInEmu = 0
						FrontEndGame = 1
						EmuChoose = emu[CptEmu]
						#print "Play ... "
					if event.key == K_ESCAPE:
						FrontEndEmu = 0
						ExitStatus = 1
						FrontEndGame = 0
						EmuChoose = ""
						KeepInEmu=0
					pygame.mixer.Sound(BeepFile).play()
			if JOY_TEST == 1 :
				if event.type == JOYAXISMOTION:
					if event.axis == 0 and event.value > 0:
						CptEmu = CptEmu - 1
					if event.axis == 0 and event.value < 0:
						CptEmu = CptEmu + 1
				if event.type == JOYBUTTONDOWN and event.button == BTN_J1_1:
					FrontEndEmu = 0
					ExitStatus = 0
					KeepInEmu = 0
					FrontEndGame = 1
					EmuChoose = emu[CptEmu]
				pygame.mixer.Sound(BeepFile).play()

			if (CptEmu > MAX_EMU) or (CptEmu < - MAX_EMU):
			    CptEmu = 0

			if (CptEmu + 1) > MAX_EMU:
			    CptRight = 0
			else:
			    CptRight = CptEmu + 1

			if (CptEmu - 1) < - MAX_EMU:
			    CptLeft = 0
			else:
			    CptLeft = CptEmu - 1

			#if one_debug == 0:
			#    print "4. CptLeft -> " + str(CptLeft)

			#if one_debug == 0:
			#    print "2. CptEmu -> " + str(CptEmu)

			#if one_debug == 0:
			#    print "3. CptRight -> " + str(CptRight)

			EmuRightPath=IMG_EMU+'/'+emu[CptRight]+ ".png"
			EmuLeftPath=IMG_EMU+'/'+emu[CptLeft]+ ".png"
			EmuCenterPath=IMG_EMU+'/'+emu[CptEmu]+ ".png"

			#if one_debug == 0:
			#    print "EmuLeftPath -> " + EmuLeftPath
			#if one_debug == 0:
			#    print "EmuCenterPath -> " + EmuCenterPath
			#if one_debug == 0:
			#    print "EmuRightPath -> " + EmuRightPath
			#if one_debug == 0:
			#    print "5. " + str(CptEmu) + " : " + emu[CptEmu]
			#if one_debug == 0:
			#    one_debug = 1;

		if (os.path.isfile(IMG_EMU+'/'+emu[CptRight]+ ".png")):
			EmuRightPath=IMG_EMU+'/'+emu[CptRight]+ ".png"
			EmuRight=pygame.transform.scale(pygame.image.load(EmuRightPath).convert_alpha(),SIZE_BIN_3)
		else:
			muRightPath=IMG_EMU+'/no_emu.png'
			EmuRight=pygame.transform.scale(pygame.image.load(EmuRightPath).convert_alpha(),SIZE_BIN_3)

		if (os.path.isfile(IMG_EMU+'/'+emu[CptLeft]+ ".png")):
			EmuLeftPath=IMG_EMU+'/'+emu[CptLeft]+ ".png"
			EmuLeft=pygame.transform.scale(pygame.image.load(EmuLeftPath).convert_alpha(),SIZE_BIN_2)
		else:
			EmuLeftPath=IMG_EMU+'/no_emu.png'
			EmuLeft=pygame.transform.scale(pygame.image.load(EmuLeftPath).convert_alpha(),SIZE_BIN_2)

		if (os.path.isfile(IMG_EMU+'/'+emu[CptEmu]+ ".png")):
			EmuCenterPath=IMG_EMU+'/'+emu[CptEmu]+ ".png"
			EmuCenter=pygame.transform.scale(pygame.image.load(EmuCenterPath).convert_alpha(),SIZE_BIN_1)
		else:
			EmuCenterPath=IMG_EMU+'/no_emu.png'
			EmuCenter=pygame.transform.scale(pygame.image.load(EmuCenterPath).convert_alpha(),SIZE_BIN_1)


		PyrScreen.blit(fond, (0,0))
		PyrScreen.blit(EmuRight,WHERE_BIN_2),SIZE_BIN_3
		PyrScreen.blit(EmuLeft,WHERE_BIN_3),SIZE_BIN_1
		PyrScreen.blit(EmuCenter,WHERE_BIN_1),SIZE_BIN_1
		pygame.display.flip()
	return FrontEndEmu,ExitStatus,FrontEndGame,EmuChoose,CptEmu
