# Zeiterfassung
# UI Definitionen

import tkinter as tk
import locale
locale.setlocale(locale.LC_ALL, '')

from time import strftime
from definitions import app_title, app_version, status_in
from users import user as uo
from users import list_users

# Pinpad

class win_pinpad(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)

        def update_time():
            string_time = strftime('%A, der %d.%m.%Y - %H:%M:%S')
            nonlocal digital_clock
            digital_clock.config(text=string_time)
            digital_clock.after(1000, update_time)

        self.title(app_title + " " + app_version)

        # Digital clock label configuration
        digital_clock = tk.Label(self)
        digital_clock.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
        # Initial call to update_time function
        update_time()

        # Benutzernummer
        def usernr_changed(UserNr):
            nonlocal usernr
            if len(str(usernr.get())) > 0:
                buttons["OK"].configure(state="active")
            else:
                buttons["OK"].configure(state="disabled")


        tk.Label(self, text="Benutzernummer:").grid(row=1, column=0)
        UserNr = tk.StringVar()
        UserNr.trace("w", lambda name, index, mode, UserNr=UserNr: usernr_changed(UserNr))
        usernr = tk.Entry(self, width=10, textvariable=UserNr)
        usernr.grid(row=1,column=1)

       # Pinpad

        def buttonPress(key):

            nonlocal usernr
            if type(key) is int:
                if key < 10:
                    usernr.insert('end', str(key))
            if key =="OK":
              print("OK pressed")
            if key == "<-":
                usernr.delete(usernr.index("end") - 1 )
            if len(usernr.get()) > 0:
                buttons["OK"].configure(state="active")
            else:
                buttons["OK"].configure(state="disabled")

        # Buttons definieren
        button_width = 7
        button_height = 3
        pinframe = tk.Frame(self)
        pinframe.grid(row=2, column=0, columnspan=3, padx=10, pady=10)
        buttons = { }

        keys = [
            [ 1, 2, 3],
            [ 4, 5, 6],
            [ 7, 8, 9],
            [ "<-", 0, "OK"]
        ]

        for y, row in enumerate(keys, 1):
            for x, key in enumerate(row):
                button = tk.Button(pinframe, width=button_width, height=button_height, text=key, command=lambda key=key: buttonPress(key))
                button.grid(row=y, column=x)
                buttons[key] = button

        buttons["OK"].configure(state="disabled")

        usernr.focus_set()

class win_userlist(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)

        def update_time():
            string_time = strftime('%A, der %d.%m.%Y - %H:%M:%S')
            nonlocal digital_clock
            digital_clock.config(text=string_time)
            digital_clock.after(1000, update_time)

        self.title(app_title + " " + app_version)

        # Digital clock label configuration
        digital_clock = tk.Label(self)
        digital_clock.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
        # Initial call to update_time function
        update_time()

        tk.Label(self, text="Benutzer auswählen").grid(row=1, column=0, columnspan=2)

        # Button Frame
        button_frame = tk.Frame(self)
        button_frame.grid(row=2, column=0, columnspan=2, padx=0, pady=10)
        userlist = list_users()


        # Button Dictionary
        buttons = { }
        button_row_index = 0

        for name in userlist:
            button = tk.Button(button_frame, text=name)
            button.grid(row=button_row_index, column=0, pady=5, sticky="ew")
            buttons[name] = button
            button_row_index = button_row_index + 1

class win_stamping(tk.Toplevel):
    def __init__(self, parent, user):
        super().__init__(parent)
        def update_time():
            string_time = strftime('%A, der %d.%m.%Y - %H:%M:%S')
            nonlocal digital_clock
            digital_clock.config(text=string_time)
            digital_clock.after(1000, update_time)

        self.title(app_title + " " + app_version)

        # Benutzer feststellen

        current_user = uo(user)

        # Digital clock label configuration
        digital_clock = tk.Label(self)
        digital_clock.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
        # Initial call to update_time function
        update_time()

        # Benutzer anzeigen
        tk.Label(self, text=current_user.fullname).grid(row=1, column=0, pady=10, columnspan=3)

        todays_hours = tk.Label(self, text="Arbeitsstunden erscheinen hier")
        todays_hours.grid(row=2, column=0, pady=10, columnspan=3)

        in_button = tk.Button(self, text="Einstempeln", bg="green")
        out_button = tk.Button(self, text="Ausstempeln", bg="red")

        if current_user.stamp_status() == status_in:
            in_button.configure(state="disabled")
            out_button.configure(state="active")
            out_button.focus_set()
        else:
            in_button.configure(state="active")
            out_button.configure(state="disabled")
            in_button.focus_set()
        in_button.grid(row=3, column = 0)
        out_button.grid(row=3, column=2)

        button_frame = tk.Frame(self, relief="groove")
        button_frame.grid(row=4, column=0, columnspan=3, pady=10)

        overview_workinghours = tk.Button(button_frame, text="Übersicht Arbeitszeiten")
        overview_missed = tk.Button(button_frame, text="Übersicht Fehlzeiten")
        overview_data = tk.Button(button_frame, text="Stammdaten")

        overview_workinghours.grid(row=0, column=0, sticky="ew")
        overview_missed.grid(row=1, column=0, sticky="ew")
        overview_data.grid(row=2, column=0, sticky="ew")

        button_close = tk.Button(self, text="Schließen")
        button_close.grid(row=5, column=1)

#========================================================

class mainwindow(tk.Tk):
    def __init__(self):
        super().__init__()

        self.geometry('300x200')
        self.title('Main Window')

        # place a button on the root window
        tk.Button(self,
                   text='PinPad Window',
                   command=self.open_pinpad).pack(expand=True)
        tk.Button(self,
                  text='Userlist Window',
                  command=self.open_userlist).pack(expand=True)
        tk.Button(self,
                  text='Stamping Window',
                  command=self.open_stamping).pack(expand=True)

    def open_pinpad(self):
        window = win_pinpad(self)
        window.grab_set()

    def open_userlist(self):
        window = win_userlist(self)
        window.grab_set()

    def open_stamping(self):
        window = win_stamping(self, user="testuser")
        window.grab_set()


if __name__ == "__main__":
    app = mainwindow()
    app.mainloop()


