import hid
import struct
import time
import tkinter as tk

DEFAULT_HID_TIMEOUT = 10

def wait_for_response(hmd_device: hid.Device, message_types:list, timeout_ms: int = 1000) -> bytes:
    start_time = time.monotonic_ns()
    
    while( (start_time + (timeout_ms*1000000)) > time.monotonic_ns()):
        bytesout = hmd_device.read(65, timeout=DEFAULT_HID_TIMEOUT)
        if(len(bytesout) > 0):
            if(bytesout[0] in message_types):
                return bytesout
    
    return b''

def get_timer(dev, timernum):
    dev.send_feature_report(bytes([0, ord('Z'), timernum]))
    response = wait_for_response(dev, [ord('Z')], timeout_ms = 100)
    if(len(response) == 0 or response[0] == ord('E')):
        timer_val = -1
    else:
        timer_val = struct.unpack('<I',response[1:5])[0]
    return timer_val

def set_timer(dev, timernum, new_val):
    new_val_bytes = bytes(struct.pack('<I', new_val))
    dev.send_feature_report(bytes([0, ord('z'), timernum]) + new_val_bytes)

def get_timer_values(intvars_list):
    bigs=hid.Device(vid=0x35bd,pid=0x0101)
    intvars_list[0].set(10*get_timer(bigs,0))
    intvars_list[1].set(10*get_timer(bigs,1))
    intvars_list[2].set(10*get_timer(bigs,3))
    intvars_list[3].set(10*get_timer(bigs,2))

def reset_power_timer():
    bigs=hid.Device(vid=0x35bd,pid=0x0101)
    set_timer(bigs, 0, 0)

def reset_left():
    bigs=hid.Device(vid=0x35bd,pid=0x0101)
    set_timer(bigs, 1, 0)

def reset_right():
    bigs=hid.Device(vid=0x35bd,pid=0x0101)
    set_timer(bigs, 3, 0)

def reset_longest_session():
    bigs=hid.Device(vid=0x35bd,pid=0x0101)
    set_timer(bigs, 2, 0)

def reset_all_timers():
    reset_power_timer()
    reset_left()
    reset_right()
    reset_longest_session()

def main():
    root = tk.Tk()

    total_on = tk.IntVar(root, 0)
    left_on = tk.IntVar(root, 0)
    right_on = tk.IntVar(root, 0)
    longest_session = tk.IntVar(root, 0)

    root.geometry("450x200")
    root.title("Force an Error")

    frame1 = tk.Frame(root,relief=tk.SUNKEN,borderwidth=5)
    frame2 = tk.Frame(root,relief=tk.SUNKEN,borderwidth=5)

    btn1 = tk.Button(frame1, text="Get current timer values (minutes)", command=lambda: get_timer_values([total_on,left_on,right_on,longest_session]))
    lbl1 = tk.Label(frame1, text="Total power on:")
    lbl2 = tk.Label(frame1, text="Left display on:")
    lbl3 = tk.Label(frame1, text="Right display on:")
    lbl4 = tk.Label(frame1, text="Longest session:")
    disp1 = tk.Label(frame1, textvariable=total_on)
    disp2 = tk.Label(frame1, textvariable=left_on)
    disp3 = tk.Label(frame1, textvariable=right_on)
    disp4 = tk.Label(frame1, textvariable=longest_session)

    btn2 = tk.Button(frame2, text="Reset all", command=reset_all_timers)
    btn3 = tk.Button(frame2, text="Reset total power on time", command=reset_power_timer)
    btn4 = tk.Button(frame2, text="Reset left display on time", command = reset_left)
    btn5 = tk.Button(frame2, text="Reset right display on time", command = reset_right)
    btn6 = tk.Button(frame2, text="Reset longest continuous session time", command = reset_longest_session)

    frame1.grid(column=0, row=0,stick='news')
    frame2.grid(column=1,row=0,sticky='news')
    root.columnconfigure(0,weight=1)
    root.columnconfigure(1,weight=1)
    btn1.grid(column=0,row=0,columnspan=2)
    lbl1.grid(column=0,row=1)
    lbl2.grid(column=0,row=2)
    lbl3.grid(column=0,row=3)
    lbl4.grid(column=0,row=4)
    disp1.grid(column=1, row=1)
    disp2.grid(column=1, row=2)
    disp3.grid(column=1, row=3)
    disp4.grid(column=1, row=4)
    btn2.pack()
    btn3.pack()
    btn4.pack()
    btn5.pack()
    btn6.pack()

    root.mainloop()

if __name__ == '__main__':
    main()