from AppliDef import *


class FrmAccueil(tk.Toplevel):
    def __init__(self, container, RatioX, RatioY, Appli):
        tk.Toplevel.__init__(self, container)
        self.MainAppli = Appli
        self.parent = container
        self.RatioX = RatioX
        self.RatioY = RatioY

        screen = ctypes.windll.user32
        FrmFond = Image.open('Media/FrmAccueil/Fond/Fond.bmp')
        self.SX = int((float(FrmFond.size[0]) * float(self.RatioX)))
        self.SY = int((float(FrmFond.size[1]) * float(self.RatioY)))

        self.ImgFrmFond = []
        self.ImgFrmFond = RedimImg(self.SX, self.SY, 'Media/FrmAccueil/Fond/')
        self.CanvasFrmFond = CanvasApp(self, width=self.SX, height=self.SY)
        self.ImgCanvasFrmFond = self.CanvasFrmFond.create_image(0, 0, anchor=tk.NW, image=self.ImgFrmFond[0])

        self.geometry(str(self.SX) + "x" + str(self.SY) + "+" + str(int(screen.GetSystemMetrics(0) * 0.002)) + "+" +
                      str(int(screen.GetSystemMetrics(1) * 0.031)))
        self.resizable(width=False, height=False)
        self.overrideredirect(True)
        self.lift()

        self.CanvasTabPhys = CanvasInWindow('Media/FrmAccueil/Physique/Tableau/', self.RatioX, self.RatioY,
                                            self.CanvasFrmFond, tk.CENTER, int(self.SX * 0.857), int(self.SY * 0.223),
                                            0)
        self.ListCanvasPhys = []
        self.ListCanvasPhys.append(CanvasInWindow('Media/FrmAccueil/Physique/Titre/', self.RatioX, self.RatioY,
                                                  self.CanvasTabPhys.CanvasListIma, tk.CENTER,
                                                  int(self.CanvasTabPhys.wsize * 0.5),
                                                  int(self.CanvasTabPhys.hsize * 0.2), 0))
        self.ListCanvasPhys.append(CanvasInWindow('Media/FrmAccueil/Physique/Mat/', self.RatioX, self.RatioY,
                                                  self.CanvasTabPhys.CanvasListIma, tk.W,
                                                  int(self.CanvasTabPhys.wsize * 0.1),
                                                  int(self.CanvasTabPhys.hsize * 0.43), 0))
        self.ListCanvasPhys.append(CanvasInWindow('Media/FrmAccueil/Physique/TP/', self.RatioX, self.RatioY,
                                                  self.CanvasTabPhys.CanvasListIma, tk.W,
                                                  int(self.CanvasTabPhys.wsize * 0.1),
                                                  int(self.CanvasTabPhys.hsize * 0.58), 0))
        self.ListCanvasPhys.append(CanvasInWindow('Media/FrmAccueil/Physique/COM/', self.RatioX, self.RatioY,
                                                  self.CanvasTabPhys.CanvasListIma, tk.W,
                                                  int(self.CanvasTabPhys.wsize * 0.1),
                                                  int(self.CanvasTabPhys.hsize * 0.72), 0))
        self.ListCanvasPhys.append(CanvasInWindow('Media/FrmAccueil/Physique/Doc/', self.RatioX * 0.3,
                                                  self.RatioY * 0.3, self.CanvasFrmFond, tk.W, int(self.SX * 0.576),
                                                  int(self.SY * 0.728), 0))
        self.ListCanvasPhys.append(CanvasInWindow('Media/FrmAccueil/Para/', self.RatioX * 0.15, self.RatioY * 0.15,
                                                  self.CanvasTabPhys.CanvasListIma, tk.W,
                                                  int(self.CanvasTabPhys.wsize * 0.905),
                                                  int(self.CanvasTabPhys.hsize * 0.15), 0))
        for i in range(len(self.ListCanvasPhys)):
            ChangeCursor(self.ListCanvasPhys[i].CanvasListIma)

            def GestEvt(_, j=i):
                return self.ClicOnPhys(j)

            self.ListCanvasPhys[i].CanvasListIma.bind('<ButtonRelease-1>', GestEvt)
        self.ListCanvasPhys[5].CanvasListIma.bind('<Motion>', self.AffichLegendPara2)

        self.CanvasTabChim = CanvasInWindow('Media/FrmAccueil/Chimie/Tableau/', self.RatioX, self.RatioY,
                                            self.CanvasFrmFond, tk.CENTER, int(self.SX * 0.15), int(self.SY * 0.218),
                                            0)
        self.ListCanvasChim = []
        self.ListCanvasChim.append(CanvasInWindow('Media/FrmAccueil/Chimie/Titre/', self.RatioX, self.RatioY,
                                                  self.CanvasTabChim.CanvasListIma, tk.CENTER,
                                                  int(self.CanvasTabChim.wsize * 0.5),
                                                  int(self.CanvasTabChim.hsize * 0.205), 0))
        self.ListCanvasChim.append(CanvasInWindow('Media/FrmAccueil/Chimie/PC/', self.RatioX, self.RatioY,
                                                  self.CanvasTabChim.CanvasListIma, tk.W,
                                                  int(self.CanvasTabChim.wsize * 0.1),
                                                  int(self.CanvasTabChim.hsize * 0.36), 0))
        self.ListCanvasChim.append(CanvasInWindow('Media/FrmAccueil/Chimie/Mat/', self.RatioX, self.RatioY,
                                                  self.CanvasTabChim.CanvasListIma, tk.W,
                                                  int(self.CanvasTabChim.wsize * 0.1),
                                                  int(self.CanvasTabChim.hsize * 0.48), 0))
        self.ListCanvasChim.append(CanvasInWindow('Media/FrmAccueil/Chimie/TP/', self.RatioX, self.RatioY,
                                                  self.CanvasTabChim.CanvasListIma, tk.W,
                                                  int(self.CanvasTabChim.wsize * 0.1),
                                                  int(self.CanvasTabChim.hsize * 0.59), 0))
        self.ListCanvasChim.append(CanvasInWindow('Media/FrmAccueil/Chimie/ComP/', self.RatioX, self.RatioY,
                                                  self.CanvasTabChim.CanvasListIma, tk.W,
                                                  int(self.CanvasTabChim.wsize * 0.1),
                                                  int(self.CanvasTabChim.hsize * 0.70), 0))
        self.ListCanvasChim.append(CanvasInWindow('Media/FrmAccueil/Chimie/ComMat/', self.RatioX, self.RatioY,
                                                  self.CanvasTabChim.CanvasListIma, tk.W,
                                                  int(self.CanvasTabChim.wsize * 0.1),
                                                  int(self.CanvasTabChim.hsize * 0.81), 0))
        self.ListCanvasChim.append(CanvasInWindow('Media/FrmAccueil/Chimie/Doc/', self.RatioX * 0.3, self.RatioY * 0.3,
                                                  self.CanvasFrmFond, tk.W, int(self.SX * 0.425),
                                                  int(self.SY * 0.7), 0))
        self.ListCanvasChim.append(CanvasInWindow('Media/FrmAccueil/Para/', self.RatioX * 0.15, self.RatioY * 0.15,
                                                  self.CanvasTabChim.CanvasListIma, tk.W,
                                                  int(self.CanvasTabChim.wsize * 0.905),
                                                  int(self.CanvasTabChim.hsize * 0.15), 0))

        for i in range(len(self.ListCanvasChim)):
            ChangeCursor(self.ListCanvasChim[i].CanvasListIma)

            def GestEvt(_, j=i):
                return self.ClicOnChim(j)

            self.ListCanvasChim[i].CanvasListIma.bind('<ButtonRelease-1>', GestEvt)
        self.ListCanvasChim[7].CanvasListIma.bind('<Motion>', self.AffichLegendPara)

        if self.MainAppli.AffichLogo == 1 and self.MainAppli.LogoPath != '':
            if os.path.exists(self.MainAppli.LogoPath):
                self.LogoEtab = ImgInCanvas(self.CanvasFrmFond, self.MainAppli.LogoPath, 0.075, tk.CENTER,
                                            int(int(self.CanvasFrmFond['width']) * 0.5),
                                            int(int(self.CanvasFrmFond['height']) * 0.075))

        self.LegendAction = self.CanvasFrmFond.create_text(0, 0, anchor=tk.CENTER, text='', fill='black',
                                                           font=AppliFont('Helvetica', self.MainAppli.SizeFont, 'bold',
                                                                          0))

        self.CanvasTabChim.CanvasListIma.bind('<Motion>', self.MoveOnCanvasTab)
        self.CanvasTabPhys.CanvasListIma.bind('<Motion>', self.MoveOnCanvasTab)
        self.CanvasFrmFond.bind('<Motion>', self.MoveOnCanvas)
        self.CanvasFrmFond.bind('<Button-1>', self.ClicOnCanvas)
        self.CanvasFrmFond.focus_set()
        self.CanvasFrmFond.pack()

    def MoveOnCanvas(self, evt):
        self.MainAppli.CanvasBtnQuit.itemconfig(self.MainAppli.ImgCanvasBtnQuit, image=self.MainAppli.ImgBtnQuit[0])
        if (self.SX * 0.393 < evt.x < self.SX * 0.515 and self.SY * 0.393 < evt.y < self.SY * 0.496) or \
                (self.SX * 0.507 < evt.x < self.SX * 0.531 and self.SY * 0.482 < evt.y < self.SY * 0.558):
            self.AffichLegendAction(evt.x, evt.y, 'Travaux pratiques', 'red')
        elif (self.SX * 0.334 < evt.x < self.SX * 0.357 and self.SY * 0.394 < evt.y < self.SY * 0.472) or \
                (self.SX * 0.281 < evt.x < self.SX * 0.313 and self.SY * 0.374 < evt.y < self.SY * 0.475) or \
                (self.SX * 0.247 < evt.x < self.SX * 0.269 and self.SY * 0.461 < evt.y < self.SY * 0.486) or \
                (self.SX * 0.631 < evt.x < self.SX * 0.657 and self.SY * 0.343 < evt.y < self.SY * 0.392):
            self.AffichLegendAction(evt.x, evt.y, 'Matériel', 'red')
        elif (self.SX * 0.209 < evt.x < self.SX * 0.228 and self.SY * 0.396 < evt.y < self.SY * 0.441) or \
                (self.SX * 0.059 < evt.x < self.SX * 0.083 and self.SY * 0.496 < evt.y < self.SY * 0.535) or \
                (self.SX * 0.572 < evt.x < self.SX * 0.591 and self.SY * 0.346 < evt.y < self.SY * 0.409):
            self.AffichLegendAction(evt.x, evt.y, 'Produits chimiques', 'red')
        elif self.SX * 0.110 < evt.x < self.SX * 0.130 and self.SY * 0.457 < evt.y < self.SY * 0.505:
            self.AffichLegendAction(evt.x, evt.y, 'Commandes produits', 'red')
        elif self.SX * 0.130 < evt.x < self.SX * 0.150 and self.SY * 0.457 < evt.y < self.SY * 0.505:
            self.AffichLegendAction(evt.x, evt.y, 'Commandes matériel', 'red')
        elif self.SX * 0.120 < evt.x < self.SX * 0.169 and self.SY * 0.505 < evt.y < self.SY * 0.552:
            self.AffichLegendAction(evt.x, evt.y, 'Fournisseurs', 'red')
        elif self.SX * 0.763 < evt.x < self.SX * 0.952 and self.SY * 0.455 < evt.y < self.SY * 0.535:
            self.AffichLegendAction(evt.x, evt.y, 'Travaux pratiques', 'blue')
        elif self.SX * 0.678 < evt.x < self.SX * 0.760 and self.SY * 0.473 < evt.y < self.SY * 0.531:
            self.AffichLegendAction(evt.x, evt.y, 'Matériel', 'blue')
        elif self.SX * 0.617 < evt.x < self.SX * 0.672 and self.SY * 0.517 < evt.y < self.SY * 0.55:
            self.AffichLegendAction(evt.x, evt.y, 'Commandes', 'blue')
        elif self.SX * 0.617 < evt.x < self.SX * 0.672 and self.SY * 0.55 < evt.y < self.SY * 0.583:
            self.AffichLegendAction(evt.x, evt.y, 'Fournisseurs', 'blue')
        else:
            self.CanvasFrmFond.configure(cursor='')
            self.CanvasFrmFond.itemconfigure(self.LegendAction, text="")

    def ClicOnCanvas(self, evt):
        if (self.SX * 0.393 < evt.x < self.SX * 0.515 and self.SY * 0.393 < evt.y < self.SY * 0.496) or \
                (self.SX * 0.507 < evt.x < self.SX * 0.531 and self.SY * 0.482 < evt.y < self.SY * 0.558):
            self.ClicOnChim(3)
        elif (self.SX * 0.343 < evt.x < self.SX * 0.357 and self.SY * 0.446 < evt.y < self.SY * 0.472) or \
                (self.SX * 0.281 < evt.x < self.SX * 0.313 and self.SY * 0.374 < evt.y < self.SY * 0.475) or \
                (self.SX * 0.247 < evt.x < self.SX * 0.269 and self.SY * 0.461 < evt.y < self.SY * 0.486) or \
                (self.SX * 0.631 < evt.x < self.SX * 0.657 and self.SY * 0.343 < evt.y < self.SY * 0.392):
            self.ClicOnChim(2)
        elif (self.SX * 0.209 < evt.x < self.SX * 0.228 and self.SY * 0.396 < evt.y < self.SY * 0.441) or \
                (self.SX * 0.059 < evt.x < self.SX * 0.083 and self.SY * 0.496 < evt.y < self.SY * 0.535) or \
                (self.SX * 0.572 < evt.x < self.SX * 0.591 and self.SY * 0.346 < evt.y < self.SY * 0.409):
            self.ClicOnChim(1)
        elif self.SX * 0.110 < evt.x < self.SX * 0.130 and self.SY * 0.457 < evt.y < self.SY * 0.505:
            self.ClicOnChim(4)
        elif self.SX * 0.130 < evt.x < self.SX * 0.150 and self.SY * 0.457 < evt.y < self.SY * 0.505:
            self.ClicOnChim(5)
        elif self.SX * 0.120 < evt.x < self.SX * 0.169 and self.SY * 0.505 < evt.y < self.SY * 0.552:
            self.ClicOnChim(8)
        elif self.SX * 0.763 < evt.x < self.SX * 0.952 and self.SY * 0.455 < evt.y < self.SY * 0.535:
            self.ClicOnPhys(2)
        elif self.SX * 0.678 < evt.x < self.SX * 0.760 and self.SY * 0.473 < evt.y < self.SY * 0.531:
            self.ClicOnPhys(1)
        elif self.SX * 0.617 < evt.x < self.SX * 0.672 and self.SY * 0.517 < evt.y < self.SY * 0.55:
            self.ClicOnPhys(3)
        elif self.SX * 0.617 < evt.x < self.SX * 0.672 and self.SY * 0.55 < evt.y < self.SY * 0.583:
            self.ClicOnPhys(6)
        elif self.MainAppli.NAction == 5:
            self.MainAppli.FrmParam.lift()

    def ClicOnPhys(self, j):
        if self.MainAppli.NAction == 0:
            if j != 5:
                self.destroy()
                self.MainAppli.FrmAccueil = None
            self.MainAppli.BaseEnCours = 'PHYS'
            if j == 0 or j == 1:
                self.MainAppli.Action2()
            elif j == 2:
                self.MainAppli.Action3()
            elif j == 3:
                self.MainAppli.Action2()
                self.MainAppli.ActionV(7)
            elif j == 4:
                self.MainAppli.Action4()
            elif j == 5:
                self.MainAppli.Action5()
            elif j == 6:
                self.MainAppli.Action6()
        else:
            winsound.PlaySound("*", winsound.SND_ASYNC)
            self.MainAppli.FrmParam.lift()

    def ClicOnChim(self, j):
        if self.MainAppli.NAction == 0:
            if j != 7:
                self.destroy()
                self.MainAppli.FrmAccueil = None
            self.MainAppli.BaseEnCours = 'CHIMIE'
            if j == 0 or j == 1:
                self.MainAppli.Action1()
            elif j == 2:
                self.MainAppli.Action2()
            elif j == 3:
                self.MainAppli.Action3()
            elif j == 4:
                self.MainAppli.Action1()
                self.MainAppli.ActionV(8)
            elif j == 5:
                self.MainAppli.Action2()
                self.MainAppli.ActionV(7)
            elif j == 6:
                self.MainAppli.Action4()
            elif j == 7:
                self.MainAppli.Action5()
            elif j == 8:
                self.MainAppli.Action6()
        else:
            winsound.PlaySound("*", winsound.SND_ASYNC)
            self.MainAppli.FrmParam.lift()

    def AffichLegendAction(self, x, y, info, color):
        self.CanvasFrmFond.configure(cursor='hand2')
        self.CanvasFrmFond.delete(self.LegendAction)
        self.LegendAction = self.CanvasFrmFond.create_text(x, y, anchor=tk.CENTER, text=info, fill=color,
                                                           font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2,
                                                                          'bold', 0))

    def AffichLegendPara(self, evt):
        self.CanvasFrmFond.delete(self.LegendAction)
        self.LegendAction = self.CanvasFrmFond.create_text(evt.x + int(self.SX * 0.25), evt.y + int(self.SY * 0.014),
                                                           anchor=tk.CENTER, text='Paramètres BDD Chimie', fill='red',
                                                           font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2,
                                                                          'bold', 0))

    def AffichLegendPara2(self, evt):
        self.CanvasFrmFond.delete(self.LegendAction)
        self.LegendAction = self.CanvasFrmFond.create_text(evt.x + int(self.SX * 0.92), evt.y + int(self.SY * 0.02),
                                                           anchor=tk.CENTER, text='Paramètres BDB Physique',
                                                           fill='Blue',
                                                           font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2,
                                                                          'bold', 0))

    def MoveOnCanvasTab(self, _):
        self.CanvasFrmFond.configure(cursor='')
        self.CanvasFrmFond.itemconfigure(self.LegendAction, text="")
