# -*- coding: utf-8 -*-
"""
Created on Sat May 25 15:22:41 2024

@author: Stéphane Pasquet
@url : https://www.mathweb.fr/euclide/le-jeu-du-pendu/
"""



from tkinter import Label, Button, Toplevel, Tk, StringVar, Canvas
from random import choice
from unidecode import unidecode


class infoBulle(Toplevel):
    def __init__(self,parent=None,texte='',temps=500):
        Toplevel.__init__(self,parent,bd=1,bg='black')
        self.tps=temps
        self.parent=parent
        self.withdraw()
        self.overrideredirect(1)
        self.transient()     
        l = Label(self,text=texte,bg="yellow",justify='left')
        l.update_idletasks()
        l.pack()
        l.update_idletasks()
        self.tipwidth = l.winfo_width()
        self.tipheight = l.winfo_height()
        self.parent.bind('<Enter>',self.delai)
        self.parent.bind('<Button-1>',self.efface)
        self.parent.bind('<Leave>',self.efface)
        
    def delai(self,event):
        self.action=self.parent.after(self.tps,self.affiche)
        
    def affiche(self):
        self.update_idletasks()
        posX = self.parent.winfo_rootx()+self.parent.winfo_width()
        posY = self.parent.winfo_rooty()+self.parent.winfo_height()
        if posX + self.tipwidth > self.winfo_screenwidth():
            posX = posX-self.winfo_width()-self.tipwidth
        if posY + self.tipheight > self.winfo_screenheight():
            posY = posY-self.winfo_height()-self.tipheight
        self.geometry('+%d+%d'%(posX,posY))
        self.deiconify()
    def efface(self,event):
        self.withdraw()
        self.parent.after_cancel(self.action)
    

class Pendu:
    def __init__(self, window):
        self.window = window
        window.geometry('1000x300')
        window.title('Pendu V1.2 - Stéphane Pasquet - Disponible sur mathweb.fr')
        window.iconbitmap("pendu.ico")
        window.resizable(0,0)
        # Start button
        startButton = Button(window, text = 'Start',  width = 10, font = ('Tahoma', 10), command = self.start)
        startButton.place(x = 5 , y = 5)
        infoBulle(parent = startButton , texte="Commencer une partie")
        # Mot à deviner
        self.wordText = StringVar()
        self.wordTextLabel = Label(window , font = ('Tahoma', 20) , anchor='se' , textvariable = self.wordText)
        self.wordTextLabel.place(x = 100 , y = 35)
        # Liste des lettres déjà utilisées
        self.listLettersText = StringVar()
        self.listLettersLabel = Label(window , font = ('Tahoma', 8) , anchor='se' , textvariable = self.listLettersText)
        self.listLettersLabel.place(x = 100 , y = 100)
        # Phrase question
        self.askText = StringVar()
        self.askLabel = Label(window , font = ('Tahoma', 14) , anchor='se' , textvariable = self.askText , fg = 'green')
        self.askLabel.place(x = 100 , y = 150)
        # Liste des mots à deviner
        self.words = []
        with open('mots.txt' , encoding = 'utf8') as fic:
            for line in fic:
                self.words.append( line.replace('\n','') )
        # Liste des lettres accentuées
        self.list_of_accentued_letters = [ 'A' , 'E' , 'I' , 'O' , 'U' ]
        # lettres déjà proposées
        self.dejaText = StringVar()
        self.dejaLabel = Label(window , font = ('Tahoma', 14) , anchor='se' , textvariable = self.dejaText , fg = 'red')
        self.dejaLabel.place(x = 100 , y = 200)
        
    def word_printed_fct(self , end = False):
        self.word_printed = ''
        if end == False:
            for i in self.word_to_find:
                if unidecode(i) in self.list_letters:
                    self.word_printed += ' ' + i + ' '
                else:
                    self.word_printed += ' _ '
        else:
            for i in self.word_to_find:
                self.word_printed += ' ' + i + ' '
        
        
    def start(self):
        Label(self.window , font = ('Tahoma', 10) , anchor='se' , text = 'Mot à deviner :').place(x = 100 , y = 5)
        self.C = Canvas(self.window , bg = '#ffffff' , width = 200 , height = 250)
        self.C.place(x = 780 , y = 5)
        self.wordTextLabel.config(fg = 'black')
        self.list_letters = []
        self.count = 0
        self.word_to_find = choice(self.words)
        self.find = False
        self.word_printed_fct()
        self.wordText.set( self.word_printed )
        if self.count < 11:
            self.ask()
        
    def ask(self):
        liste = 'Lettres déjà proposées:   '
        for i in self.list_letters:
            liste += i + ' - '
        liste = liste[:-3]
        self.listLettersText.set( liste )
        
        self.word_printed_fct()
        self.wordText.set( self.word_printed )
        
        self.all_letters = True
        for i in self.word_to_find:
            if unidecode(i) not in self.list_letters:
                self.all_letters = False
        if self.all_letters == False:
            self.askText.set( 'Appuyez sur une lettre.' )
            self.window.bind('<Any-KeyPress>',self.verif)
        else:
            self.askLabel.config(fg = 'blue' , font = ('Tahoma', 20) )
            self.askText.set( 'Bravo !' )
            
    def verif(self,event):
        self.dejaText.set('')        
        if ord(event.char) >= 65 and ord(event.char) <= 122:
            lettre = event.char.upper()
            if lettre not in self.list_letters:
                self.list_letters += [ lettre ]
                if lettre not in self.word_to_find and lettre not in self.list_of_accentued_letters:
                    self.count += 1
                    self.draw_pendu()
                elif lettre in self.list_of_accentued_letters:
                    if (lettre == 'A') and ('A' not in self.word_to_find) and ('À' not in self.word_to_find) and ('Â' not in self.word_to_find) and ('Ä' not in self.word_to_find):
                        self.count += 1
                        self.draw_pendu()
                    elif (lettre == 'E') and ('E' not in self.word_to_find) and ('È' not in self.word_to_find) and ('Ê' not in self.word_to_find) and ('É' not in self.word_to_find) and ('Ë' not in self.word_to_find):
                        self.count += 1
                        self.draw_pendu()
                    elif (lettre == 'I') and ('I' not in self.word_to_find) and ('Î' not in self.word_to_find) and ('Ï' not in self.word_to_find):
                        self.count += 1
                        self.draw_pendu()
                    elif (lettre == 'O') and ('O' not in self.word_to_find) and ('Ö' not in self.word_to_find) and ('Ô' not in self.word_to_find):
                        self.count += 1
                        self.draw_pendu()
                    elif (lettre == 'U') and ('U' not in self.word_to_find) and ('Ù' not in self.word_to_find) and ('Ü' not in self.word_to_find):
                        self.count += 1
                        self.draw_pendu()
            elif self.count < 11:
                self.dejaText.set('Lettre déjà proposée !')

            if self.count < 11:
                self.ask()
        else:
            self.ask()
    
    def draw_pendu(self):
        if self.count == 1:
            self.C.create_line(5,240,190,240,fill='brown4',width=10)
        if self.count == 2:
            self.C.create_line(20,240,20,10,fill='brown4',width=10)
        if self.count == 3:
            self.C.create_line(15,15,150,15,fill='brown4',width=10)
        if self.count == 4:
            self.C.create_line(20,60,65,15,fill='brown4',width=10,cap='round')
        if self.count == 5:
            self.C.create_line(140,20,140,50,fill='LightSalmon3',width=5)
        if self.count == 6:
            self.C.create_oval(130,50,150,80,fill='tan1',width=0)
        if self.count == 7:
            self.C.create_line(140,80,140,150,fill='tan1',width=3)
        if self.count == 8:
            self.C.create_line(140,150,110,200,fill='tan1',width=3)
        if self.count == 9:
            self.C.create_line(140,150,170,200,fill='tan1',width=3)
        if self.count == 10:
            self.C.create_line(140,110,110,130,fill='tan1',width=3)
        if self.count == 11:
            self.C.create_line(140,110,170,130,fill='tan1',width=3)
            self.C.create_text(140,65,text='Arf !',fill='red',font = ('Tahoma', 20) , angle = 45 )
            self.end()
            
    def end(self):
        self.askText.set( 'Fin de partie !' )
        self.askLabel.config(fg = 'red' , font = ('Tahoma', 20) )
        self.word_printed_fct(end = True)
        self.wordText.set( self.word_printed )
        self.wordTextLabel.config(fg = 'red')
        

root = Tk()
app = Pendu(root)
root.mainloop()