#!/usr/bin/env python
#-*- coding: utf-8 -*-
# pyrharckade 2 is a simple pygame front for PIMAME
# version: 2.0A
# guillaume@tuxme.net
# GPL Licence

import glob



import os, sys, pygame, pygame.font, pygame.image,subprocess, sys, time
sys.path.insert(0, '../CONF/')

import os
from ConfAllPath import *
from KeyMapKeyBoard import *
import shutil
from DefDownladPerGame import DownloadGraphical
if not glob.glob("ROM_CONFIG_FILES.csv"):
	DownloadGraphical("SYS","ROM_CONFIG_FILES")
	shutil.move("../MEDIA/SYS/ROMS/ROM_CONFIG_FILES.csv","./ROM_CONFIG_FILES.csv")



from pygame.locals import *
from DefReadConfRomFile import DefReadConfRom
from ConfAllPath import DefConfPathHome
import urllib2
import math
import subprocess
from DefResAndSysFile import *
from DefCheckEnvFile import DefCheckEnv
Resolution=DefResolution()
ParentOfPyr,ConfRomFile,AllMedia,ConfigKeyboardFile=DefConfPathHome()
Sys=DefCheckEnv()
MAX, MAX_EMU,li,emu=DefReadConfRom(ConfRomFile)
pygame.joystick.init()
if pygame.joystick.get_count() != 0:
	mon_joystick = pygame.joystick.Joystick(0)
	mon_joystick.init()
	JOY_TEST = 1
	if HACK_KEY_CARD == 'Y':
		JOY_TEST = 0
else:
	JOY_TEST = 0



pygame.init()

print "MAX: " + str(MAX)
print "MAX_EMU: " + str(MAX_EMU)




finished = 0


### PROD
pygame.init()
Y,X=Resolution
PyrScreen = pygame.display.set_mode(Resolution,FULLSCREEN)

### TEST
#Y,X=(800,600)
#Resolution=(Y,X)
#PyrScreen = pygame.display.set_mode(Resolution)


Space_bar=20
XBAR=(Y-(Y/Space_bar))
YBAR=(X/10)
WHERE_XBAR=Space_bar
WHERE_YBAR=(Y/2) - (YBAR/2)

fg = 250, 240, 230
bg = 5, 5, 5


fond = pygame.transform.scale(pygame.image.load("KEY/background.png").convert(),Resolution)

PyrScreen.blit(fond, (0,0))
time.sleep(0.8)
FontSize=(Y/20)

font = pygame.font.Font('../MEDIA/font.ttf', FontSize)
font_big = pygame.font.Font('../MEDIA/font.ttf', FontSize+10)
SCREEN_W,SCREEN_H = Resolution

resolution=SCREEN_W,SCREEN_H

SIZE_IMG_BTN=400

WHERE_IMG_X = int(SCREEN_W /3)
WHERE_IMG_Y = int(SCREEN_H /2) - int(SIZE_IMG_BTN/2)
WHERE_IMG = (WHERE_IMG_X,WHERE_IMG_Y)
WHERE_TEXTE_X = int(SCREEN_W /4) 
WHERE_TEXTE_Y = int(SCREEN_H /2) 
WHERE_TEXTE=(WHERE_TEXTE_X,WHERE_TEXTE_Y)


WHERE_TEXTE_1U=(WHERE_TEXTE_X,WHERE_TEXTE_Y+50)
WHERE_TEXTE_2U=(WHERE_TEXTE_X,WHERE_TEXTE_Y+100)
WHERE_TEXTE_3U=(WHERE_TEXTE_X,WHERE_TEXTE_Y+150)
WHERE_TEXTE_4U=(WHERE_TEXTE_X,WHERE_TEXTE_Y+200)
WHERE_TEXTE_5U=(WHERE_TEXTE_X,WHERE_TEXTE_Y+250)
WHERE_TEXTE_1D=(WHERE_TEXTE_X,WHERE_TEXTE_Y-50)
WHERE_TEXTE_2D=(WHERE_TEXTE_X,WHERE_TEXTE_Y-100)
WHERE_TEXTE_3D=(WHERE_TEXTE_X,WHERE_TEXTE_Y-150)
WHERE_TEXTE_4D=(WHERE_TEXTE_X,WHERE_TEXTE_Y-200)
WHERE_TEXTE_5D=(WHERE_TEXTE_X,WHERE_TEXTE_Y-250)


KeyToExport="RUN"
CPT=0
NMAX=-MAX
MAX=MAX-1
AddInRegularConfFile="N"

text1 = "Touch any key to generate list"
text2 = font.render(text1, True, fg)

PyrScreen.blit(text2,(WHERE_TEXTE))
pygame.key.set_repeat(400, 70)
if CPT>MAX:
	CPT=NMAX
if CPT<NMAX:
	CPT=MAX-1



CPT_1U=CPT-1
if CPT_1U<NMAX:
	#CPT_1U=-(CPT+1)
	CPT_1U=-(NMAX+1)
CPT_2U=CPT-2
if CPT_2U<NMAX:
	CPT_2U=-(NMAX+2)
CPT_3U=CPT-3
if CPT_3U<NMAX:
	CPT_3U=-(NMAX+3)
CPT_4U=CPT-4
if CPT_4U<NMAX:
	CPT_4U=-(NMAX+4)
CPT_5U=CPT-5
if CPT_5U<NMAX:
	CPT_5U=-(NMAX+5)

CPT_1D=CPT+1
if CPT_1D>MAX:
	CPT_1D=NMAX+CPT+1
CPT_2D=CPT+2
if CPT_2D>MAX:
	CPT_2D=NMAX+CPT+2
CPT_3D=CPT+3
if CPT_3D>MAX:
	CPT_3D=NMAX+CPT+3
CPT_4D=CPT+4
if CPT_4D>MAX:
	CPT_4D=NMAX+CPT+4
CPT_5D=CPT+5
if CPT_5D>MAX:
	CPT_5D=NMAX+CPT+5

while KeyToExport != "ESC" :
	for event in pygame.event.get():
		if JOY_TEST != 0 :
			if event.type == JOYAXISMOTION:
				if event.axis == 1 and event.value > 0:
					CPT=CPT-1
				if event.axis == 1 and event.value < 0:
					CPT=CPT-1
				if event.axis == 0 and event.value < 0:
					ROM_L1 = li[CPT][0][0]
					ROM_L2 = ROM_L1
					while (ROM_L1 == ROM_L2):
						CPT = CPT - 1
						if CPT>MAX:
							CPT=NMAX
						if CPT<NMAX:
							CPT=MAX-1

						ROM_L1 = li[CPT][0][0]

				if event.axis == 0 and event.value > 0:
					ROM_L1 = li[CPT][0][0]
					ROM_L2 = ROM_L1
					while (ROM_L1 == ROM_L2):
						CPT = CPT +1 
						if CPT>MAX:
							CPT=NMAX
						if CPT<NMAX:
							CPT=MAX-1

						ROM_L1 = li[CPT][0][0]	

			if event.type == JOYBUTTONDOWN and event.button == BTN_J1_1:
				DownloadGraphical(str(li[CPT][1]), str(li[CPT][0]))
				pygame.display.update()
				CheckStringConf=str(li[CPT][0])+','+str(li[CPT][1])
				ConfFile = open("../ROM_CONFIG_FILES.csv","r")
				for ConfFileLine in ConfFile:
					if CheckStringConf in ConfFileLine:
						AddInRegularConfFile="Y"
	                        ConfFile.close()
				if AddInRegularConfFile != "Y":
					with open("../ROM_CONFIG_FILES.csv",'ab') as f: f.write(str(li[CPT][0])+','+str(li[CPT][1])+"\n")


			if event.type == JOYBUTTONDOWN and event.button == BTN_J1_1:
				print "escape -> " + str(event.button )
				KeyToExport="ESC"

			if CPT>MAX:
				CPT=NMAX
			if CPT<NMAX:
				CPT=MAX-1

		

			CPT_1U=CPT-1
			if CPT_1U<NMAX:
				#CPT_1U=-(CPT+1)
				CPT_1U=-(NMAX+1)
			CPT_2U=CPT-2
			if CPT_2U<NMAX:
	                        CPT_2U=-(NMAX+2)
			CPT_3U=CPT-3
			if CPT_3U<NMAX:
				CPT_3U=-(NMAX+3)
			CPT_4U=CPT-4
			if CPT_4U<NMAX:
				CPT_4U=-(NMAX+4)
			CPT_5U=CPT-5
			if CPT_5U<NMAX:
				CPT_5U=-(NMAX+5)

			CPT_1D=CPT+1
			if CPT_1D>MAX:
				CPT_1D=NMAX+CPT+1
			CPT_2D=CPT+2
			if CPT_2D>MAX:
				CPT_2D=NMAX+CPT+2
			CPT_3D=CPT+3
			if CPT_3D>MAX:
				CPT_3D=NMAX+CPT+3
			CPT_4D=CPT+4
			if CPT_4D>MAX:
				CPT_4D=NMAX+CPT+4
			CPT_5D=CPT+5
			if CPT_5D>MAX:
				CPT_5D=NMAX+CPT+5
		if JOY_TEST == 0 :

			if CPT>MAX:
				CPT=NMAX
			if CPT<NMAX:
				CPT=MAX-1

		

			CPT_1U=CPT-1
			if CPT_1U<NMAX:
				#CPT_1U=-(CPT+1)
				CPT_1U=-(NMAX+1)
			CPT_2U=CPT-2
			if CPT_2U<NMAX:
	                        CPT_2U=-(NMAX+2)
			CPT_3U=CPT-3
			if CPT_3U<NMAX:
				CPT_3U=-(NMAX+3)
			CPT_4U=CPT-4
			if CPT_4U<NMAX:
				CPT_4U=-(NMAX+4)
			CPT_5U=CPT-5
			if CPT_5U<NMAX:
				CPT_5U=-(NMAX+5)

			CPT_1D=CPT+1
			if CPT_1D>MAX:
				CPT_1D=NMAX+CPT+1
			CPT_2D=CPT+2
			if CPT_2D>MAX:
				CPT_2D=NMAX+CPT+2
			CPT_3D=CPT+3
			if CPT_3D>MAX:
				CPT_3D=NMAX+CPT+3
			CPT_4D=CPT+4
			if CPT_4D>MAX:
				CPT_4D=NMAX+CPT+4
			CPT_5D=CPT+5
			if CPT_5D>MAX:
				CPT_5D=NMAX+CPT+5




			if event.type == KEYDOWN:
				if event.key == BTN_J1_2:
					print "escape -> " + str(event.key)
					KeyToExport="ESC"

				if  event.key == DOWN_J1:
					CPT=CPT-1
				if  event.key == UP_J1:
					CPT=CPT+1
				if  event.key == LEFT_J1:
					ROM_L1 = li[CPT][0][0]
					ROM_L2 = ROM_L1
					while (ROM_L1 == ROM_L2):
						CPT = CPT - 1
						if CPT>MAX:
							CPT=NMAX
						if CPT<NMAX:
							CPT=MAX-1

						ROM_L1 = li[CPT][0][0]
				if  event.key == RIGHT_J1:
					ROM_L1 = li[CPT][0][0]
					ROM_L2 = ROM_L1
					while (ROM_L1 == ROM_L2):
						CPT = CPT +1 
						if CPT>MAX:
							CPT=NMAX
						if CPT<NMAX:
							CPT=MAX-1

						ROM_L1 = li[CPT][0][0]

				if event.key == BTN_J1_1:
					DownloadGraphical(str(li[CPT][1]), str(li[CPT][0]))
					pygame.display.update()
					CheckStringConf=str(li[CPT][0])+','+str(li[CPT][1])
					ConfFile = open("../ROM_CONFIG_FILES.csv","r")
					for ConfFileLine in ConfFile:
						if CheckStringConf in ConfFileLine:
							AddInRegularConfFile="Y"
		                        ConfFile.close()
					if AddInRegularConfFile != "Y":
						with open("../ROM_CONFIG_FILES.csv",'ab') as f: f.write(str(li[CPT][0])+','+str(li[CPT][1])+"\n")




		


	text_selection_1U =  str(li[CPT_1U][0]) +  " ("+ str(li[CPT_1U][1]) + ")"

	#text_render_1U = font.render(text_selection_1U, True, fg)

	text_render_1U = font.render(text_selection_1U, True, fg)
	


	text_selection_2U =  str(li[CPT_2U][0]) +  " ("+ str(li[CPT_2U][1]) + ")"
	text_render_2U = font.render(text_selection_2U, True, fg)
	text_selection_3U =  str(li[CPT_3U][0]) +  " ("+ str(li[CPT_3U][1]) + ")"
	text_render_3U = font.render(text_selection_3U, True, fg)
	text_selection_4U =  str(li[CPT_4U][0]) +  " ("+ str(li[CPT_4U][1]) + ")"
	text_render_4U = font.render(text_selection_4U, True, fg)
	text_selection_5U =  str(li[CPT_5U][0]) +  " ("+ str(li[CPT_5U][1]) + ")"
	text_render_5U = font.render(text_selection_5U, True, fg)
	text_selection_5D =  str(li[CPT_5D][0]) +  " ("+ str(li[CPT_5D][1]) + ")"
	text_render_5D = font.render(text_selection_5D, True, fg)
	text_selection_4D =  str(li[CPT_4D][0]) +  " ("+ str(li[CPT_4D][1]) + ")"
	text_render_4D = font.render(text_selection_4D, True, fg)
	text_selection_3D =  str(li[CPT_3D][0]) +  " ("+ str(li[CPT_3D][1]) + ")"
	text_render_3D = font.render(text_selection_3D, True, fg)
	text_selection_2D =  str(li[CPT_2D][0]) +  " ("+ str(li[CPT_2D][1]) + ")"
	text_render_2D = font.render(text_selection_2D, True, fg)
	text_selection_1D =  str(li[CPT_1D][0]) +  " ("+ str(li[CPT_1D][1]) + ")"
	text_render_1D = font.render(text_selection_1D, True, fg)




	text_selection =  str(li[CPT][0]) + " ("+ str(li[CPT][1]) + ")"
	PATH_TEST="../MEDIA/"+str(li[CPT][1])+"/ROMS/"+str(li[CPT][0])+"*"
	#print str(PATH_TEST)
#	if not os.path.exists(PATH_TEST):
	if glob.glob(PATH_TEST):
		text_render = font_big.render(text_selection, True, pygame.Color("green"), pygame.Color("white"))
	else:
		text_render = font_big.render(text_selection, True, pygame.Color("red"), pygame.Color("white"))
	PyrScreen.blit(fond, (0,0))
	PyrScreen.blit(text_render_5U,(WHERE_TEXTE_5U))
	PyrScreen.blit(text_render_4U,(WHERE_TEXTE_4U))
	PyrScreen.blit(text_render_3U,(WHERE_TEXTE_3U))
	PyrScreen.blit(text_render_2U,(WHERE_TEXTE_2U))
	PyrScreen.blit(text_render_1U,(WHERE_TEXTE_1U))

	PyrScreen.blit(text_render_5D,(WHERE_TEXTE_5D))
	PyrScreen.blit(text_render_4D,(WHERE_TEXTE_4D))
	PyrScreen.blit(text_render_3D,(WHERE_TEXTE_3D))
	PyrScreen.blit(text_render_2D,(WHERE_TEXTE_2D))
	PyrScreen.blit(text_render_1D,(WHERE_TEXTE_1D))

	PyrScreen.blit(text_render,(WHERE_TEXTE))

	pygame.display.update()

	pygame.display.update()

#file.close()






