#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 4.26
#  in conjunction with Tcl version 8.6
#    Feb 04, 2020 04:48:04 PM CET  platform: Linux

import sys

# from tkinter import *
import PIL
from PIL import Image
from PIL.ImageTk import PhotoImage

from sorter.videohandler import VideoHandler

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk

    py3 = False
except ImportError:
    import tkinter.ttk as ttk

    py3 = True

import TinySorter1_support


def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    global vid
    vid = VideoHandler()
    root = tk.Tk()
    TinySorter1_support.set_Tk_var()
    top = TinySorter(root, vid)
    TinySorter1_support.init(root, top, vid)
    root.mainloop()


w = None


def create_TinySorter(root, *args, **kwargs):
    '''Starting point when module is imported by another program.'''
    global w, w_win, rt, vid

    rt = root
    w = tk.Toplevel(root)

    TinySorter1_support.set_Tk_var()
    top = TinySorter(w, vid)
    TinySorter1_support.init(w, top, vid, *args, **kwargs)
    return w, top


def destroy_TinySorter():
    global w
    w.destroy()
    w = None


# Alles was hier steht wirkt sich auf die GUI aus
class TinySorter:
    def __init__(self, top=None, vidsrc=None):
        self.vidsrc = vidsrc
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9'  # X11 color: 'gray85'
        _ana1color = '#d9d9d9'  # X11 color: 'gray85'
        _ana2color = '#ececec'  # Closest X11 color: 'gray92'
        intelcolor = '#0071C5'
        white = '#ffffff'
        font9 = "-family {DejaVu Sans} -size 10 -weight bold -slant " \
                "roman -underline 0 -overstrike 0"
        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.', background=_bgcolor)
        self.style.configure('.', foreground=_fgcolor)
        self.style.configure('.', font="TkDefaultFont")
        self.style.map('.', background=
        [('selected', _compcolor), ('active', _ana2color)])

        top.geometry("1311x718+310+228")
        top.minsize(1, 1)
        top.maxsize(1905, 1050)
        top.resizable(1, 1)
        top.title("TinySorter")
        top.configure(background="#0071C5")
        # top.configure(background="#ffffff")
        top.configure(highlightcolor="black")

        intellogo = PhotoImage(file="IntelLogo.png")
        hfulogo = PhotoImage(file="HFUlogo.png")
        gdlogo = PhotoImage(file="gdlogo.jpg")
        self.intellogo = tk.Label(top, image=intellogo)
        self.intellogo.image = intellogo
        self.intellogo.place(relx=0, rely=0.7, relwidth=0.25, relheight=0.3)
        self.intellogo.configure(background=white)
        self.hfulogo = tk.Label(top, image=hfulogo)
        self.hfulogo.image = hfulogo
        self.hfulogo.place(relx=0.25, rely=0.7, relwidth=0.25, relheight=0.3)
        self.hfulogo.configure(background=white)
        self.gdlogo = tk.Label(top, image=gdlogo)
        self.gdlogo.image = gdlogo
        self.gdlogo.place(relx=0.5, rely=0.7, relwidth=0.5, relheight=0.3)
        self.gdlogo.configure(background=white)

        self.Classes = tk.LabelFrame(top)
        # self.Classes.place(x=0, y=230, height=600, relwidth=0.25)
        self.Classes.place(relx=0, rely=0, relheight=0.55, relwidth=0.25)
        self.Classes.configure(relief='groove')
        self.Classes.configure(font="-family {DejaVu Sans} -size 11 -weight bold")
        self.Classes.configure(text='''Datensatz''')
        self.Classes.configure(background=white)

        self.Frame1 = tk.Frame(self.Classes)
        self.Frame1.place(relx=0.063, rely=0.1, relheight=0.2, relwidth=0.859
                          , bordermode='ignore')
        self.Frame1.configure(highlightbackground=intelcolor)
        self.Frame1.configure(highlightthickness=3)
        self.Frame1.configure(relief="groove")
        self.Frame1.configure(background=white)

        self.Frame2 = tk.Frame(self.Classes)
        self.Frame2.place(relx=0.063, rely=0.35, relheight=0.2, relwidth=0.859
                          , bordermode='ignore')
        self.Frame2.configure(highlightbackground=intelcolor)
        self.Frame2.configure(highlightthickness=3)
        self.Frame2.configure(relief="groove")
        self.Frame2.configure(background=white)

        self.Frame3 = tk.Frame(self.Classes)
        self.Frame3.place(relx=0.063, rely=0.6, relheight=0.2, relwidth=0.859
                          , bordermode='ignore')
        self.Frame3.configure(highlightbackground=intelcolor)
        self.Frame3.configure(highlightthickness=3)
        self.Frame3.configure(relief="groove")
        self.Frame3.configure(background=white)

        self.Shake = tk.Button(self.Classes)
        self.Shake.place(relx=0.063, rely=0.85, relheight=0.1, relwidth=0.40
                         , bordermode='ignore')
        self.Shake.configure(activebackground="#f9f9f9")
        self.Shake.configure(background=intelcolor)
        self.Shake.configure(fg="white")
        self.Shake.configure(font="-family {DejaVu Sans} -size 15 -weight bold")
        self.Shake.configure(text='''Schütteln''')
        self.Shake.bind('<Button-1>', lambda e: TinySorter1_support.shake(e))

        self.Reset = tk.Button(self.Classes)
        self.Reset.place(relx=0.55, rely=0.85, relheight=0.1, relwidth=0.40
                         , bordermode='ignore')
        self.Reset.configure(activebackground="#f9f9f9")
        self.Reset.configure(background=intelcolor)
        self.Reset.configure(fg="white")
        self.Reset.configure(font="-family {DejaVu Sans} -size 15 -weight bold")
        self.Reset.configure(text='''Zurücksetzen''')
        self.Reset.bind('<Button-1>', lambda e: TinySorter1_support.reset(e))

        self.TSeparator1_1 = ttk.Separator(self.Frame3)
        self.TSeparator1_1.place(relx=0.582, rely=0.0, relheight=1.067)
        self.TSeparator1_1.configure(orient="vertical")

        self.TSeparator1 = ttk.Separator(self.Frame1)
        self.TSeparator1.place(relx=0.589, rely=0.027, relheight=0.933)
        self.TSeparator1.configure(orient="vertical")

        self.TSeparator1_1 = ttk.Separator(self.Frame2)
        self.TSeparator1_1.place(relx=0.582, rely=0.0, relheight=1.067)
        self.TSeparator1_1.configure(orient="vertical")

        self.TakePicture1 = tk.Button(self.Frame1)
        self.TakePicture1.place(relx=0.04, rely=0.4, relheight=0.5, relwidth=0.5)
        self.TakePicture1.configure(activebackground="#f9f9f9")
        self.TakePicture1.configure(background=intelcolor)
        self.TakePicture1.configure(fg="white")
        self.TakePicture1.configure(text='''Bild aufnehmen''')
        self.TakePicture1.configure(font="-family {DejaVu Sans} -size 15 -weight bold")
        self.TakePicture1.bind('<Button-1>', lambda e: TinySorter1_support.takePic1(e))

        self.TakePicture2 = tk.Button(self.Frame2)
        self.TakePicture2.place(relx=0.04, rely=0.4, relheight=0.5, relwidth=0.5)
        self.TakePicture2.configure(activebackground="#f9f9f9")
        self.TakePicture2.configure(background=intelcolor)
        self.TakePicture2.configure(fg="white")
        self.TakePicture2.configure(text='''Bild aufnehmen''')
        self.TakePicture2.configure(font="-family {DejaVu Sans} -size 15 -weight bold")
        self.TakePicture2.bind('<Button-1>', lambda e: TinySorter1_support.takePic2(e))

        self.TakePicture3 = tk.Button(self.Frame3)
        self.TakePicture3.place(relx=0.04, rely=0.4, relheight=0.5, relwidth=0.5)
        self.TakePicture3.configure(activebackground="#f9f9f9")
        self.TakePicture3.configure(background=intelcolor)
        self.TakePicture3.configure(fg="white")
        self.TakePicture3.configure(text='''Bild aufnehmen''')
        self.TakePicture3.configure(font="-family {DejaVu Sans} -size 15 -weight bold")
        self.TakePicture3.bind('<Button-1>', lambda e: TinySorter1_support.takePic3(e))

        self.Class1 = tk.Text(self.Frame1)
        self.Class1.place(relx=0.04, rely=0.05, relheight=0.32, relwidth=0.5)
        self.Class1.configure(background=white)
        self.Class1.configure(borderwidth="2")
        self.Class1.configure(font="-family {DejaVu Sans} -size 14 -weight bold")
        self.Class1.configure(selectbackground="#c4c4c4")
        self.Class1.configure(wrap="word")
        # self.Class1.configure(textvariable=TinySorter1_support.class1_name)
        self.Class1.insert(tk.INSERT, "Klasse 1", "a")

        self.Class2 = tk.Text(self.Frame2)
        self.Class2.place(relx=0.04, rely=0.05, relheight=0.32, relwidth=0.5)
        self.Class2.configure(background=white)
        self.Class2.configure(borderwidth="2")
        self.Class2.configure(font="-family {DejaVu Sans} -size 14 -weight bold")
        self.Class2.configure(selectbackground="#c4c4c4")
        self.Class2.configure(wrap="word")
        # self.Class2.configure(textvariable=TinySorter1_support.class2_name)
        self.Class2.insert(tk.INSERT, "Klasse 2", "a")

        self.Class3 = tk.Label(self.Frame3)
        self.Class3.place(relx=0.04, rely=0.05, relheight=0.32, relwidth=0.5)
        self.Class3.configure(background=white)
        self.Class3.configure(borderwidth="2")
        self.Class3.configure(font="-family {DejaVu Sans} -size 14 -weight bold")
        self.Class3.configure(text="Leer")
        self.Class3.configure(anchor=tk.W)

        self.Counter1 = tk.Label(self.Frame1)
        self.Counter1.place(relx=0.691, rely=0.2, relheight=0.613
                            , relwidth=0.233)
        self.Counter1.configure(background="white")
        self.Counter1.configure(font="-family {DejaVu Sans} -size 20 -weight bold")
        self.Counter1.configure(fg=intelcolor)
        self.Counter1.configure(textvariable=TinySorter1_support.counter_class1)

        self.Counter2 = tk.Label(self.Frame2)
        self.Counter2.place(relx=0.691, rely=0.2, relheight=0.613
                            , relwidth=0.233)
        self.Counter2.configure(background="white")
        self.Counter2.configure(font="-family {DejaVu Sans} -size 20 -weight bold")
        self.Counter2.configure(fg=intelcolor)
        self.Counter2.configure(textvariable=TinySorter1_support.counter_class2)

        self.Counter3 = tk.Label(self.Frame3)
        self.Counter3.place(relx=0.691, rely=0.2, relheight=0.613
                            , relwidth=0.233)
        self.Counter3.configure(background="white")
        self.Counter3.configure(font="-family {DejaVu Sans} -size 20 -weight bold")
        self.Counter3.configure(fg=intelcolor)
        self.Counter3.configure(textvariable=TinySorter1_support.counter_class3)

        self.Training = tk.LabelFrame(top)
        self.Training.place(relx=0, rely=0.55, relheight=0.15
                            , relwidth=0.25)
        self.Training.configure(relief='groove')
        self.Training.configure(font="-family {DejaVu Sans} -size 11 -weight bold")
        self.Training.configure(text='''Training''')
        self.Training.configure(background="white")

        self.Klabel = tk.Label(self.Training)
        self.Klabel.place(relx=0.05, rely=0.35, bordermode='ignore')
        self.Klabel.configure(activebackground="#f9f9f9")
        self.Klabel.configure(background="white")
        self.Klabel.configure(font="-family {DejaVu Sans} -size 30 -weight bold")
        self.Klabel.configure(fg=intelcolor)
        self.Klabel.configure(text='''K = ''')

        self.Kvalue = tk.Label(self.Training)
        self.Kvalue.place(relx=0.25, rely=0.35, bordermode='ignore')
        self.Kvalue.configure(activebackground="#f9f9f9")
        self.Kvalue.configure(background="white")
        self.Kvalue.configure(font="-family {DejaVu Sans} -size 30 -weight bold")
        self.Kvalue.configure(fg=intelcolor)
        self.Kvalue.configure(textvariable=TinySorter1_support.k)

        self.KbtnUp = tk.Button(self.Training)
        self.KbtnUp.place(relx=0.4, rely=0.1, relheight=0.4, relwidth=0.15, bordermode='ignore')
        self.KbtnUp.configure(background=intelcolor)
        self.KbtnUp.configure(text="\u2bc5")
        self.KbtnUp.configure(font="-family {DejaVu Sans} -size 25 -weight bold")
        self.KbtnUp.configure(fg="white")
        self.KbtnUp.bind('<Button-1>', lambda e: TinySorter1_support.increase_k(e))

        self.KbtnDown = tk.Button(self.Training)
        self.KbtnDown.place(relx=0.4, rely=0.5, relheight=0.4, relwidth=0.15, bordermode='ignore')
        self.KbtnDown.configure(background=intelcolor)
        self.KbtnDown.configure(text="\u2bc6")
        self.KbtnDown.configure(font="-family {DejaVu Sans} -size 25 -weight bold")
        self.KbtnDown.configure(fg="white")
        self.KbtnDown.bind('<Button-1>', lambda e: TinySorter1_support.decrease_k(e))

        self.TrainModel = tk.Button(self.Training)
        self.TrainModel.place(relx=0.6, rely=0.1, relheight=0.8, relwidth=0.35
                              , bordermode='ignore')
        self.TrainModel.configure(activebackground="#f9f9f9")
        self.TrainModel.configure(background=intelcolor)
        self.TrainModel.configure(font="-family {DejaVu Sans} -size 20 -weight bold")
        self.TrainModel.configure(fg="white")
        self.TrainModel.configure(text='''lernen''')
        self.TrainModel.bind('<Button-1>', lambda e: TinySorter1_support.trainModel(e))

        self.Anleitung = tk.LabelFrame(top)
        self.Anleitung.place(relx=0.6, rely=0, relheight=0.67, relwidth=0.4)
        self.Anleitung.configure(relief='groove')
        self.Anleitung.configure(font="-family {DejaVu Sans} -size 11 -weight bold")
        self.Anleitung.configure(text='''Anleitung''')
        self.Anleitung.configure(background="white")

        self.AnleitungText = tk.Text(self.Anleitung, height=10)
        self.AnleitungText.place(relx=0.05, rely=0.1, relheight=0.65, relwidth=0.9)
        self.AnleitungText.configure(relief='groove')
        self.AnleitungText.configure(font="-family {DejaVu Sans} -size 18 -weight bold")
        self.AnleitungText.configure(fg=intelcolor)
        self.AnleitungText.configure(background="white")
        self.AnleitungText.insert("1.0", "1. Benenne die Klassen \n")
        self.AnleitungText.insert("2.0", "\n")
        self.AnleitungText.insert("3.0", "2. nehme Bilder der Jeweiligen Klassen auf \n")
        self.AnleitungText.insert("4.0", "\n")
        self.AnleitungText.insert("5.0", "3. wähle ein K \n")
        self.AnleitungText.insert("6.0", "\n")
        self.AnleitungText.insert("7.0", "4. lernen \n")
        self.AnleitungText.insert("8.0", "\n")
        self.AnleitungText.insert("9.0", "5. starte Sortierer \n")
        self.AnleitungText.insert("10.0", "\n")
        self.AnleitungText.insert("11.0", "6. stoppe Sortierer \n")
        self.AnleitungText.insert("12.0", "\n")
        self.AnleitungText.insert("13.0", "7. Wiederhole 2-6 mit mehr Bilder und anderem K \n")

        self.Cameraframe = tk.LabelFrame(top)
        self.Cameraframe.place(relx=0.25, rely=0, relheight=0.7, width=650)
        self.Cameraframe.configure(relief='groove')
        self.Cameraframe.configure(font="-family {DejaVu Sans} -size 11 -weight bold")
        self.Cameraframe.configure(text='''Kamera''')
        self.Cameraframe.configure(background="white")

        self.Infotext1 = tk.Label(self.Cameraframe)
        self.Infotext1.place(relx=0.01, rely=0.71, width=630, relheight=0.09)
        # self.Infotext1.configure(anchor=tk.W)
        self.Infotext1.configure(font="-family {DejaVu Sans} -size 30 -weight bold")
        self.Infotext1.configure(fg=intelcolor)
        self.Infotext1.configure(background="white")
        self.Infotext1.configure(textvariable=TinySorter1_support.info1)
        # self.Infotext1.configure(text="lernen...")

        self.Infotext2 = tk.Label(self.Cameraframe)
        self.Infotext2.place(relx=0.01, rely=0.8, width=630, relheight=0.09)
        # self.Infotext2.configure(anchor=tk.W)
        self.Infotext2.configure(font="-family {DejaVu Sans} -size 30 -weight bold")
        self.Infotext2.configure(fg=intelcolor)
        self.Infotext2.configure(background="white")
        self.Infotext2.configure(textvariable=TinySorter1_support.info2)
        # self.Infotext2.configure(text="Vorhersage: ")

        self.TSeparator3_6 = ttk.Separator(self.Cameraframe)
        self.TSeparator3_6.place(relx=-0, rely=0.9, relwidth=1
                                 , bordermode='ignore')

        self.Startsorting = tk.Button(self.Cameraframe)
        self.Startsorting.place(relx=0.01, rely=0.91, relheight=0.08, relwidth=0.48
                                , bordermode='ignore')
        self.Startsorting.configure(activebackground="#f9f9f9")
        self.Startsorting.configure(background=intelcolor)
        self.Startsorting.configure(fg="white")
        self.Startsorting.configure(text='''Starte Sortieren''')
        self.Startsorting.configure(font="-family {DejaVu Sans} -size 15 -weight bold")
        self.Startsorting.bind('<Button-1>', lambda e: TinySorter1_support.startSorting(e))

        self.Stopsorting = tk.Button(self.Cameraframe)
        self.Stopsorting.place(relx=0.51, rely=0.91, relheight=0.08, relwidth=0.48
                               , bordermode='ignore')
        self.Stopsorting.configure(activebackground="#f9f9f9")
        self.Stopsorting.configure(background=intelcolor)
        self.Stopsorting.configure(fg="white")
        self.Stopsorting.configure(text='''Stoppe Sortieren''')
        self.Stopsorting.configure(font="-family {DejaVu Sans} -size 15 -weight bold")
        self.Stopsorting.bind('<Button-1>', lambda e: TinySorter1_support.stopSorting(e))

        self.CamPic = tk.Canvas(self.Cameraframe, width=vidsrc.width, height=vidsrc.height)

        self.CamPic.place(relx=0, rely=0, height=vidsrc.height, width=vidsrc.width, bordermode='inside')
        # self.CamPic.place(relx=0.031, rely=0.063, relheight=0.733, relwidth=0.941, bordermode='ignore')
        self.CamPic.configure(background="#ffffff")
        self.CamPic.configure(borderwidth="2")
        self.CamPic.configure(relief="ridge")
        self.CamPic.configure(selectbackground="#c4c4c4")

        self.delay = 15
        self.update()

    # Funktion um die Kamerabilder zu aktualisieren
    def update(self):
        # Get a frame from the video source
        ret, frame = self.vidsrc.get_frame()

        if ret:
            self.photo = PIL.ImageTk.PhotoImage(
                image=PIL.Image.fromarray(frame))  # .resize((self.vid.width, self.vid.height), 0))
            # print(self.photo.height(), self.photo.width())
            self.CamPic.create_image(0, 0, image=self.photo, anchor=tk.NW)
            # self.CamPic.create_image(-120, -130, image=self.photo, anchor=tk.NW)

        self.CamPic.after(self.delay, self.update)


if __name__ == '__main__':
    vp_start_gui()
