from AppliDef import *


class FrmFour(tk.Toplevel):
    def __init__(self, container, ListFours, RatioX, RatioY, Appli):
        tk.Toplevel.__init__(self, container)
        self.MainAppli = Appli
        self.parent = container
        self.RatioX = RatioX
        self.RatioY = RatioY
        self.ListFours = ListFours
        self.t = None
        self.window = None
        self.ImgCaptureWeb = None

        screen = ctypes.windll.user32
        FrmFond = Image.open('Media/FrmFour/Fond/Fond.jpg')
        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/FrmFour/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.174)) + "+" +
                      str(int(screen.GetSystemMetrics(1) * 0.08)))
        self.resizable(width=False, height=False)
        self.overrideredirect(True)
        self.lift()

        self.MainAppli.ListBoxProduits.selection_clear(0, tk.END)
        self.MainAppli.IndexElt = 0
        self.MainAppli.ListBoxProduits.selection_set(self.MainAppli.IndexElt)
        self.MainAppli.ListBoxProduits.see(self.MainAppli.IndexElt)

        self.EnTeteTP = self.CanvasFrmFond.create_text(int(self.SX * 0.01), int(self.SY * 0.015), text='',
                                                       font=AppliFont('Calibri', self.MainAppli.SizeFont + 2,
                                                                      'bold', 0), anchor=tk.W)
        self.CanvasFrmFond.addtag_withtag('EnTeteWeb', self.EnTeteTP)

        if len(ListFours) > 0:
            self.AffichCaptureWeb(0)

        self.CanvasFrmFond.bind('<Button-1>', self.ClicOnCanvas)
        self.CanvasFrmFond.focus_set()
        self.CanvasFrmFond.pack()

    def AffichCaptureWeb(self, Four):
        if self.ImgCaptureWeb is not None:
            self.ImgCaptureWeb.CanvasImg.destroy()
        if self.ListFours[Four][9] != '' and self.ListFours[Four][9] is not None \
                and os.path.exists('Datas/Fournisseurs/Captures/' + self.ListFours[Four][9]):
            if self.ListFours[Four][8] != '' and self.ListFours[Four][8] is not None:
                self.CanvasFrmFond.itemconfigure('EnTeteWeb', text=self.ListFours[Four][8])
            else:
                self.CanvasFrmFond.itemconfigure('EnTeteWeb', text=self.ListFours[Four][0])
            self.ImgCaptureWeb = ImgInCanvas(self.CanvasFrmFond, 'Datas/Fournisseurs/Captures/' +
                                             self.ListFours[Four][9], 0.98, tk.CENTER,
                                             int(self.SX * 0.5), int(self.SY * 0.5))

        elif self.ListFours[Four][8] != '' and self.ListFours[Four][8] is not None:
            self.CanvasFrmFond.itemconfigure('EnTeteWeb', text=self.ListFours[Four][8])
            self.ImgCaptureWeb = ImgInCanvas(self.CanvasFrmFond, 'Datas/Fournisseurs/Captures/NoCapture', 0.5,
                                             tk.CENTER, int(self.SX * 0.5), int(self.SY * 0.5))

        else:
            self.CanvasFrmFond.itemconfigure('EnTeteWeb', text=self.ListFours[Four][0])
            self.ImgCaptureWeb = ImgInCanvas(self.CanvasFrmFond, 'Datas/Fournisseurs/Captures/NoWeb', 0.49, tk.CENTER,
                                             int(self.SX * 0.5), int(self.SY * 0.5))

        ChangeCursor(self.ImgCaptureWeb.CanvasImg)
        self.ImgCaptureWeb.CanvasImg.bind('<Button-1>', self.AffichWeb)

    def SelectFour(self, j):
        self.MainAppli.ListBoxProduits.selection_clear(0, tk.END)
        self.MainAppli.ListBoxProduits.selection_set(j)
        self.MainAppli.ListBoxProduits.see(j)
        self.AffichCaptureWeb(j)

        if not self.MainAppli.Init:
            self.MainAppli.InfoFour.Four = self.ListFours[j]
            self.MainAppli.InfoFour.AffichInfoFour()
            self.MainAppli.InfoFour.lift()
        self.lift()

    def AffichWeb(self, _):
        if self.ListFours[self.MainAppli.IndexElt][8] != '' and self.ListFours[self.MainAppli.IndexElt][8] is not None:
            Adresse = self.ListFours[self.MainAppli.IndexElt][8]
        else:
            Adresse = 'https://www.google.fr/search?q=' + self.ListFours[self.MainAppli.IndexElt][0]
        webbrowser.open(Adresse, new=1)

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


class FrmInfosFour(tk.Toplevel):
    def __init__(self, container, Four, RatioX, RatioY, Appli):
        tk.Toplevel.__init__(self, container)
        self.MainAppli = Appli
        self.parent = container
        self.RatioX = RatioX
        self.RatioY = RatioY
        self.Four = Four
        self.ImgFournisseur = None
        self.CanvasDescript = None

        screen = ctypes.windll.user32
        FrmFond = Image.open('Media/FrmInfo/Fond/Fond.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/FrmInfo/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.753)) + "+" +
                      str(int(screen.GetSystemMetrics(1) * 0.11)))
        self.resizable(width=False, height=False)
        self.overrideredirect(True)
        self.lift()

        self.CanvasMenu = CanvasInWindow('Media/FrmInfo/FondMenu/', self.RatioX, self.RatioY,
                                         self.CanvasFrmFond, tk.CENTER, int(self.SX * 0.5), int(self.SY * 0.52), 0)
        self.MainAppli.CanvasFrmMain.itemconfigure('LegendePanneauD', text="Informations sur le fournisseur")

        if Four:
            self.AffichInfoFour()
        self.CanvasFrmFond.focus_set()
        self.CanvasFrmFond.pack()

    def AffichInfoFour(self):
        self.EffaceMenu()
        self.CanvasDescript = CanvasInWindow('Media/FrmInfo/FondDescriptMat/', self.RatioX * 1.55, self.RatioY * 1.7,
                                             self.CanvasMenu.CanvasListIma, tk.CENTER, int(self.CanvasMenu.wsize * 0.5),
                                             int(self.CanvasMenu.hsize * 0.48), 0)

        if self.Four[7] is not None and self.Four[7] != '' \
                and os.path.exists('Datas/Fournisseurs/Logos/' + self.Four[7]):
            PathLogo = 'Datas/Fournisseurs/Logos/' + self.Four[7]
        else:
            PathLogo = 'Datas/Fournisseurs/Logos/VIDE'
        if self.ImgFournisseur is not None:
            self.ImgFournisseur.CanvasImg.destroy()
        self.ImgFournisseur = ImgInCanvas(self.CanvasDescript.CanvasListIma, PathLogo, 0.5, tk.CENTER,
                                          int(int(self.CanvasDescript.CanvasListIma['width']) * 0.5),
                                          int(int(self.CanvasDescript.CanvasListIma['height']) * 0.18))

        if self.Four[7] is None or self.Four[7] == '' or not os.path.exists('Datas/Fournisseurs/Logos/' + self.Four[7]):
            if len(self.Four[0]) < 15:
                TextFournisseur = self.Four[0]
            else:
                TextFournisseur = self.Four[0].split(' ')[0]
                for i in range(1, len(self.Four[0].split(' '))):
                    TextFournisseur = TextFournisseur + '\n' + self.Four[0].split(' ')[i]
            self.ImgFournisseur.CanvasImg.create_text(int(int(self.ImgFournisseur.CanvasImg['width']) * 0.5),
                                                      int(int(self.ImgFournisseur.CanvasImg['height']) * 0.5),
                                                      text=TextFournisseur,
                                                      font=AppliFont('Helvetica', self.MainAppli.SizeFont + 4, 'bold',
                                                                     0), anchor=tk.CENTER, justify=tk.CENTER)

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.08), int(self.CanvasDescript.hsize * 0.35), text='Contact:',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 1), anchor=tk.W)

        c = 0
        for i in [0, 12, 1, 2, 3, 4, 10, 11]:
            Text = ''
            if i != 4 and i != 10 and i != 11:
                if self.Four[i] is not None:
                    Text = self.Four[i]
            elif i == 4:
                if self.Four[i] is not None:
                    Text = 'Tel: ' + self.Four[i]
            elif i == 10:
                if self.Four[i] is not None:
                    Text = 'Fax: ' + self.Four[i]
            elif i == 11:
                if self.Four[i] is not None:
                    Text = 'Email: ' + self.Four[i]

            if Text != '':
                LabelInCanvas(self.CanvasDescript.CanvasListIma, Text, '#FFFAF4',
                              AppliFont('Helvetica', self.MainAppli.SizeFont + 1, 'normal', 0),
                              int(self.CanvasDescript.wsize * 0.1),
                              int(self.CanvasDescript.hsize * 0.4 + c * self.CanvasDescript.hsize * 0.032), tk.W)
                c += 1

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.08), int(self.CanvasDescript.hsize * 0.68), text='Commandes:',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 1), anchor=tk.W)

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.1), int(self.CanvasDescript.hsize * 0.73), text='Frais de port (€ HT):',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'normal', 0), anchor=tk.W)
        if self.Four[5] is not None:
            LabelInCanvas(self.CanvasDescript.CanvasListIma, self.Four[5], '#FFFAF4',
                          AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 0),
                          int(self.CanvasDescript.wsize * 0.55),
                          int(self.CanvasDescript.hsize * 0.73), tk.W)

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.1), int(self.CanvasDescript.hsize * 0.77),
            text='Limite Franco de port (€ HT):',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'normal', 0), anchor=tk.W)
        if self.Four[6] is not None:
            LabelInCanvas(self.CanvasDescript.CanvasListIma, self.Four[6], '#FFFAF4',
                          AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 0),
                          int(self.CanvasDescript.wsize * 0.55),
                          int(self.CanvasDescript.hsize * 0.77), tk.W)

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.08), int(self.CanvasDescript.hsize * 0.88), text='Catégorie:',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 1), anchor=tk.W)
        if self.Four[13] is not None:
            LabelInCanvas(self.CanvasDescript.CanvasListIma, self.Four[13], '#FFFAF4',
                          AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'normal', 0),
                          int(self.CanvasDescript.wsize * 0.1),
                          int(self.CanvasDescript.hsize * 0.93), tk.W)

    def EffaceMenu(self):
        for widget in self.CanvasMenu.CanvasListIma.winfo_children():
            widget.destroy()
