from AppliDef import *


def ModifListDanger(List):
    for i in range(len(List)):
        if List[i] == 'AttentionGHS':
            List[i] = 0
        elif List[i] == 'CancerGHS':
            List[i] = 1
        elif List[i] == 'CGHS':
            List[i] = 2
        elif List[i] == 'ComburantGHS':
            List[i] = 3
        elif List[i] == 'DangerGHS':
            List[i] = 4
        elif List[i] == 'ExplosifGHS':
            List[i] = 5
        elif List[i] == 'FGHS':
            List[i] = 6
        elif List[i] == 'GazGHS':
            List[i] = 7
        elif List[i] == 'PolluantGHS':
            List[i] = 8
        elif List[i] == 'TGHS':
            List[i] = 9
    return List


def ModifFB(FB):
    LChiffre = ['0','1','2','3','4','5','6','7','8','9']
    indice = [0x2080,0x2081,0x2082,0x2083,0x2084,0x2085,0x2086,0x2087,0x2088,0x2089]
    ch = FB[0]
    for i in range(1, len(FB)-1):
        if FB[i] in LChiffre and FB[i - 1] != ' ' and FB[i - 1] != ',' and FB[i + 1] != ' ':
            ch = ch + chr(indice[int(FB[i])])
        else:
            ch = ch + FB[i]
    if FB[-1] in LChiffre:
        ch = ch + chr(indice[int(FB[-1])])
    else:
        ch = ch + FB[-1]
    return ch


class FrmEtagProduits(tk.Toplevel):
    def __init__(self, container, ListProduits, RatioX, RatioY, Appli):
        tk.Toplevel.__init__(self, container)
        self.MainAppli = Appli
        self.parent = container
        self.ListProduits = ListProduits
        self.RatioX = RatioX
        self.RatioY = RatioY
        self.ListProduitsEtag = []
        self.NEtag = 0
        self.NProdEtag = 0

        self.ImgEtagProduits, wsize, hsize, self.CanvasEtagProduits, self.ImgCanvasEtagProduits = CanInWindow(
            'Media/FrmEtagProduits/Fond/', self.RatioX, self.RatioY, container, tk.CENTER,
            int(self.MainAppli.SX * 0.462), int(self.MainAppli.SY * 0.527), 0)
        self.DefilProduits = tk.Scale(self.CanvasEtagProduits, orient=tk.HORIZONTAL, length=wsize, showvalue=0)
        self.CanvasEtagProduits.create_window(int(wsize / 2), int(hsize * 0.99), anchor=tk.CENTER,
                                              window=self.DefilProduits)
        self.DefilProduits.config(command=self.DefilEtagProduits)
        self.defilmax = CalculDefilMax(self.ListProduits, 15)
        self.DefilProduits.config(from_=0, to=self.defilmax, resolution=1)
        self.DefilProduits.bind('<MouseWheel>', self.MouseWheelDefil)

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

        self.ImgFrmFond = []
        self.ImgFrmFond = RedimImg(self.SX, self.SY, 'Media/FrmEtagProduits/Etag/')
        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.176)) + "+" +
                      str(int(screen.GetSystemMetrics(1) * 0.083)))
        self.resizable(width=False, height=False)
        self.overrideredirect(True)
        self.lift()
        self.AffichProduits(self.NEtag)
        if len(ListProduits) > 0:
            self.SelectProd(0)
        self.CanvasFrmFond.bind('<MouseWheel>', self.MouseWheelSelect)
        self.CanvasFrmFond.bind('<Button-1>', self.ClicOnCanvas)
        self.CanvasFrmFond.pack()

    def AffichProduits(self, NEtag):
        NProd = 0
        for l in range(3):
            if NProd >= len(self.ListProduits):
                break
            for k in range(5):
                NProd = k + l * 5 + NEtag * 15
                if NProd >= len(self.ListProduits):
                    break
                listGHS = []
                FB = ''
                FormDev = ''
                if self.ListProduits[NProd][2] is not None:
                    FormDev = self.ListProduits[NProd][2]
                for i in range(3):
                    if self.ListProduits[NProd][i + 3] is not None and self.ListProduits[NProd][i + 3] != '':
                        listGHS.append(self.ListProduits[NProd][i + 3])
                listGHS = ModifListDanger(listGHS)
                if self.ListProduits[NProd][6] is not None and self.ListProduits[NProd][6] != '':
                    FB = self.ListProduits[NProd][6]
                    FB = ModifFB(FB)
                if self.ListProduits[NProd][12] is not None and self.ListProduits[NProd][12] != '' \
                        or (self.ListProduits[NProd][1] == 'mL' and self.ListProduits[NProd][7].upper() != 'GAZ'):
                    self.ListProduitsEtag.append(Bouteille(self.CanvasFrmFond, self.ListProduits[NProd][0],
                                                           FormDev, listGHS, FB, int(self.SX * 0.1 + k * self.SX * 0.2),
                                                           int(self.SY * 0.185 + l * self.SY * 0.322),
                                                           self.RatioX, self.RatioY * 0.9))
                elif self.ListProduits[NProd][1] == 'g':
                    self.ListProduitsEtag.append(Flacon(self.CanvasFrmFond, self.ListProduits[NProd][0], FormDev,
                                                        listGHS, FB, int(self.SX * 0.1 + k * self.SX * 0.2),
                                                        int(self.SY * 0.182 + l * self.SY * 0.323),
                                                        self.RatioX, self.RatioY * 0.9))
                elif self.ListProduits[NProd][7] is not None and self.ListProduits[NProd][7].upper() == 'GAZ':
                    self.ListProduitsEtag.append(BouteilleGaz(self.CanvasFrmFond, self.ListProduits[NProd][0], FormDev,
                                                              listGHS, FB, int(self.SX * 0.1 + k * self.SX * 0.2),
                                                              int(self.SY * 0.171 + l * self.SY * 0.323),
                                                              self.RatioX, self.RatioY))
                elif self.ListProduits[NProd][1].upper() == 'UNIT' or self.ListProduits[NProd][1] == '':
                    self.ListProduitsEtag.append(Boite(self.CanvasFrmFond, self.ListProduits[NProd][0], FormDev,
                                                       listGHS, FB, int(self.SX * 0.1 + k * self.SX * 0.2),
                                                       int(self.SY * 0.22 + l * self.SY * 0.323),
                                                       self.RatioX * 1.2, self.RatioX * 1.2))

        for i in range(len(self.ListProduitsEtag)):
            ChangeCursor(self.ListProduitsEtag[i].CanvasImg)

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

            self.ListProduitsEtag[i].CanvasImg.bind('<ButtonRelease-1>', GestEvt)
            for g in range(len(self.ListProduitsEtag[i].CanvasGHS)):
                self.ListProduitsEtag[i].CanvasGHS[g].CanvasListIma.bind('<ButtonRelease-1>', GestEvt)
            try:
                self.ListProduitsEtag[i].CanvasImgEtiqProd.bind('<ButtonRelease-1>', GestEvt)
            except:
                pass

    def DefilEtagProduits(self, _):
        if self.MainAppli.NAction != 102 and self.MainAppli.NAction != 1021 and self.MainAppli.NAction != 202 and \
                self.MainAppli.NAction != 2021 and self.MainAppli.NAction != 5:
            for widget in self.CanvasFrmFond.winfo_children():
                widget.destroy()
            for i in range(len(self.ListProduitsEtag)):
                if self.ListProduitsEtag[i].NomProduit is not None:
                    self.CanvasFrmFond.delete(self.ListProduitsEtag[i].NomProduit)
                if self.ListProduitsEtag[i].NomProduit1 is not None:
                    self.CanvasFrmFond.delete(self.ListProduitsEtag[i].NomProduit1)
                if self.ListProduitsEtag[i].NomProduit2 is not None:
                    self.CanvasFrmFond.delete(self.ListProduitsEtag[i].NomProduit2)
            self.NEtag = self.DefilProduits.get()
            self.MainAppli.NEtag = self.NEtag
            self.ListProduitsEtag = []
            self.AffichProduits(self.NEtag)
            if 0 < self.MainAppli.IndexElt - self.NEtag * 15 < 15:
                self.SelectProd(self.MainAppli.IndexElt - self.NEtag * 15)
            else:
                self.SelectProd(0)
        else:
            self.DefilProduits.set(self.NEtag)
            self.MainAppli.FrmLift()

    def MouseWheelDefil(self, event):
        if self.MainAppli.NAction != 102 and self.MainAppli.NAction != 1021 and self.MainAppli.NAction != 202 and \
                self.MainAppli.NAction != 2021 and self.MainAppli.NAction != 5:
            if event.delta == -120:
                self.NEtag = self.NEtag - 1
                if self.NEtag < 0:
                    self.NEtag = 0
            if event.delta == 120:
                self.NEtag = self.NEtag + 1
                if self.NEtag > self.defilmax:
                    self.NEtag = self.defilmax
            self.DefilProduits.set(self.NEtag)
            self.MainAppli.NEtag = self.NEtag

    def MouseWheelSelect(self, event):
        if self.MainAppli.NAction != 102 and self.MainAppli.NAction != 1021 and self.MainAppli.NAction != 202 and \
                self.MainAppli.NAction != 2021 and self.MainAppli.NAction != 5:
            if event.delta == -120:
                self.NProdEtag = self.NProdEtag - 1
                if self.NProdEtag < 0:
                    if self.NEtag > 0:
                        self.NEtag = self.NEtag - 1
                        self.NProdEtag = 14
                        self.MainAppli.IndexElt = self.NProdEtag + self.NEtag * 15
                        self.MainAppli.NEtag = self.NEtag
                        self.DefilProduits.set(self.NEtag)
                    else:
                        self.NProdEtag = 0
                        self.SelectProd(self.NProdEtag)
                else:
                    self.SelectProd(self.NProdEtag)
            if event.delta == 120:
                self.NProdEtag = self.NProdEtag + 1
                if self.NProdEtag > 14 or self.NProdEtag + self.NEtag * 15 > len(self.ListProduits) - 1:
                    if self.NEtag < self.defilmax:
                        self.NEtag = self.NEtag + 1
                        self.NProdEtag = 0
                        self.MainAppli.IndexElt = self.NProdEtag + self.NEtag * 15
                        self.MainAppli.NEtag = self.NEtag
                        self.DefilProduits.set(self.NEtag)
                    else:
                        self.NProdEtag = self.NProdEtag - 1
                        self.SelectProd(self.NProdEtag)
                else:
                    self.SelectProd(self.NProdEtag)

    def SelectProd(self, j):
        if self.MainAppli.NAction != 102 and self.MainAppli.NAction != 1021 and self.MainAppli.NAction != 202 and \
                self.MainAppli.NAction != 2021 and self.MainAppli.NAction != 5:
            for i in range(len(self.ListProduitsEtag)):
                if self.ListProduitsEtag[i].NomProduit is not None:
                    self.CanvasFrmFond.itemconfigure(self.ListProduitsEtag[i].NomProduit, fill='black')
                if self.ListProduitsEtag[i].NomProduit1 is not None:
                    self.CanvasFrmFond.itemconfigure(self.ListProduitsEtag[i].NomProduit1, fill='black')
                if self.ListProduitsEtag[i].NomProduit2 is not None:
                    self.CanvasFrmFond.itemconfigure(self.ListProduitsEtag[i].NomProduit2, fill='black')
            if self.ListProduitsEtag[j].NomProduit is not None:
                self.CanvasFrmFond.itemconfigure(self.ListProduitsEtag[j].NomProduit, fill='blue')
            if self.ListProduitsEtag[j].NomProduit1 is not None:
                self.CanvasFrmFond.itemconfigure(self.ListProduitsEtag[j].NomProduit1, fill='blue')
            if self.ListProduitsEtag[j].NomProduit2 is not None:
                self.CanvasFrmFond.itemconfigure(self.ListProduitsEtag[j].NomProduit2, fill='blue')

            self.MainAppli.ListBoxProduits.selection_clear(0, tk.END)
            self.MainAppli.IndexElt = j + self.NEtag * 15
            self.NProdEtag = j
            self.MainAppli.ListBoxProduits.selection_set(self.MainAppli.IndexElt)
            self.MainAppli.ListBoxProduits.see(self.MainAppli.IndexElt)
            if not self.MainAppli.Init:
                self.MainAppli.InfoProd.Prod = self.ListProduits[self.MainAppli.IndexElt]
                if self.MainAppli.InfoProd.MenuOnglets.MenuSelected[0]:
                    self.MainAppli.InfoProd.AffichInfo()
                elif self.MainAppli.InfoProd.MenuOnglets.MenuSelected[1]:
                    self.MainAppli.InfoProd.AffichFDS()
                elif self.MainAppli.InfoProd.MenuOnglets.MenuSelected[2]:
                    self.MainAppli.InfoProd.AffichStock()
                self.MainAppli.InfoProd.lift()
            self.lift()
        elif self.MainAppli.NAction == 5:
            self.MainAppli.FrmLift()

    def ClicOnCanvas(self, _):
        if self.MainAppli.NAction == 5:
            self.MainAppli.FrmLift()


class Bouteille:
    def __init__(self, container, Nom, FormDev, listGHS, FB, X, Y, RX, RY):
        self.ImgBouteille = []
        img = Image.open('Media/FrmEtagProduits/Bouteille/Bouteille.bmp')
        imgwsize = int((float(img.size[0]) * float(RX)))
        imghsize = int((float(img.size[1]) * float(RY)))
        img = img.resize((imgwsize, imghsize), Image.ANTIALIAS)
        self.ImgBouteille.append(ImageTk.PhotoImage(img))

        self.CanvasImg = CanvasApp(container, width=imgwsize, height=imghsize)
        container.create_window(X, Y, anchor=tk.CENTER, window=self.CanvasImg)
        self.ImgCanvasImgBouteille = self.CanvasImg.create_image(0, 0, anchor=tk.NW,
                                                                 image=self.ImgBouteille[0])
        if len(Nom) >= 35:
            sizefont = 7
        else:
            sizefont = 10

        if len(Nom) >= 41:
            self.NomProduit = None
            (Nom1, Nom2) = (Nom[:len(Nom) // 2], Nom[len(Nom) // 2:])
            self.NomProduit1 = container.create_text(X, Y + imghsize * 0.6, text=Nom1 + '-',
                                                     font=AppliFont('Helvetica', sizefont, 'bold', 0), anchor=tk.CENTER)
            self.NomProduit2 = container.create_text(X, Y + imghsize * 0.64, text=Nom2,
                                                     font=AppliFont('Helvetica', sizefont, 'bold', 0), anchor=tk.CENTER)
        else:
            self.NomProduit1 = None
            self.NomProduit2 = None
            self.NomProduit = container.create_text(X, Y + imghsize * 0.62, text=Nom,
                                                    font=AppliFont('Helvetica', sizefont, 'bold', 0), anchor=tk.CENTER)

        if FormDev != '':
            self.ImgEtiqProd = []
            img = Image.open('Datas/Formdev/' + FormDev)
            imgEtiqwsize = int(imgwsize * 0.6)
            imgEtiqhsize = int(imgEtiqwsize * float(img.size[1]) / float(img.size[0]))
            if imgEtiqhsize > imghsize * 0.25:
                imgEtiqhsize = int(imghsize * 0.25)
                imgEtiqwsize = int(imgEtiqhsize * float(img.size[0]) / float(img.size[1]))
            img = img.resize((imgEtiqwsize, imgEtiqhsize), Image.ANTIALIAS)
            self.ImgEtiqProd.append(ImageTk.PhotoImage(img))

            self.CanvasImgEtiqProd = CanvasApp(self.CanvasImg, width=imgEtiqwsize, height=imgEtiqhsize)
            self.CanvasImg.create_window(int(imgwsize * 0.5), int(imghsize * 0.48), anchor=tk.CENTER,
                                         window=self.CanvasImgEtiqProd)
            self.ImgCanvasImgEtiqProd = self.CanvasImgEtiqProd.create_image(0, 0, anchor=tk.NW,
                                                                            image=self.ImgEtiqProd[0])
            CreateCadre(self.CanvasImg, int(imgwsize * 0.49 - imgEtiqwsize / 2),
                        int(imghsize * 0.475 - imgEtiqhsize / 2),
                        int(imgwsize * 0.51 - imgEtiqwsize / 2) + imgEtiqwsize,
                        int(imghsize * 0.485 - imgEtiqhsize / 2) + imgEtiqhsize)
        else:
            if len(Nom) > 17:
                NomBout = Nom.split(' ')
                for i in range(len(NomBout)):
                    self.CanvasImg.create_text(int(imgwsize * 0.5), int(imghsize * 0.4 + i * imghsize * 0.05),
                                               text=NomBout[i],
                                               font=AppliFont('Calibri', sizefont - 4, 'bold', 0), anchor=tk.CENTER)
            else:
                self.CanvasImg.create_text(int(imgwsize * 0.5), int(imghsize * 0.4), text=Nom,
                                           font=AppliFont('Calibri', sizefont - 4, 'bold', 0), anchor=tk.CENTER)

        self.CanvasGHS = []
        deb = 0
        if len(listGHS) == 3:
            deb = int(imgwsize * 0.25)
        elif len(listGHS) == 2:
            deb = int(imgwsize * 0.38)
        elif len(listGHS) == 1:
            deb = int(imgwsize * 0.5)
        for i in range(len(listGHS)):
            self.CanvasGHS.append(CanvasInWindow('Media/PictoDanger/', RX * 0.25, RY * 0.25, self.CanvasImg,
                                                 tk.CENTER, deb + int(i * imgwsize * 0.25), int(imghsize * 0.8),
                                                 listGHS[i]))

        self.FBProduit = self.CanvasImg.create_text(int(imgwsize * 0.5), int(imghsize * 0.64), text=FB,
                                                    font=AppliFont('Calibri', 8, 'bold', 0),
                                                    anchor=tk.CENTER)


class Flacon:
    def __init__(self, container, Nom, FormDev, listGHS, FB, X, Y, RX, RY):
        self.ImgFlacon = []
        img = Image.open('Media/FrmEtagProduits/Flacon/Flacon.bmp')
        imgwsize = int((float(img.size[0]) * float(RX)))
        imghsize = int((float(img.size[1]) * float(RY)))
        img = img.resize((imgwsize, imghsize), Image.ANTIALIAS)
        self.ImgFlacon.append(ImageTk.PhotoImage(img))

        self.CanvasImg = CanvasApp(container, width=imgwsize, height=imghsize)
        container.create_window(X, Y, anchor=tk.CENTER, window=self.CanvasImg)
        self.ImgCanvasImgFlacon = self.CanvasImg.create_image(0, 0, anchor=tk.NW, image=self.ImgFlacon[0])

        if len(Nom) >= 35:
            sizefont = 7
        else:
            sizefont = 10

        if len(Nom) >= 41:
            self.NomProduit = None
            (Nom1, Nom2) = (Nom[:len(Nom) // 2], Nom[len(Nom) // 2:])
            self.NomProduit1 = container.create_text(X, Y + imghsize * 0.632, text=Nom1 + '-',
                                                     font=AppliFont('Helvetica', sizefont, 'bold', 0), anchor=tk.CENTER)
            self.NomProduit2 = container.create_text(X, Y + imghsize * 0.672, text=Nom2,
                                                     font=AppliFont('Helvetica', sizefont, 'bold', 0), anchor=tk.CENTER)
        else:
            self.NomProduit1 = None
            self.NomProduit2 = None
            self.NomProduit = container.create_text(X, Y + imghsize * 0.652, text=Nom,
                                                    font=AppliFont('Helvetica', sizefont, 'bold', 0), anchor=tk.CENTER)
        if FormDev != '' and os.path.exists('Datas/Formdev/' + FormDev):
            self.ImgEtiqProd = []
            img = Image.open('Datas/Formdev/' + FormDev)
            imgEtiqwsize = int(imgwsize * 0.7)
            imgEtiqhsize = int(imgEtiqwsize * float(img.size[1]) / float(img.size[0]))
            if imgEtiqhsize > imghsize * 0.25:
                imgEtiqhsize = int(imghsize * 0.25)
                imgEtiqwsize = int(imgEtiqhsize * float(img.size[0]) / float(img.size[1]))
            img = img.resize((imgEtiqwsize, imgEtiqhsize), Image.ANTIALIAS)
            self.ImgEtiqProd.append(ImageTk.PhotoImage(img))

            self.CanvasImgEtiqProd = CanvasApp(self.CanvasImg, width=imgEtiqwsize, height=imgEtiqhsize)
            self.CanvasImg.create_window(int(imgwsize * 0.5), int(imghsize * 0.5), anchor=tk.CENTER,
                                         window=self.CanvasImgEtiqProd)
            self.ImgCanvasImgEtiqProd = self.CanvasImgEtiqProd.create_image(0, 0, anchor=tk.NW,
                                                                            image=self.ImgEtiqProd[0])
            CreateCadre(self.CanvasImg, int(imgwsize * 0.49 - imgEtiqwsize / 2),
                        int(imghsize * 0.495 - imgEtiqhsize / 2),
                        int(imgwsize * 0.51 - imgEtiqwsize / 2) + imgEtiqwsize,
                        int(imghsize * 0.505 - imgEtiqhsize / 2) + imgEtiqhsize)
        else:
            if len(Nom) > 17:
                NomFlacon = Nom.split(' ')
                for i in range(len(NomFlacon)):
                    self.CanvasImg.create_text(int(imgwsize * 0.5), int(imghsize * 0.4 + i * imghsize * 0.05),
                                               text=NomFlacon[i],
                                               font=AppliFont('Calibri', sizefont - 4, 'bold', 0), anchor=tk.CENTER)
            else:
                self.CanvasImg.create_text(int(imgwsize * 0.5), int(imghsize * 0.4), text=Nom,
                                           font=AppliFont('Calibri', sizefont - 4, 'bold', 0), anchor=tk.CENTER)

        self.CanvasGHS = []
        deb = 0
        if len(listGHS) == 3:
            deb = int(imgwsize * 0.25)
        elif len(listGHS) == 2:
            deb = int(imgwsize * 0.38)
        elif len(listGHS) == 1:
            deb = int(imgwsize * 0.5)
        for i in range(len(listGHS)):
            self.CanvasGHS.append(CanvasInWindow('Media/PictoDanger/', RX * 0.25, RY * 0.25,
                                                 self.CanvasImg, tk.CENTER,
                                                 deb + int(i * imgwsize * 0.25), int(imghsize * 0.78), listGHS[i]))

        self.FBProduit = self.CanvasImg.create_text(int(imgwsize * 0.5), int(imghsize * 0.68), text=FB,
                                                    font=AppliFont('Calibri', 8, 'bold', 0),
                                                    anchor=tk.CENTER)


class BouteilleGaz:
    def __init__(self, container, Nom, FormDev, listGHS, FB, X, Y, RX, RY):
        self.ImgBoutGaz = []
        img = Image.open('Media/FrmEtagProduits/Gaz/Gaz.bmp')
        imgwsize = int((float(img.size[0]) * float(RX)))
        imghsize = int((float(img.size[1]) * float(RY)))
        img = img.resize((imgwsize, imghsize), Image.ANTIALIAS)
        self.ImgBoutGaz.append(ImageTk.PhotoImage(img))

        self.CanvasImg = CanvasApp(container, width=imgwsize, height=imghsize)
        container.create_window(X, Y, anchor=tk.CENTER, window=self.CanvasImg)
        self.ImgCanvasImgBoutGaz = self.CanvasImg.create_image(0, 0, anchor=tk.NW, image=self.ImgBoutGaz[0])
        if len(Nom) >= 35:
            sizefont = 7
        else:
            sizefont = 10

        if len(Nom) >= 41:
            self.NomProduit = None
            (Nom1, Nom2) = (Nom[:len(Nom) // 2], Nom[len(Nom) // 2:])
            self.NomProduit1 = container.create_text(X, Y + imghsize * 0.575, text=Nom1 + '-',
                                                     font=AppliFont('Helvetica', sizefont, 'bold', 0), anchor=tk.CENTER)
            self.NomProduit2 = container.create_text(X, Y + imghsize * 0.615, text=Nom2,
                                                     font=AppliFont('Helvetica', sizefont, 'bold', 0), anchor=tk.CENTER)
        else:
            self.NomProduit1 = None
            self.NomProduit2 = None
            self.NomProduit = container.create_text(X, Y + imghsize * 0.595, text=Nom,
                                                    font=AppliFont('Helvetica', sizefont, 'bold', 0), anchor=tk.CENTER)

        if FormDev != '':
            self.ImgEtiqProd = []
            img = Image.open('Datas/Formdev/' + FormDev)
            imgEtiqwsize = int(imgwsize * 0.5)
            imgEtiqhsize = int(imgEtiqwsize * float(img.size[1]) / float(img.size[0]))
            if imgEtiqhsize > imghsize * 0.25:
                imgEtiqhsize = int(imghsize * 0.25)
                imgEtiqwsize = int(imgEtiqhsize * float(img.size[0]) / float(img.size[1]))
            img = img.resize((imgEtiqwsize, imgEtiqhsize), Image.ANTIALIAS)
            self.ImgEtiqProd.append(ImageTk.PhotoImage(img))

            self.CanvasImgEtiqProd = CanvasApp(self.CanvasImg, width=imgEtiqwsize, height=imgEtiqhsize)
            self.CanvasImg.create_window(int(imgwsize * 0.5), int(imghsize * 0.55), anchor=tk.CENTER,
                                         window=self.CanvasImgEtiqProd)
            self.ImgCanvasImgEtiqProd = self.CanvasImgEtiqProd.create_image(0, 0, anchor=tk.NW,
                                                                            image=self.ImgEtiqProd[0])
            CreateCadre(self.CanvasImg, int(imgwsize * 0.49 - imgEtiqwsize / 2),
                        int(imghsize * 0.545 - imgEtiqhsize / 2),
                        int(imgwsize * 0.51 - imgEtiqwsize / 2) + imgEtiqwsize,
                        int(imghsize * 0.555 - imgEtiqhsize / 2) + imgEtiqhsize)

        self.CanvasGHS = []
        deb = 0
        if len(listGHS) == 3:
            deb = int(imgwsize * 0.25)
        elif len(listGHS) == 2:
            deb = int(imgwsize * 0.38)
        elif len(listGHS) == 1:
            deb = int(imgwsize * 0.5)
        for i in range(len(listGHS)):
            self.CanvasGHS.append(CanvasInWindow('Media/PictoDanger/', RX * 0.22, RY * 0.22, self.CanvasImg,
                                                 tk.CENTER, deb + int(i * imgwsize * 0.25), int(imghsize * 0.8),
                                                 listGHS[i]))

        self.FBProduit = self.CanvasImg.create_text(int(imgwsize * 0.5), int(imghsize * 0.7), text=FB,
                                                    font=AppliFont('Calibri', 8, 'bold', 0),
                                                    anchor=tk.CENTER)


class Boite:
    def __init__(self, container, Nom, FormDev, listGHS, FB, X, Y, RX, RY):
        self.ImgBoite = []
        img = Image.open('Media/FrmEtagProduits/Boite/Boite.bmp')
        imgwsize = int((float(img.size[0]) * float(RX)))
        imghsize = int((float(img.size[1]) * float(RY)))
        img = img.resize((imgwsize, imghsize), Image.ANTIALIAS)
        self.ImgBoite.append(ImageTk.PhotoImage(img))

        self.CanvasImg = CanvasApp(container, width=imgwsize, height=imghsize)
        container.create_window(X, Y, anchor=tk.CENTER, window=self.CanvasImg)
        self.ImgCanvasImgBoite = self.CanvasImg.create_image(0, 0, anchor=tk.NW, image=self.ImgBoite[0])
        if len(Nom) >= 35:
            sizefont = 7
        else:
            sizefont = 10
        if len(Nom) >= 41:
            self.NomProduit = None
            (Nom1, Nom2) = (Nom[:len(Nom) // 2], Nom[len(Nom) // 2:])
            self.NomProduit1 = container.create_text(X, Y + imghsize * 0.75, text=Nom1 + '-',
                                                     font=AppliFont('Helvetica', sizefont, 'bold', 0), anchor=tk.CENTER)
            self.NomProduit2 = container.create_text(X, Y + imghsize * 0.82, text=Nom2,
                                                     font=AppliFont('Helvetica', sizefont, 'bold', 0), anchor=tk.CENTER)
        else:
            self.NomProduit1 = None
            self.NomProduit2 = None
            self.NomProduit = container.create_text(X, Y + imghsize * 0.78, text=Nom,
                                                    font=AppliFont('Helvetica', sizefont, 'bold', 0), anchor=tk.CENTER)

        if FormDev != '':
            self.ImgEtiqProd = []
            img = Image.open('Datas/Formdev/' + FormDev)
            imgEtiqwsize = int(imgwsize * 0.6)
            imgEtiqhsize = int(imgEtiqwsize * float(img.size[1]) / float(img.size[0]))
            if imgEtiqhsize > imghsize * 0.25:
                imgEtiqhsize = int(imghsize * 0.25)
                imgEtiqwsize = int(imgEtiqhsize * float(img.size[0]) / float(img.size[1]))
            img = img.resize((imgEtiqwsize, imgEtiqhsize), Image.ANTIALIAS)
            self.ImgEtiqProd.append(ImageTk.PhotoImage(img))

            self.CanvasImgEtiqProd = CanvasApp(self.CanvasImg, width=imgEtiqwsize, height=imgEtiqhsize)
            self.CanvasImg.create_window(int(imgwsize * 0.5), int(imghsize * 0.22), anchor=tk.CENTER,
                                         window=self.CanvasImgEtiqProd)
            self.ImgCanvasImgEtiqProd = self.CanvasImgEtiqProd.create_image(0, 0, anchor=tk.NW,
                                                                            image=self.ImgEtiqProd[0])
            CreateCadre(self.CanvasImg, int(imgwsize * 0.49 - imgEtiqwsize / 2),
                        int(imghsize * 0.215 - imgEtiqhsize / 2),
                        int(imgwsize * 0.51 - imgEtiqwsize / 2) + imgEtiqwsize,
                        int(imghsize * 0.225 - imgEtiqhsize / 2) + imgEtiqhsize)
        else:
            if len(Nom) > 17:
                NomBoite = Nom.split(' ')
                for i in range(len(NomBoite)):
                    self.CanvasImg.create_text(int(imgwsize * 0.5), int(imghsize * 0.2 + i * imghsize * 0.05),
                                               text=NomBoite[i],
                                               font=AppliFont('Calibri', sizefont - 2, 'bold', 0), anchor=tk.CENTER)
            else:
                self.CanvasImg.create_text(int(imgwsize * 0.5), int(imghsize * 0.2), text=Nom,
                                           font=AppliFont('Calibri', sizefont, 'bold', 0), anchor=tk.CENTER)

        self.CanvasGHS = []
        deb = 0
        if len(listGHS) == 3:
            deb = int(imgwsize * 0.25)
        elif len(listGHS) == 2:
            deb = int(imgwsize * 0.38)
        elif len(listGHS) == 1:
            deb = int(imgwsize * 0.5)
        for i in range(len(listGHS)):
            self.CanvasGHS.append(CanvasInWindow('Media/PictoDanger/', RX * 0.22, RY * 0.22, self.CanvasImg,
                                                 tk.CENTER, deb + int(i * imgwsize * 0.25), int(imghsize * 0.87),
                                                 listGHS[i]))

        self.FBProduit = self.CanvasImg.create_text(int(imgwsize * 0.5), int(imghsize * 0.7), text=FB,
                                                    font=AppliFont('Calibri', 8, 'bold', 0),
                                                    anchor=tk.CENTER)
