from cursesmenu import *
from cursesmenu.items import *
import curses
import argparse as ap
from FlippR_Networking import Networking
import signal
import os

networking = "notset"

def create_menu():
    menu = CursesMenu("FlippR")

    solenoids = networking.getSolenoids()["solenoids"]
    sol_menu = create_submenu("Solenoids", solenoids, networking.triggerSolenoid)
    sol_menu_item = SubmenuItem("Solenoids", sol_menu, menu)
    menu.append_item(sol_menu_item)

    sounds = networking.getSounds()["sounds"]
    sound_menu = create_submenu("Sounds", sounds, networking.playSound)
    sound_menu_item = SubmenuItem("Sounds", sound_menu, menu)
    menu.append_item(sound_menu_item)

#actually not supported by server
#    displays = networking.getDisplays()
#    display_menu = create_displays_submenu(displays)
#    display_menu_item = SubmenuItem("Displays", display_menu, menu)
#    menu.append_item(display_menu_item)

    lamps = networking.getLamps()["lamps"]
    lamp_menu = create_lamps_submenu(lamps)
    lamp_menu_item = SubmenuItem("Lamps", lamp_menu, menu)
    menu.append_item(lamp_menu_item)

    input_item = FunctionItem("Print Input", print_input)
    menu.append_item(input_item)
    
    #win = curses.newwin(20,20,20,20)
    menu.show()
    
#quit = False
#def sigint_handler(quit):
 #   quit = True

def print_input():
    print("Starting to print incoming events:")
    # end_loop_handler = signal.signal(signal.SIGINT, sigint_handler)
    while(True):
        print(networking.getInputEvent())
    #  signal.signal(signal.SIGINT, end_loop_handler)

def create_lamps_submenu(lamps):
    menu = CursesMenu("Output", "Lamps")
    for lamp in lamps:
        lamp_submenu = create_lamp_submenu(lamp)
        sub_item = SubmenuItem(lamp['name'], lamp_submenu, menu)
        menu.append_item(sub_item)
    return menu

def create_lamp_submenu(lamp):
    args = list()
    args.append(lamp['name'])
    menu = CursesMenu("Lamps", lamp['name'])
    activate_item = FunctionItem("Activate", networking.activateLamp, args)
    deactivate_item = FunctionItem("Deactivate", networking.deactivateLamp, args)
    status_item = FunctionItem("Status", print_lamp_status, args)
    menu.append_item(activate_item)
    menu.append_item(deactivate_item)
    menu.append_item(status_item)
    return menu

def print_lamp_status(lamp):
    status = networking.lampStatus(lamp)
    print(status)

def create_displays_submenu(displays):
    menu = CursesMenu("Output", "Displays")
    for display in displays:
        args = list()
        args.append(display["name"])
        fun_item = FunctionItem(display["name"], write_display_score, args)
        menu.append_item(fun_item)
    return menu

def write_display_score(display):
    score = input("Score: ")
    networking.writeDisplayScore(display, score)

def create_submenu(title, items, fun):
    menu = CursesMenu("Output", title)
    for item in items:
        args = list()
        args.append(item["name"])
        fun_item = FunctionItem(item["name"], fun, args)
        menu.append_item(fun_item)
    return menu

def main():
    argparser = ap.ArgumentParser()
    global networking
    if os.environ["XDG_RUNTIME_DIR"]:
        path = os.environ["XDG_RUNTIME_DIR"]
    else:
        path = "/tmp"
    in_path = path + "/S.flippR_driver.in"
    out_path = path + "/S.flippR_driver.out"
    print("Input path: " + in_path + " output path " + out_path )
    networking = Networking(out_path, in_path)
    print(networking)
    create_menu()

main()
