from AppliDef import *


class FrmInfosDoc(tk.Toplevel):
    def __init__(self, container, Doc, RatioX, RatioY, Appli):
        tk.Toplevel.__init__(self, container)
        self.MainAppli = Appli
        self.parent = container
        self.RatioX = RatioX
        self.RatioY = RatioY
        self.Doc = Doc
        self.CanvasDescript = None
        self.NomDoc1 = None
        self.NomDoc2 = None
        self.NomDoc = None
        self.ZoneText = 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 document")

        if Doc:
            self.AffichInfoDoc()
        self.CanvasFrmFond.focus_set()
        self.CanvasFrmFond.pack()

    def AffichInfoDoc(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 len(self.Doc[0]) >= 55:
            self.NomDoc = None
            (Nom1, Nom2) = (self.Doc[0][:len(self.Doc[0]) // 2], self.Doc[0][len(self.Doc[0]) // 2:])
            self.NomDoc1 = self.CanvasDescript.CanvasListIma.create_text(
                int(self.CanvasDescript.wsize * 0.5), int(self.CanvasDescript.hsize * 0.1), text=Nom1 + '-',
                font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 1), anchor=tk.CENTER)
            self.NomDoc2 = self.CanvasDescript.CanvasListIma.create_text(
                int(self.CanvasDescript.wsize * 0.5), int(self.CanvasDescript.hsize * 0.13), text=Nom2,
                font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 1), anchor=tk.CENTER)
        else:
            self.NomDoc1 = None
            self.NomDoc2 = None
            self.NomDoc = self.CanvasDescript.CanvasListIma.create_text(
                int(self.CanvasDescript.wsize * 0.5), int(self.CanvasDescript.hsize * 0.1), text=self.Doc[0],
                font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 1), anchor=tk.CENTER)

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.05), int(self.CanvasDescript.hsize * 0.18), text='Catégorie:',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 1), anchor=tk.W)

        if self.Doc[4] is not None and self.Doc[4] != '':
            self.CanvasDescript.CanvasListIma.create_text(
                int(self.CanvasDescript.wsize * 0.05), int(self.CanvasDescript.hsize * 0.23), text=self.Doc[4],
                font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 0), anchor=tk.W)

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.05), int(self.CanvasDescript.hsize * 0.3), text='Résumé:',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 1), anchor=tk.W)

        self.ZoneText = ZoneScrolledText(self.CanvasDescript.CanvasListIma, width=int(self.CanvasDescript.wsize * 0.12),
                                         height=int(self.CanvasDescript.hsize * 0.03),
                                         font=AppliFont('Helvetica', self.MainAppli.SizeFont, 'normal', 0))

        self.CanvasDescript.CanvasListIma.create_window(int(self.CanvasDescript.wsize * 0.025),
                                                        int(self.CanvasDescript.hsize * 0.335), anchor=tk.NW,
                                                        window=self.ZoneText)
        if self.Doc[2] is not None and self.Doc[2] != '':
            File = ''
            if self.MainAppli.BaseEnCours == "CHIMIE":
                if os.path.exists('Datas/DescriptDocChim/' + self.Doc[2] + '.txt'):
                    File = open('Datas/DescriptDocChim/' + self.Doc[2] + '.txt', "r", encoding="utf-8")
            else:
                if os.path.exists('Datas/DescriptDocPhys/' + self.Doc[2] + '.txt'):
                    File = open('Datas/DescriptDocPhys/' + self.Doc[2] + '.txt', "r", encoding="utf-8")
            for ligne in File:
                self.ZoneText.insert(tk.INSERT, ligne)

        self.ZoneText.configure(state='disabled')

        self.CanvasDescript.CanvasListIma.create_text(
            int(self.CanvasDescript.wsize * 0.05), int(self.CanvasDescript.hsize * 0.88), text='Mots clés:',
            font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 1), anchor=tk.W)

        if self.Doc[3] is not None and self.Doc[3] != '':
            if len(self.Doc[3]) < 53:
                TextMotsCles = self.Doc[3]
            else:
                TextMotsCles = self.Doc[3][0:52] + '-\n' + self.Doc[3][52:len(self.Doc[3])]
            self.CanvasDescript.CanvasListIma.create_text(
                int(self.CanvasDescript.wsize * 0.05), int(self.CanvasDescript.hsize * 0.93), text=TextMotsCles,
                font=AppliFont('Helvetica', self.MainAppli.SizeFont + 2, 'bold', 0), anchor=tk.W)

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