from AppliDef import *


class FrmDescriptSketch(tk.Toplevel):
    def __init__(self, container, Titre, RatioX, RatioY, IndexFile, Appli):
        tk.Toplevel.__init__(self, container)
        self.MainAppli = Appli
        self.IndexFile = IndexFile

        screen = ctypes.windll.user32

        FrmFond = Image.open('Media/CanvasDetails/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/CanvasDetails/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.7535)) + "+" +
                      str(int(screen.GetSystemMetrics(1) * 0.11)))
        self.resizable(width=False, height=False)
        self.overrideredirect(True)

        self.LabelTitre = LabelText(self.CanvasFrmFond, Titre, '#F7F7F7', font=FontLabelTitre(14))
        self.CanvasFrmFond.create_window(int(self.SX / 2), int(self.SY * 0.03), anchor=tk.CENTER,
                                         window=self.LabelTitre)

        self.ImgCircuit = []
        WidthPhotoCircuits = self.SX * 0.9
        PhotoCircuit = Image.open(self.MainAppli.ListCircuitSketchs[self.IndexFile])
        HeightPhotoCircuits = WidthPhotoCircuits * float(PhotoCircuit.size[1]) / float(PhotoCircuit.size[0])
        RatioImgPhotoCircuitsX = WidthPhotoCircuits / float(PhotoCircuit.size[0])
        RatioImgPhotoCircuitsY = HeightPhotoCircuits / float(PhotoCircuit.size[1])
        wsize = int((float(PhotoCircuit.size[0]) * float(RatioImgPhotoCircuitsX)))
        hsize = int((float(PhotoCircuit.size[1]) * float(RatioImgPhotoCircuitsY)))
        PhotoCircuit = PhotoCircuit.resize((wsize, hsize), Image.ANTIALIAS)
        self.ImgCircuit.append(ImageTk.PhotoImage(PhotoCircuit))

        self.CanvasImgCircuit = CanvasApp(self.CanvasFrmFond, width=wsize, height=hsize)
        self.CanvasFrmFond.create_window(int(self.SX / 2), int(self.SY * 0.22), anchor=tk.CENTER,
                                         window=self.CanvasImgCircuit)
        self.CanvasImgCircuit.create_image(0, 0, anchor=tk.NW, image=self.ImgCircuit[0])

        self.LabelComp = LabelText(self.CanvasFrmFond, 'Liste des composants:', '#F7F7F7', font=FontLabelTitre(12))
        self.CanvasFrmFond.create_window(int(self.SX * 0.03), int(self.SY * 0.39), anchor=tk.NW,
                                         window=self.LabelComp)

        self.ZoneTextComp = ZoneScrolledText(self.CanvasFrmFond, width=int(self.SX * 0.12), height=int(self.SY * 0.008),
                                             font=FontText(10))
        self.CanvasFrmFond.create_window(int(self.SX * 0.005), int(self.SY * 0.44), anchor=tk.NW,
                                         window=self.ZoneTextComp)
        self.ZoneTextComp.bind('<Double-1>', self.MainAppli.ZoomDescript)
        ChangeCursor(self.ZoneTextComp)

        if self.MainAppli.ListSketchs[self.IndexFile][2] is not None:
            fichier = open(self.MainAppli.ListSketchs[self.IndexFile][2], 'r', encoding="utf-8")
            for ligne in fichier:
                self.ZoneTextComp.insert(tk.INSERT, ligne)

        self.LabelObjectif = LabelText(self.CanvasFrmFond, 'Objectifs:', '#F7F7F7', font=FontLabelTitre(12))
        self.CanvasFrmFond.create_window(int(self.SX * 0.03), int(self.SY * 0.61), anchor=tk.NW,
                                         window=self.LabelObjectif)

        self.ZoneTextObjectif = ZoneScrolledText(self.CanvasFrmFond, width=int(self.SX * 0.12),
                                                 height=int(self.SY * 0.019), font=FontText(10))
        self.CanvasFrmFond.create_window(int(self.SX * 0.005), int(self.SY * 0.66), anchor=tk.NW,
                                         window=self.ZoneTextObjectif)
        self.ZoneTextObjectif.bind('<Double-1>', self.MainAppli.ZoomDescript)
        ChangeCursor(self.ZoneTextObjectif)

        if self.MainAppli.ListSketchs[self.IndexFile][3] is not None:
            fichier = open(self.MainAppli.ListSketchs[self.IndexFile][3], 'r', encoding="utf-8")
            for ligne in fichier:
                self.ZoneTextObjectif.insert(tk.INSERT, ligne)

        self.ZoneTextObjectif.config(state='disabled')
        self.ZoneTextComp.config(state='disabled')
        self.CanvasFrmFond.bind('<Motion>', self.MoveOnCanvas)
        self.CanvasFrmFond.pack()

    def MoveOnCanvas(self,_):
        self.MainAppli.InitOnglets()


class FrmCodeSketch(tk.Toplevel):
    def __init__(self, container, Titre, RatioX, RatioY, IndexFile, Appli):
        tk.Toplevel.__init__(self, container)
        self.MainAppli = Appli
        self.IndexFile = IndexFile
        screen = ctypes.windll.user32

        FrmFond = Image.open('Media/CanvasDetails/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/CanvasDetails/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.7535)) + "+" +
                      str(int(screen.GetSystemMetrics(1) * 0.11)))
        self.resizable(width=False, height=False)
        self.overrideredirect(True)

        self.LabelTitre = LabelText(self.CanvasFrmFond, Titre, '#F7F7F7', font=FontLabelTitre(14))
        self.CanvasFrmFond.create_window(int(self.SX / 2), int(self.SY * 0.03), anchor=tk.CENTER,
                                         window=self.LabelTitre)

        self.ImgCircuit = []
        WidthPhotoCircuits = self.SX * 0.9
        PhotoCircuit = Image.open(self.MainAppli.ListCircuitSketchs[self.IndexFile])
        HeightPhotoCircuits = WidthPhotoCircuits * float(PhotoCircuit.size[1]) / float(PhotoCircuit.size[0])
        RatioImgPhotoCircuitsX = WidthPhotoCircuits / float(PhotoCircuit.size[0])
        RatioImgPhotoCircuitsY = HeightPhotoCircuits / float(PhotoCircuit.size[1])
        wsize = int((float(PhotoCircuit.size[0]) * float(RatioImgPhotoCircuitsX)))
        hsize = int((float(PhotoCircuit.size[1]) * float(RatioImgPhotoCircuitsY)))
        PhotoCircuit = PhotoCircuit.resize((wsize, hsize), Image.ANTIALIAS)
        self.ImgCircuit.append(ImageTk.PhotoImage(PhotoCircuit))

        self.CanvasImgCircuit = CanvasApp(self.CanvasFrmFond, width=wsize, height=hsize)
        self.CanvasFrmFond.create_window(int(self.SX / 2), int(self.SY * 0.22), anchor=tk.CENTER,
                                         window=self.CanvasImgCircuit)
        self.CanvasImgCircuit.create_image(0, 0, anchor=tk.NW, image=self.ImgCircuit[0])

        self.LabelCode = LabelText(self.CanvasFrmFond, 'Code:', '#F7F7F7', font=FontLabelTitre(12))
        self.CanvasFrmFond.create_window(int(self.SX * 0.03), int(self.SY * 0.39), anchor=tk.NW,
                                         window=self.LabelCode)

        self.ZoneTextCode = ZoneScrolledText(self.CanvasFrmFond, width=int(self.SX * 0.12), height=int(self.SY * 0.035),
                                             font=FontText2(10))
        self.CanvasFrmFond.create_window(int(self.SX * 0.005), int(self.SY * 0.44), anchor=tk.NW,
                                         window=self.ZoneTextCode)
        self.ZoneTextCode.bind('<Double-1>', self.MainAppli.ZoomCode)
        ChangeCursor(self.ZoneTextCode)

        if self.MainAppli.ListSketchs[self.IndexFile][4] is not None:
            fichier = open(self.MainAppli.ListSketchs[self.IndexFile][4], 'r', encoding="utf-8")
            for ligne in fichier:
                self.ZoneTextCode.insert(tk.INSERT, ligne)

        self.ZoneTextCode.config(state='disabled')
        self.CanvasFrmFond.bind('<Motion>', self.MoveOnCanvas)
        self.CanvasFrmFond.pack()

    def MoveOnCanvas(self,_):
        self.MainAppli.InitOnglets()


class FrmDocSketch(tk.Toplevel):
    def __init__(self, container, RatioX, RatioY, RatioY2, IndexFile, Appli):
        tk.Toplevel.__init__(self, container)
        self.MainAppli = Appli
        self.IndexFile = IndexFile
        screen = ctypes.windll.user32

        FrmFond = Image.open('Media/CanvasDetails/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/CanvasDetails/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.7535)) + "+" +
                      str(int(screen.GetSystemMetrics(1) * 0.11)))
        self.resizable(width=False, height=False)
        self.overrideredirect(True)

        if self.MainAppli.ListSketchs[self.IndexFile][5] is not None:
            self.FrmPDF = FrmPDF(self.CanvasFrmFond, self.MainAppli.ListSketchs[self.IndexFile][5], 1,
                                 int(self.SX * 0.5), int(self.SY / 2.1), int(self.SX * 0.96), int(self.SY * 0.95),
                                 RatioX, RatioY2, 0.46, 0.53)
            self.FrmPDF.CanvasDoc.bind('<Double-1>', self.MainAppli.ZoomDoc)
        else:
            self.NoPDF = NoPDF(self.CanvasFrmFond, int(self.SX / 2), int(self.SY / 2),
                               self.MainAppli.RatioImgPhotoCircuitsX, self.MainAppli.RatioImgPhotoCircuitsY)

        self.CanvasFrmFond.bind('<Motion>', self.MoveOnCanvas)
        self.CanvasFrmFond.pack()

    def MoveOnCanvas(self,_):
        self.MainAppli.InitOnglets()
