from AppliDef import *


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

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

        self.BtnFind = BtnAction(self.CanvasFrmFond, int(self.SX * 0.956), int(self.SY * 0.514), self.RatioX,
                                 self.RatioY2, 'Media/FrmFind/BtnFind/')
        self.BtnFind.CanvasBtn.bind('<ButtonRelease-1>', self.Find)

        self.DataFind = tk.StringVar()
        self.DataFind.set('')

        EntryWidth = int(self.SX * float(ConfigResolution(self.MainAppli.Nconfig, 'FD1')))
        SizeFont = int(ConfigResolution(self.MainAppli.Nconfig, 'FD2'))

        self.EntryFind = tk.Entry(self.CanvasFrmFond, width=EntryWidth, font=FontLabel(SizeFont),
                                  textvariable=self.DataFind)
        self.CanvasFrmFond.create_window(int(self.SX * 0.008), int(self.SY * 0.12), anchor=tk.NW,
                                         window=self.EntryFind)
        self.EntryFind.focus_set()
        self.EntryFind.bind('<Return>', self.Find)

        self.CanvasFrmFond.pack()

    def Find(self, _):
        self.BtnFind.CanvasBtn.itemconfig(self.BtnFind.ImgCanvasBtn, image=self.BtnFind.ImgBtn[0])
        self.MainAppli.ListBoxFichiers.delete(0, tk.END)
        self.MainAppli.RequeteRecherche = self.DataFind.get()
        self.MainAppli.ActionRecherche()

