#!/usr/bin/env python3

global cpt
global Origcpt
global EmuMod

FirstRun = 1
cpt = 0
import configparser
from collections import Counter
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
import PIL.Image
import PIL.ImageTk
import sys
from modules.arrow import *
from modules.emulateur_display import *
import time
import os
#sys.path.append( './modules/')
#from arrow import *


#EmuMod :
# 	0 : Emu select
# 	1 : Rom select



#def EmuMod():

def BackgroundRom(Emu):
	

	EmuBgFile = "./data/emu/media/" + str(Emu) +"_bg.png"
	EmuBgFile = IsImgExist(EmuBgFile,"bg")

	OpenEmuBgfile=Image.open(EmuBgFile)
	PhotoEmuBgFile=OpenEmuBgfile.resize((Xsize,Ysize))
	TkPhotoEmuBgFile=ImageTk.PhotoImage(PhotoEmuBgFile)
	LabelEmuBgFile=Label(arkadeus, image=TkPhotoEmuBgFile)
	LabelEmuBgFile.TkPhotoEmuBgFile = TkPhotoEmuBgFile
	Cnv.create_image(0, 0, image=TkPhotoEmuBgFile, anchor = "nw")


def IsImgExist(AbsPathFile,TypeMedia):
	if not os.path.exists(AbsPathFile):
		match TypeMedia:
			case "emu":
				return "./media/noemu.png"
			case "bg":
				return "./media/nobg.png"
	else:
		return AbsPathFile
			
			


def BackgroundEmu():

	BackgroundEmu = Media["background"]
	BgEmu = PhotoImage(file = Background)
	LabelBgEmu = Label(arkadeus,image=BgEmu)
	LabelBgEmu.BgEmu = BgEmu
	Cnv.create_image( 0, 0, image = BgEmu, anchor = "nw")

def EnterEmu(GetEmuMod):
	global EmuMod
	global cpt
	EmuMod=1
	runMcpt = cpt + 1
	if runMcpt > EmuCpt:
		runMcpt = 0
	print("Emu : " + str(res[runMcpt]))
	#Cnv.delete("all")
	BackgroundRom(str(res[runMcpt]))
	
#def ParseEmu():


def ViewEmu(cL,xL,yL,cM,xM,yM,cR,xR,yR):
	NfL="./data/emu/media/" + res[cL] + ".png"
	NfL = IsImgExist(NfL,"emu")
	NfM="./data/emu/media/" + res[cM] + ".png"
	NfM = IsImgExist(NfM,"emu")
	NfR="./data/emu/media/" + res[cR] + ".png"
	NfR = IsImgExist(NfR,"emu")


	OpenNfL=Image.open(NfL)
	PhotoNfL=OpenNfL.resize((Square,Square))
	TkNfL=ImageTk.PhotoImage(PhotoNfL)
	LabelPNfL=Label(arkadeus, image=TkNfL)
	LabelPNfL.TkNfL = TkNfL

	OpenNfM=Image.open(NfM)
	PhotoNfM=OpenNfM.resize((Square,Square))
	TkNfM=ImageTk.PhotoImage(PhotoNfM)
	LabelPNfM=Label(arkadeus, image=TkNfM)
	LabelPNfM.TkNfM = TkNfM

	OpenNfR=Image.open(NfR)
	PhotoNfR=OpenNfR.resize((Square,Square))
	TkNfR=ImageTk.PhotoImage(PhotoNfR)
	LabelPNfR=Label(arkadeus, image=TkNfR)
	LabelPNfR.TkNfR = TkNfR

	Cnv.pack(fill = "both", expand = True)
	for IdCanvas in Cnv.find_all():
		if IdCanvas != 1:
			Cnv.delete(IdCanvas)

	Cnv.create_image(xL, yL, image=TkNfL)
	Cnv.create_image(xM, yM, image=TkNfM)
	Cnv.create_image(xR, yR, image=TkNfR)
	print("Curent Emu : " + str(res[cM]))

def leftKey(GetEmuMod):
	if GetEmuMod == 0:
		global cpt
		cpt =  cpt - 1
		if cpt < 0:
			cpt = EmuCpt
		Lcpt = cpt
		Mcpt = cpt + 1
		Rcpt = cpt + 2
		if Mcpt > EmuCpt:
			Mcpt = 0
			Rcpt = 1
		if Rcpt > EmuCpt:
			Rcpt = 0
		print("DEBUG : " + str(EmuMod) + " Emu : " + str(cpt))
		ViewEmu(Lcpt,EmuLeftSquareX, EmuLeftSquareY,Mcpt,EmuMiddleSquareX, EmuMiddleSquareY,Rcpt,EmuRightSquareX, EmuRightSquareY)

def rightKey(GetEmuMod):

	if GetEmuMod == 0:
		global cpt
		cpt = cpt + 1
		if cpt > EmuCpt:
			cpt = 0
		Lcpt = cpt
		Mcpt = cpt + 1
		Rcpt = cpt + 2
		if Mcpt > EmuCpt:
			Mcpt = 0
			Rcpt = 1
		if Rcpt > EmuCpt:
			Rcpt = 0
		print("DEBUG : " + str(EmuMod) + " Emu : " + str(cpt))
		ViewEmu(Lcpt,EmuLeftSquareX, EmuLeftSquareY,Mcpt,EmuMiddleSquareX, EmuMiddleSquareY,Rcpt,EmuRightSquareX, EmuRightSquareY)

def Escape(GetEmuMod):
	if GetEmuMod == 0:
		print("Ciao !!")
		arkadeus.quit()
	if GetEmuMod == 1:
		global EmuMod
		EmuMod = 0

		BackgroundEmu()
		ViewEmu(0,EmuLeftSquareX, EmuLeftSquareY,1,EmuMiddleSquareX, EmuMiddleSquareY,2,EmuRightSquareX, EmuRightSquareY)




arkadeus= Tk()

global res 
res = []

dir_path = r'./data/emu/bin/'

EmuMod = 0
# Iterate directory
for path in os.listdir(dir_path):
	if os.path.isfile(os.path.join(dir_path, path)):
		FileName=os.path.splitext(path)[0]
		res.append(FileName)
		print("DEBUG : " + FileName)

cnt = Counter(res)
global EmuCpt
EmuCpt=len(cnt) - 1
print(" Nmbre emulateur : " + str(EmuCpt))


config_obj = configparser.ConfigParser()
config_obj.read("./conf/config.ini")
Media = config_obj["Media"]
ForceRes = Media["force_resolution"]
Background = Media["background"]
Square = int(Media["square_size"])

config_rom = configparser.ConfigParser()
config_rom.read("./conf/games.ini")

Emulateur = config_rom["emulateurs"]

print(Emulateur["EmulateurList"])
IdEmu(Emulateur["EmulateurList"])


arkadeus.attributes('-fullscreen', True)
arkadeus.title("Arkadeus")



if ForceRes == "y":
	Xsize = int(Media["x_resolution"])
	Ysize = int(Media["y_resolution"])
else:
	Xsize = arkadeus.winfo_screenwidth()
	Ysize = arkadeus.winfo_screenheight()


print("res : " + str(Xsize) + ":" + str(Ysize) )



################ Carto Emu Mode ##################
EmuMiddleSquareX=(Xsize/2)
EmuMiddleSquareY=(Ysize/2)

EmuLeftSquareX=(Xsize/3)
EmuLeftSquareY=(Ysize/3)

EmuRightSquareX=(Xsize*2/3)
EmuRightSquareY=(Ysize/3)


################ Carto Rom Mode ##################


RomMiddleSquareX=(Xsize/2)
RomMiddleSquareY=(Ysize/2)

RomLeftSquareX=(Xsize/3)
RomLeftSquareY=(Ysize/3)

RomRightSquareX=(Xsize*2/3)
RomRightSquareY=(Ysize/3)


Cnv = Canvas( arkadeus, width = Ysize , height = Xsize)
Cnv.pack(fill = "both", expand = True)

BackgroundEmu()
ViewEmu(cpt,EmuLeftSquareX, EmuLeftSquareY,cpt+1,EmuMiddleSquareX, EmuMiddleSquareY,cpt+2,EmuRightSquareX, EmuRightSquareY)


arkadeus.bind("<Left>", lambda event: leftKey(EmuMod))
arkadeus.bind("<Right>", lambda event: rightKey(EmuMod))
arkadeus.bind("<Return>", lambda event: EnterEmu(EmuMod))
arkadeus.bind('<Escape>',lambda event : Escape(EmuMod))

arkadeus.mainloop()
