from nicegui import ui

import calendar

from datetime import date

from datetime import datetime

from datetime import timedelta

from caldav import DAVClient, Event

import asyncio

caldav_url = "https://cal.lemonhall.me/dav.php"

username = "lemonhall"

password = "xxxxxxx"

client = DAVClient(url=caldav_url,username=username,password=password)

ui.label('欢迎来到 NiceGUI!这个hotreloding还不错的样子')

ui.button('按下这里',on_click=lambda:ui.notify('按下按键'))

def print_calendars_demo(calendars):

    """

    This example prints the name and URL for every calendar on the list

    """

    if calendars:

        ## Some calendar servers will include all calendars you have

        ## access to in this list, and not only the calendars owned by

        ## this principal.

        print("your principal has %i calendars:" % len(calendars))

        for c in calendars:

            print("    Name: %-36s  URL: %s" % (c.name, c.url))

    else:

        print("your principal has no calendars")

## Typically the next step is to fetch a principal object.

## This will cause communication with the server.

my_principal = client.principal()

## The principals calendars can be fetched like this:

calendars = my_principal.calendars()

## print out some information

print_calendars_demo(calendars)

my_new_calendar = my_principal.calendar(name="linode默认日历")

my_year = 2023

my_month = 11

month_array=calendar.monthcalendar(my_year,my_month)

print(month_array)

async def fetche_events(my_year, my_month, one_day):

    one_day_events_fetched = my_new_calendar.search(

        start=datetime(my_year, my_month, one_day, 8),

        end=datetime(my_year, my_month, one_day, 19),

        event=True,

        expand=False,

    )

    return one_day_events_fetched

async def handle_update_data(morning_button,afternoon_button,my_year,my_month,one_day):

    one_day_events_fetched = await fetche_events(my_year,my_month,one_day)

    morning_events_fetched_text = one_day_events_fetched[0].vobject_instance.vevent.summary.value

    afternoon_events_fetched_text = one_day_events_fetched[1].vobject_instance.vevent.summary.value

    morning_button.value=morning_events_fetched_text

    afternoon_button.value=afternoon_events_fetched_text

    morning_button.update()

    afternoon_button.update()

with ui.grid(columns=8):

    title_style = "w-32 h-5 border "

    cell_style = "w-32 h-32 border"

    title_color = 'red-500'

    cells_morning =[]

    cells_afternoon =[]

    for one_week in month_array:

        days = ['星期','周一','周二','周三','周四','周五','周六','周日']

        for day_title in days:

            ui.button(day_title,color=title_color,on_click=lambda:ui.notify(day_title)).classes(title_style)

        with ui.column():

            # 这实际上是每一周的表头

            ui.button("日期",color=title_color,on_click=lambda:ui.notify("")).classes(title_style)

            ui.button("上午",color=title_color,on_click=lambda:ui.notify("上午")).classes(cell_style)

            ui.button("下午",color=title_color,on_click=lambda:ui.notify("下午")).classes(cell_style)

        for one_day in one_week:

            morning_events_fetched =""

            afternoon_events_fetched =""

            morning_events_fetched_text =""

            afternoon_events_fetched_text =""

            one_day_events_fetched =""

            with ui.column():

                # 这里才是实际上的内容部分

                if one_day != 0:

                    date_text = datetime(my_year,my_month,one_day).strftime("%m月%d日")

                    # one_day_events_fetched = my_new_calendar.search(

                    #     start=datetime(my_year, my_month, one_day, 8),

                    #     end=datetime(my_year, my_month, one_day, 19),

                    #     event=True,

                    #     expand=False,

                    # )

                    # morning_events_fetched_text = one_day_events_fetched[0].vobject_instance.vevent.summary.value

                    # afternoon_events_fetched_text = one_day_events_fetched[1].vobject_instance.vevent.summary.value

                    ui.button(date_text,color=title_color,on_click=lambda:ui.notify(one_day)).classes(title_style)

                    #ui.textarea(label='Text', placeholder='start typing',on_change=lambda e: result.set_text('you typed: ' + e.value))

                    tmp1=ui.textarea(label='').classes(cell_style).style('font-size: 70%;')

                    tmp2=ui.textarea(label='').classes(cell_style).style('font-size: 70%;')

                    asyncio.run(handle_update_data(tmp1,tmp2,my_year,my_month,one_day))

                else:

                    date_text = ""

                    ui.button(date_text,color=title_color,on_click=lambda:ui.notify(one_day)).classes(title_style)

                    ui.textarea(label='').classes(cell_style).style('font-size: 70%;')

                    ui.textarea(label='').classes(cell_style).style('font-size: 70%;')

ui.run()