from AppliDef import *


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

        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()

        self.BtnFind = BtnAction(self.CanvasFrmFond, int(self.SX * 0.956), int(self.SY * 0.514), self.RatioX,
                                 self.RatioY * 0.9, '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, 'LEntryFindData',
                                                          self.MainAppli.curSave)))
        SizeFont = int(ConfigResolution(self.MainAppli.Nconfig, 'SizeFontFindData', self.MainAppli.curSave))

        self.EntryFind = tk.Entry(self.CanvasFrmFond, width=EntryWidth,
                                  font=AppliFont('Helvetica', SizeFont, 'normal', 0), 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.ListBoxProduits.delete(0, tk.END)
        self.MainAppli.RequeteRecherche = self.DataFind.get()
        self.MainAppli.ActionRecherche()


def ModifDanger(N):
    danger = ''
    if N == 0:
        danger = 'AttentionGHS'
    if N == 1:
        danger = 'CancerGHS'
    if N == 2:
        danger = 'CGHS'
    if N == 3:
        danger = 'ComburantGHS'
    if N == 4:
        danger = 'DangerGHS'
    if N == 5:
        danger = 'ExplosifGHS'
    if N == 6:
        danger = 'FGHS'
    if N == 7:
        danger = 'GazGHS'
    if N == 8:
        danger = 'PolluantGHS'
    if N == 9:
        danger = 'TGHS'
    return danger


class FrmPicto(tk.Toplevel):
    def __init__(self, container, RatioX, RatioY, AppliMain):
        tk.Toplevel.__init__(self, container)
        self.MainAppli = AppliMain
        self.parent = container
        self.RatioX = RatioX
        self.RatioY = RatioY
        self.ListProdCat = []

        screen = ctypes.windll.user32
        FrmFond = Image.open('Media/FrmPicto/Fond/Fond.bmp')
        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/FrmPicto/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.142)))
        self.resizable(width=False, height=False)
        self.overrideredirect(True)
        self.lift()

        self.ListDanger = ['Attention', 'Danger pour \nla santé', 'Corrosif', 'Comburant', 'Danger', 'Explosif',
                           'Inflammable', 'Gaz sous pression', "Nocif pour \nl'environnement", 'Toxique']
        self.CanvasGHS = []
        self.LabelGHS = []
        l = 0
        c = 0
        for i in range(10):
            self.CanvasGHS.append(CanvasInWindow('Media/PictoDanger/', self.RatioX, self.RatioX, self.CanvasFrmFond,
                                                 tk.CENTER, int(self.SX * 0.25 + c * self.SX * 0.48),
                                                 int(self.SY * 0.08 + l * self.SY * 0.18), i))
            self.LabelGHS.append(LabelInCanvas(self.CanvasFrmFond, self.ListDanger[i], '#E0E0E0',
                                               AppliFont('Helvetica', self.MainAppli.SizeFont, 'normal', 0),
                                               int(self.SX * 0.25 + c * self.SX * 0.48),
                                               int(self.SY * 0.15 + l * self.SY * 0.18), tk.CENTER))
            c = c + 1
            if c > 1:
                c = 0
                l = l + 1

            ChangeCursor(self.CanvasGHS[i].CanvasListIma)
            ChangeCursor(self.LabelGHS[i].Label)

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

            self.CanvasGHS[i].CanvasListIma.bind('<ButtonRelease-1>', GestEvt)
            self.LabelGHS[i].Label.bind('<ButtonRelease-1>', GestEvt)

        self.CanvasFrmFond.focus_set()
        self.CanvasFrmFond.pack()

    def SelectDanger(self, j):
        self.MainAppli.DangerSelected = ModifDanger(j)
        self.ListProdCat = []
        for i in range(1,4):
            self.MainAppli.cur.execute('SELECT ' + self.MainAppli.ListChampsTabProd +
                                       " FROM PRODUITS WHERE " + 'GHS' + str(i) + "='" + self.MainAppli.DangerSelected +
                                       "' order by NOMS")
            self.ListProdCat.extend(self.MainAppli.cur.fetchall())
        self.MainAppli.NAction = 1022
        self.MainAppli.ChargeListProduits(self.ListProdCat)
        self.MainAppli.EtagProduits.destroy()
        self.MainAppli.AffichEtagProduits(self.ListProdCat)
        self.MainAppli.RequeteRecherche = 'Danger "' + self.LabelGHS[j].Label['text'].replace('\n','') + '":'
        self.MainAppli.CanvasFrmMain.itemconfig(self.MainAppli.LegendActionV, text=self.MainAppli.RequeteRecherche)
        self.MainAppli.ListProdCat = self.ListProdCat
        self.MainAppli.MAJInfo()
        self.destroy()
        self.MainAppli.FrmLift()
