# Zeiterfassung
# UI
from definitions import *

import tkinter as tk
from time import strftime
from timestamping import *
from users import determine_filename
import locale

locale.setlocale(locale.LC_ALL, '')

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

def ui_stempeln(line_index, user):
    append_timestamp(determine_filename(user))

    global buttons
    global in_time_labels
    global out_time_labels

    last_timestamps = get_last_2_timestmaps(determine_filename(user))

    if stempel_zustand(determine_filename(user)) == status_out:
        buttons[line_index].configure(relief="raised", bg="red", text=user + "\n" + status_out)
        in_time_labels[line_index].configure(text=convert_timestamp(last_timestamps[0],"%H:%M"))
        out_time_labels[line_index].configure(text=convert_timestamp(last_timestamps[-1], "%H:%M"))
    elif stempel_zustand(determine_filename(user)) == status_in:
        buttons[line_index].configure(relief="sunken", bg="green", text=user + "\n" + status_in)
        in_time_labels[line_index].configure(text=convert_timestamp(last_timestamps[-1], "%H:%M"))
        out_time_labels[line_index].configure(text="")
    else:
        buttons[line_index].configure(bg="yellow", text="Fehler")

def win_stempeln(userlist):
    stempeln = tk.Tk()
    stempeln.title(program_name + " " + program_version)
    #stempeln.geometry("600x400")
    stempeln.minsize(width=200, height=200)

    global buttons
    global in_time_labels
    global out_time_labels
    global digital_clock

    buttons = [ ]
    in_time_labels = [ ]
    out_time_labels = [ ]

    button_index = 0

    digital_clock = tk.Label(stempeln)
    digital_clock.grid(column=1, row=0)

    update_time()

    frame_stempeln = tk.Frame(stempeln, borderwidth=5, relief="ridge", padx=10, pady=10)
    frame_stempeln.grid(row=1, column=1)

    tk.Label(frame_stempeln, text="Benutzer:", anchor="w", width=10).grid(row=0, column=1, sticky="w")
    tk.Label(frame_stempeln, text="Gekommen:").grid(row=0, column=2)
    tk.Label(frame_stempeln, text="Gegangen:").grid(row=0, column=3)

    #Schleife zur Erzeugung von Stempelzeilen für User
    for i in userlist:
        button = tk.Button(frame_stempeln, height=3, compound="left", command=lambda b=button_index, user=i: ui_stempeln(b, user))
        in_time = tk.Label(frame_stempeln, padx=10)
        out_time = tk.Label(frame_stempeln, padx=10)
        if len_timestamps(determine_filename(i)) % 2 == 0:
            button.configure(relief="raised", bg ="red", text=i + "\n" + status_out)
        else:
            button.configure(relief="sunken", bg="green", text=i + "\n" + status_in)
        button.grid(row=button_index+1, column=1, sticky="ew")
        in_time.grid(row=button_index+1, column=2)
        out_time.grid(row=button_index+1, column=3)
        buttons.append(button)
        in_time_labels.append(in_time)
        out_time_labels.append(out_time)
        button_index+=1

    stempeln.mainloop()

win_stempeln( ["testuser", "testuser2"])