from datetime import datetime

from nicegui import ui, app

from users import *
from definitions import *
from web_ui import *
from calendar import monthrange

import hashlib
import calendar
import locale

@ui.page('/touchscreen')
def page_touchscreen():

    def button_click(name):
        nonlocal buttons
        current_user = user(name)
        current_user.timestamp()
        if current_user.stamp_status() == status_in:
            buttons[name].props('color=green')
            ui.notify(status_in)
        else:
            buttons[name].props('color=red')
            ui.notify(status_out)

    pageheader("Bitte User auswählen:")

    userlist = list_users()
    number_of_users = len(userlist)
    buttons = { }

    if number_of_users > 5:
        number_of_columns = 5
    else:
        number_of_columns = number_of_users


    with ui.grid(columns=number_of_columns):
        for name in userlist:
            current_user = user(name)
            current_button = ui.button(on_click=lambda name=name: button_click(name))
            with current_button:
                try:
                    with open(current_user.photofile, 'r') as file:
                        pass
                    file.close()
                    ui.image(current_user.photofile)
                except:
                    pass
                ui.label(current_user.fullname)
            if current_user.stamp_status() == status_in:
                current_button.props('color=green')
            else:
                current_button.props('color=red')
            buttons[name] = current_button