from datetime import datetime

from nicegui import ui, app

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

import hashlib
import calendar
import locale

locale.setlocale(locale.LC_ALL, '')

class pageheader:
    def __init__(self, heading):
        self.heading = heading

        ui.markdown(f"##{app_title} {app_version}")
        ui.markdown(f"###{self.heading}")

class ValueBinder:
    def __init__(self):
        self.value = ""

def cookie_hash(user, password):
    return hashlib.sha256(b"{user}{app.storage.user['id']}{password}").hexdigest()

def load_adminsettings():
    # Settingsdatei einlesen
    try:
        with open(f"{scriptpath}/{usersettingsfilename}") as json_file:
            data = json.load(json_file)
        json_file.close()
        return(data)
    except:
        return(-1)

def convert_seconds_to_hours(seconds):
    if seconds < 0:
        sign = "-"
        seconds = seconds * (-1)
    else:
        sign = ""
    hours = seconds // 3600
    remaining_seconds = seconds - hours * 3600
    minutes = remaining_seconds // 60
    remaining_seconds = remaining_seconds - minutes * 60
    if remaining_seconds > 0 and sign != "-":
        minutes = minutes + 1
    if hours < 10:
        hours = "0" + str(hours)
    else:
        hours = str(hours)
    if minutes < 10:
        minutes = "0" + str(minutes)
    else:
        minutes = str(minutes)

    if sign == "-":
        return(f"-{hours}:{minutes}")
    else:
        return(f"{hours}:{minutes}")

@ui.page('/userlist')
def page_userlist():

    def click_button(button):
        ui.notify(button)

    ui.markdown(f"#{app_title} {app_version}")

    userlist = list_users()
    buttons = { }

    for name in userlist:
        button = ui.button(text=name, on_click=lambda name=name:click_button(name) )
        buttons[name] = button


