#!/usr/bin/env python
#-*- coding: utf-8 -*-

import os, sys, pygame, pygame.font, pygame.image,subprocess, sys, time
from pygame.locals import *
sys.path.insert(0, '../CONF/')
import urllib2
import math
from DefResAndSysFile import *
Resolution=DefResolution()

def getSize(filename):
    st = os.stat(filename)
    return st.st_size


pygame.init()

finished = 0


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

ConfigFile="../CONF/KeyMapKeyBoard.py"
KeyHack = 'NULL'
T1=0
TDiff=0
Space_bar=20
XBAR=(Y-(Y/Space_bar))
YBAR=(X/10)
WHERE_XBAR=Space_bar
WHERE_YBAR=(Y/2) - (YBAR/2)


fond = pygame.transform.scale(pygame.image.load("KEY/background_key.png").convert(),Resolution)
PyrScreen.blit(fond, (0,0))
time.sleep(0.8)
SCREEN_W,SCREEN_H = Resolution
FontSize=(SCREEN_H/10)
print "FontSize => " + str(FontSize)
font = pygame.font.Font('../MEDIA/font.ttf', FontSize)



resolution=SCREEN_W,SCREEN_H

SIZE_IMG_BTN=int(SCREEN_H/3)

WHERE_IMG_X = int(SCREEN_W /2) - int(SIZE_IMG_BTN/2)
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 /6) 
WHERE_TEXTE_Y = int(SCREEN_H /6) 
WHERE_TEXTE=(WHERE_TEXTE_X,WHERE_TEXTE_Y)
WHERE_TEXTE_HACK=(10,10)

pygame.joystick.init()
while KeyHack == 'NULL':
	text1 = "Do you used a hack keyboard card ? (UP : Yes / DOWN : No)"
	text2 = font.render(text1, True, pygame.Color("black"), pygame.Color("red"))
	TDiff=5-(round(time.time() - T1))
#	PyrScreen.blit(fond, (0,0))
	pygame.display.update()
#		PyrScreen.blit(text2,(Space_bar,WHERE_YBAR+(YBAR*2)))
	PyrScreen.blit(text2,(WHERE_TEXTE_HACK))
        PngKeyPush="KEY/BTN_HACK.png"
        PngKeyPushObj=pygame.transform.scale(pygame.image.load(PngKeyPush).convert_alpha(),(SIZE_IMG_BTN,SIZE_IMG_BTN))
        PyrScreen.blit(PngKeyPushObj,WHERE_IMG),(SIZE_IMG_BTN,SIZE_IMG_BTN)


	for event in pygame.event.get():

		if event.type == KEYDOWN and  event.key == K_UP:
			KeyHack='Y'

		if event.type == KEYDOWN and  event.key == K_DOWN:
			KeyHack='N'


if  pygame.joystick.get_count() != 0:
	print "Y a un joystick"

if pygame.joystick.get_count() != 0 and KeyHack == 'N':
	print "You select no HackKeyboard  or joy = " +  KeyHack
	mon_joystick = pygame.joystick.Joystick(0)
	mon_joystick.init()
	
	ListeCommandToSet=['BTN_J1_1','BTN_J1_2','BTN_J1_3','BTN_J1_4','BTN_J1_5','BTN_J1_6','SELECT_J1','COIN_J1','START_J1','BTN_J2_1','BTN_J2_2','BTN_J2_3','BTN_J2_4','BTN_J2_5','BTN_J2_6','SELECT_J2','COIN_J2','START_J2']
	JOY_TEST = 1
else:
	print "You select a HackKeyboard or keyboard = " +  KeyHack
	JOY_TEST = 0
	ListeCommandToSet=['UP_J1','DOWN_J1','LEFT_J1','RIGHT_J1','BTN_J1_1','BTN_J1_2','BTN_J1_3','BTN_J1_4','BTN_J1_5','BTN_J1_6','SELECT_J1','COIN_J1','START_J1','UP_J2','DOWN_J2','LEFT_J2','RIGHT_J2','BTN_J2_1','BTN_J2_2','BTN_J2_3','BTN_J2_4','BTN_J2_5','BTN_J2_6','SELECT_J2','COIN_J2','START_J2']

if not os.path.exists(ConfigFile):
	file = open(ConfigFile, "w")
	file.write("import pygame\n")
	file.write("from pygame.locals import *\n\n")
	if KeyHack == 'Y':
		file.write("HACK_KEY_CARD = 'Y'\n\n")
	if KeyHack == 'N':
		file.write("HACK_KEY_CARD = 'N'\n\n")



#else:
#	os.remove(ConfigFile)
#	file = open(ConfigFile, "w")
#	file.write("import pygame\n")
#	file.write("from pygame.locals import *\n\n")











	continuer = 1
	KeyToExport="NULL"

	for KeyPush in ListeCommandToSet:
		print KeyPush
		text1 = "Push a key for : " +  KeyPush + "(" + str(TDiff) + ")"
		text2 = font.render(text1, True, pygame.Color("black"), pygame.Color("white"))
		#PyrScreen.blit(fond, (0,0))
		PyrScreen.blit(text2,(WHERE_TEXTE))
	
		PngKeyPush="KEY/" + KeyPush + ".png"
		PngKeyPushObj=pygame.transform.scale(pygame.image.load(PngKeyPush).convert_alpha(),(SIZE_IMG_BTN,SIZE_IMG_BTN))
		PyrScreen.blit(PngKeyPushObj,WHERE_IMG),(SIZE_IMG_BTN,SIZE_IMG_BTN)

	#	PyrScreen.blit(PngKeyPushObj)

	#	pygame.display.update()


		print "Push a key for : " + KeyPush

		if T1 == 0:
			T1=time.time()

		while KeyToExport == "NULL":
			text1 = "Push a key for : " +  KeyPush + "(" + str(TDiff) + ")"
			text2 = font.render(text1, True, pygame.Color("black"), pygame.Color("white"))
			TDiff=5-(round(time.time() - T1))
		#	PyrScreen.blit(fond, (0,0))
			pygame.display.update()
	#		PyrScreen.blit(text2,(Space_bar,WHERE_YBAR+(YBAR*2)))
			PyrScreen.blit(text2,(WHERE_TEXTE))


			for event in pygame.event.get():

				if JOY_TEST != 0 and event.type == JOYBUTTONDOWN:
					T1 = 0	
					if event.button == 1:
						print 'Btn 1 -> ' + str(event.button)
						KeyToExport="1"
					if event.button == 2:
						print 'Btn 2 -> ' + str(event.button)
						KeyToExport="2"
					if event.button == 3:
						print 'Btn 3 -> ' + str(event.button)
						KeyToExport="3"
					if event.button == 4:
						print 'Btn 4 -> ' + str(event.button)
						KeyToExport="4"
					if event.button == 5:
						print 'Btn 5 -> ' + str(event.button)
						KeyToExport="5"
					if event.button == 6:
						print 'Btn 6 -> ' + str(event.button)
						KeyToExport="6"
					if event.button == 7:
						print 'Btn 7 -> ' + str(event.button)
						KeyToExport="7"
					if  event.button == 8:
						print 'Btn 8 -> ' + str(event.button)
						KeyToExport="8"
					if  event.button == 9:
						print 'Btn 9 -> ' + str(event.button)
						KeyToExport="9"

 
				if JOY_TEST == 0 :              

					if event.type == KEYDOWN:
						T1 = 0	
						if event.key == K_BACKSPACE:
							print "backspace -> " + str(event.key)
							KeyToExport="K_BACKSPACE"
						if event.key == K_TAB:
							print "tab -> " + str(event.key)
							KeyToExport="K_TAB"
						if event.key == K_CLEAR:
							print "clear -> " + str(event.key)
							KeyToExport="K_CLEAR"
						if event.key == K_RETURN:
							print "return -> " + str(event.key)
							KeyToExport="K_RETURN"
						if event.key == K_PAUSE:
							print "pause -> " + str(event.key)
							KeyToExport="K_PAUSE"
						if event.key == K_ESCAPE:
							print "escape -> " + str(event.key)
							KeyToExport="K_ESCAPE"
						if event.key == K_SPACE:
							print "space -> " + str(event.key)
							KeyToExport="K_SPACE"
						if event.key == K_EXCLAIM:
							print "exclaim -> " + str(event.key)
							KeyToExport="K_EXCLAIM"
						if event.key == K_QUOTEDBL:
							print "quotedbl -> " + str(event.key)
							KeyToExport="K_QUOTEDBL"
						if event.key == K_HASH:
							print "hash -> " + str(event.key)
							KeyToExport="K_HASH"
						if event.key == K_DOLLAR:
							print "dollar -> " + str(event.key)
							KeyToExport="K_DOLLAR"
						if event.key == K_AMPERSAND:
							print "ampersand -> " + str(event.key)
							KeyToExport="K_AMPERSAND"
						if event.key == K_QUOTE:
							print "quote -> " + str(event.key)
							KeyToExport="K_QUOTE"
						if event.key == K_LEFTPAREN:
							print "leftparen -> " + str(event.key)
							KeyToExport="K_LEFTPAREN"
						if event.key == K_RIGHTPAREN:
							print "rightparen -> " + str(event.key)
							KeyToExport="K_RIGHTPAREN"
						if event.key == K_ASTERISK:
							print "asterisk -> " + str(event.key)
							KeyToExport="K_ASTERISK"
						if event.key == K_PLUS:
							print "plus -> " + str(event.key)
							KeyToExport="K_PLUS"
						if event.key == K_COMMA:
							print "comma -> " + str(event.key)
							KeyToExport="K_COMMA"
						if event.key == K_MINUS:
							print "minus -> " + str(event.key)
							KeyToExport="K_MINUS"
						if event.key == K_PERIOD:
							print "period -> " + str(event.key)
							KeyToExport="K_PERIOD"
						if event.key == K_SLASH:
							print "slash -> " + str(event.key)
							KeyToExport="K_SLASH"
						if event.key == K_0:
							print "0 -> " + str(event.key)
							KeyToExport="K_0"
						if event.key == K_1:
							print "1 -> " + str(event.key)
							KeyToExport="K_1"
						if event.key == K_2:
							print "2 -> " + str(event.key)
							KeyToExport="K_2"
						if event.key == K_3:
							print "3 -> " + str(event.key)
							KeyToExport="K_3"
						if event.key == K_4:
							print "4 -> " + str(event.key)
							KeyToExport="K_4"
						if event.key == K_5:
							print "5 -> " + str(event.key)
							KeyToExport="K_5"
						if event.key == K_6:
							print "6 -> " + str(event.key)
							KeyToExport="K_6"
						if event.key == K_7:
							print "7 -> " + str(event.key)
							KeyToExport="K_7"
						if event.key == K_8:
							print "8 -> " + str(event.key)
							KeyToExport="K_8"
						if event.key == K_9:
							print "9 -> " + str(event.key)
							KeyToExport="K_9"
						if event.key == K_COLON:
							print "colon -> " + str(event.key)
							KeyToExport="K_COLON"
						if event.key == K_SEMICOLON:
							print "semicolon -> " + str(event.key)
							KeyToExport="K_SEMICOLON"
						if event.key == K_LESS:
							print "less -> " + str(event.key)
							KeyToExport="K_LESS"
						if event.key == K_EQUALS:
							print "equals -> " + str(event.key)
							KeyToExport="K_EQUALS"
						if event.key == K_GREATER:
							print "greater -> " + str(event.key)
							KeyToExport="K_GREATER"
						if event.key == K_QUESTION:
							print "question -> " + str(event.key)
							KeyToExport="K_QUESTION"
						if event.key == K_AT:
							print "at -> " + str(event.key)
							KeyToExport="K_AT"
						if event.key == K_LEFTBRACKET:
							print "leftbracket -> " + str(event.key)
							KeyToExport="K_LEFTBRACKET"
						if event.key == K_BACKSLASH:
							print "backslash -> " + str(event.key)
							KeyToExport="K_BACKSLASH"
						if event.key == K_RIGHTBRACKET:
							print "rightbracket -> " + str(event.key)
							KeyToExport="K_RIGHTBRACKET"
						if event.key == K_CARET:
							print "caret -> " + str(event.key)
							KeyToExport="K_CARET"
						if event.key == K_UNDERSCORE:
							print "underscore -> " + str(event.key)
							KeyToExport="K_UNDERSCORE"
						if event.key == K_BACKQUOTE:
							print "backquote -> " + str(event.key)
							KeyToExport="K_BACKQUOTE"
						if event.key == K_a:
							print "a -> " + str(event.key)
							KeyToExport="K_a"
						if event.key == K_b:
							print "b -> " + str(event.key)
							KeyToExport="K_b"
						if event.key == K_c:
							print "c -> " + str(event.key)
							KeyToExport="K_c"
						if event.key == K_d:
							print "d -> " + str(event.key)
							KeyToExport="K_d"
						if event.key == K_e:
							print "e -> " + str(event.key)
							KeyToExport="K_e"
						if event.key == K_f:
							print "f -> " + str(event.key)
							KeyToExport="K_f"
						if event.key == K_g:
							print "g -> " + str(event.key)
							KeyToExport="K_g"
						if event.key == K_h:
							print "h -> " + str(event.key)
							KeyToExport="K_h"
						if event.key == K_i:
							print "i -> " + str(event.key)
							KeyToExport="K_i"
						if event.key == K_j:
							print "j -> " + str(event.key)
							KeyToExport="K_j"
						if event.key == K_k:
							print "k -> " + str(event.key)
							KeyToExport="K_k"
						if event.key == K_l:
							print "l -> " + str(event.key)
							KeyToExport="K_l"
						if event.key == K_m:
							print "m -> " + str(event.key)
							KeyToExport="K_m"
						if event.key == K_n:
							print "n -> " + str(event.key)
							KeyToExport="K_n"
						if event.key == K_o:
							print "o -> " + str(event.key)
							KeyToExport="K_o"
						if event.key == K_p:
							print "p -> " + str(event.key)
							KeyToExport="K_p"
						if event.key == K_q:
							print "q -> " + str(event.key)
							KeyToExport="K_q"
						if event.key == K_r:
							print "r -> " + str(event.key)
							KeyToExport="K_r"
						if event.key == K_s:
							print "s -> " + str(event.key)
							KeyToExport="K_s"
						if event.key == K_t:
							print "t -> " + str(event.key)
							KeyToExport="K_t"
						if event.key == K_u:
							print "u -> " + str(event.key)
							KeyToExport="K_u"
						if event.key == K_v:
							print "v -> " + str(event.key)
							KeyToExport="K_v"
						if event.key == K_w:
							print "w -> " + str(event.key)
							KeyToExport="K_w"
						if event.key == K_x:
							print "x -> " + str(event.key)
							KeyToExport="K_x"
						if event.key == K_y:
							print "y -> " + str(event.key)
							KeyToExport="K_y"
						if event.key == K_z:
							print "z -> " + str(event.key)
							KeyToExport="K_z"
						if event.key == K_DELETE:
							print "delete -> " + str(event.key)
							KeyToExport="K_DELETE"
						if event.key == K_KP0:
							print "kp0 -> " + str(event.key)
							KeyToExport="K_KP0"
						if event.key == K_KP1:
							print "kp1 -> " + str(event.key)
							KeyToExport="K_KP1"
						if event.key == K_KP2:
							print "kp2 -> " + str(event.key)
							KeyToExport="K_KP2"
						if event.key == K_KP3:
							print "kp3 -> " + str(event.key)
							KeyToExport="K_KP3"
						if event.key == K_KP4:
							print "kp4 -> " + str(event.key)
							KeyToExport="K_KP4"
						if event.key == K_KP5:
							print "kp5 -> " + str(event.key)
							KeyToExport="K_KP5"
						if event.key == K_KP6:
							print "kp6 -> " + str(event.key)
							KeyToExport="K_KP6"
						if event.key == K_KP7:
							print "kp7 -> " + str(event.key)
							KeyToExport="K_KP7"
						if event.key == K_KP8:
							print "kp8 -> " + str(event.key)
							KeyToExport="K_KP8"
						if event.key == K_KP9:
							print "kp9 -> " + str(event.key)
							KeyToExport="K_KP9"
						if event.key == K_KP_PERIOD:
							print "kp_period -> " + str(event.key)
							KeyToExport="K_KP_PERIOD"
						if event.key == K_KP_DIVIDE:
							print "kp_divide -> " + str(event.key)
							KeyToExport="K_KP_DIVIDE"
						if event.key == K_KP_MULTIPLY:
							print "kp_multiply -> " + str(event.key)
							KeyToExport="K_KP_MULTIPLY"
						if event.key == K_KP_MINUS:
							print "kp_minus -> " + str(event.key)
							KeyToExport="K_KP_MINUS"
						if event.key == K_KP_PLUS:
							print "kp_plus -> " + str(event.key)
							KeyToExport="K_KP_PLUS"
						if event.key == K_KP_ENTER:
							print "kp_enter -> " + str(event.key)
							KeyToExport="K_KP_ENTER"
						if event.key == K_KP_EQUALS:
							print "kp_equals -> " + str(event.key)
							KeyToExport="K_KP_EQUALS"
						if event.key == K_UP:
							print "up -> " + str(event.key)
							KeyToExport="K_UP"
						if event.key == K_DOWN:
							print "down -> " + str(event.key)
							KeyToExport="K_DOWN"
						if event.key == K_RIGHT:
							print "right -> " + str(event.key)
							KeyToExport="K_RIGHT"
						if event.key == K_LEFT:
							print "left -> " + str(event.key)
							KeyToExport="K_LEFT"
						if event.key == K_INSERT:
							print "insert -> " + str(event.key)
							KeyToExport="K_INSERT"
						if event.key == K_HOME:
							print "home -> " + str(event.key)
							KeyToExport="K_HOME"
						if event.key == K_END:
							print "end -> " + str(event.key)
							KeyToExport="K_END"
						if event.key == K_PAGEUP:
							print "pageup -> " + str(event.key)
							KeyToExport="K_PAGEUP"
						if event.key == K_PAGEDOWN:
							print "pagedown -> " + str(event.key)
							KeyToExport="K_PAGEDOWN"
						if event.key == K_F1:
							print "f1 -> " + str(event.key)
							KeyToExport="K_F1"
						if event.key == K_F2:
							print "f2 -> " + str(event.key)
							KeyToExport="K_F2"
						if event.key == K_F3:
							print "f3 -> " + str(event.key)
							KeyToExport="K_F3"
						if event.key == K_F4:
							print "f4 -> " + str(event.key)
							KeyToExport="K_F4"
						if event.key == K_F5:
							print "f5 -> " + str(event.key)
							KeyToExport="K_F5"
						if event.key == K_F6:
							print "f6 -> " + str(event.key)
							KeyToExport="K_F6"
						if event.key == K_F7:
							print "f7 -> " + str(event.key)
							KeyToExport="K_F7"
						if event.key == K_F8:
							print "f8 -> " + str(event.key)
							KeyToExport="K_F8"
						if event.key == K_F9:
							print "f9 -> " + str(event.key)
							KeyToExport="K_F9"
						if event.key == K_F10:
							print "f10 -> " + str(event.key)
							KeyToExport="K_F10"
						if event.key == K_F11:
							print "f11 -> " + str(event.key)
							KeyToExport="K_F11"
						if event.key == K_F12:
							print "f12 -> " + str(event.key)
							KeyToExport="K_F12"
						if event.key == K_F13:
							print "f13 -> " + str(event.key)
							KeyToExport="K_F13"
						if event.key == K_F14:
							print "f14 -> " + str(event.key)
							KeyToExport="K_F14"
						if event.key == K_F15:
							print "f15 -> " + str(event.key)
							KeyToExport="K_F15"
						if event.key == K_NUMLOCK:
							print "numlock -> " + str(event.key)
							KeyToExport="K_NUMLOCK"
						if event.key == K_CAPSLOCK:
							print "capslock -> " + str(event.key)
							KeyToExport="K_CAPSLOCK"
						if event.key == K_SCROLLOCK:
							print "scrollock -> " + str(event.key)
							KeyToExport="K_SCROLLOCK"
						if event.key == K_RSHIFT:
							print "rshift -> " + str(event.key)
							KeyToExport="K_RSHIFT"
						if event.key == K_LSHIFT:
							print "lshift -> " + str(event.key)
							KeyToExport="K_LSHIFT"
						if event.key == K_RCTRL:
							print "rctrl -> " + str(event.key)
							KeyToExport="K_RCTRL"
						if event.key == K_LCTRL:
							print "lctrl -> " + str(event.key)
							KeyToExport="K_LCTRL"
						if event.key == K_RALT:
							print "ralt -> " + str(event.key)
							KeyToExport="K_RALT"
						if event.key == K_LALT:
							print "lalt -> " + str(event.key)
							KeyToExport="K_LALT"
						if event.key == K_RMETA:
							print "rmeta -> " + str(event.key)
							KeyToExport="K_RMETA"
						if event.key == K_LMETA:
							print "lmeta -> " + str(event.key)
							KeyToExport="K_LMETA"
						if event.key == K_LSUPER:
							print "lsuper -> " + str(event.key)
							KeyToExport="K_LSUPER"
						if event.key == K_RSUPER:
							print "rsuper -> " + str(event.key)
							KeyToExport="K_RSUPER"
						if event.key == K_MODE:
							print "mode -> " + str(event.key)
							KeyToExport="K_MODE"
						if event.key == K_HELP:
							print "help -> " + str(event.key)
							KeyToExport="K_HELP"
						if event.key == K_PRINT:
							print "print -> " + str(event.key)
							KeyToExport="K_PRINT"
						if event.key == K_SYSREQ:
							print "sysreq -> " + str(event.key)
							KeyToExport="K_SYSREQ"
						if event.key == K_BREAK:
							print "break -> " + str(event.key)
							KeyToExport="K_BREAK"
						if event.key == K_MENU:
							print "menu -> " + str(event.key)
							KeyToExport="K_MENU"
						if event.key == K_POWER:
							print "power -> " + str(event.key)
							KeyToExport="K_POWER"
						if event.key == K_EURO:
							print "euro -> " + str(event.key)
							KeyToExport="K_EURO"
			


			if TDiff != 0:
				PyrScreen.blit(fond, (0,0))
				PyrScreen.blit(text2,(WHERE_TEXTE))
				PyrScreen.blit(PngKeyPushObj,WHERE_IMG),(SIZE_IMG_BTN,SIZE_IMG_BTN)

			if TDiff == -1:
				print str(KeyPush) + " = No touch"
				T1 = 0
				KeyToExport="NULL"

				#pygame.time.wait(10)
		file.write(str(KeyPush) + " = " + str(KeyToExport) + "\n")


		KeyToExport="NULL"

		#pygame.draw.rect(PyrScreen,(200,200,200),((WHERE_XBAR,WHERE_YBAR),(XBAR,YBAR)),0)

	#	PyrScreen.blit(fond, (0,0))
		PyrScreen.blit(fond, (0,0))
		pygame.display.update()
	pygame.display.update()

	file.close()

