import tkinter as tk
import tkinter.filedialog
import argparse
import DGSDK
import os

VAR_MODELNAME = None
VAR_FILENAME = None

def browseFile():
    global VAR_FILENAME
    assert(VAR_FILENAME is not None)

    file = tk.filedialog.askopenfile()
    if(file == None):
        return
    VAR_FILENAME.set(file.name)
    DGSDK.dg64LoadFile(file.name)

def main(args):
    global VAR_FILENAME, VAR_MODELNAME

    # Set dll
    DGSDK.dg64SetDLL(args.dll)

    root = tk.Tk()
    root.title("DGSDK GUI")
    root.iconbitmap(os.path.join(os.getcwd(), "res/DG_VC_SDK.ico"))
    # Stay Fixed Size Window
    root.minsize(416, 112)
    root.maxsize(416, 112)
    root.resizable(False, False)

    VAR_MODELNAME = tk.StringVar()
    VAR_FILENAME = tk.StringVar()


    # Model Name Row
    labelModel = tk.Label(root,  text="Model Name:", anchor="e")
    entryModel = tk.Entry(root, textvariable=VAR_MODELNAME)

    labelModel.grid(column=0, row=0, sticky="e", padx=4, pady=4)
    entryModel.grid(column=1, row=0, sticky="ew", padx=4, pady=4)

    # FileName Row
    labelFile = tk.Label(root, text="File Name:", anchor="e")
    entryFile = tk.Entry(root, textvariable=VAR_FILENAME)
    buttonFileBrowse = tk.Button(root, text="Browse", width=12, command=browseFile)

    labelFile.grid(column=0, row=1, sticky="e", padx=4, pady=4)
    entryFile.grid(column=1, row=1, sticky="ew", padx=4, pady=4)
    buttonFileBrowse.grid(column=2, row=1, padx=4, pady=4)

    # Button Row
    buttonRow = tk.Frame(root)
    buttonGetHardware = tk.Button(buttonRow, text="Get Hardware", width=12, state="active")
    buttonStart = tk.Button(buttonRow, text="Start", width=12, command=DGSDK.dg64Start, state="disabled")
    buttonStop = tk.Button(buttonRow, text="Stop", width=12, command=DGSDK.dg64Stop, state="disabled")
    buttonShutdown = tk.Button(buttonRow, text="Shutdown", width=12, state="disabled")

    def onButtonGetHardwarePressed(event):
        buttonGetHardware['state'] = tk.DISABLED
        buttonStart['state'] = tk.ACTIVE
        buttonStop['state'] = tk.ACTIVE
        buttonShutdown['state'] = tk.ACTIVE 
        DGSDK.dg64GetHardware()
        VAR_MODELNAME.set(DGSDK.ModelInfo[DGSDK.dwModel])

    def onButtonShutdownPressed(event):
        buttonGetHardware['state'] = tk.ACTIVE
        buttonStart['state'] = tk.DISABLED
        buttonStop['state'] = tk.DISABLED
        buttonShutdown['state'] = tk.DISABLED
        DGSDK.dg64Shutdown()

    buttonGetHardware.bind("<Button-1>", onButtonGetHardwarePressed)
    buttonShutdown.bind("<Button-1>", onButtonShutdownPressed)
    
    buttonGetHardware.grid(row=0, column=0, padx=4, pady=4, sticky="news")
    buttonStart.grid(row=0, column=1, padx=4, pady=4, sticky="news")
    buttonStop.grid(row=0, column=2, padx=4, pady=4, sticky="news")
    buttonShutdown.grid(row=0, column=3, padx=4, pady=4, sticky="news")

    buttonRow.grid(row=2, column=0, columnspan=3, padx=4, pady=4, sticky="news")

    root.mainloop()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-dll', '-d', default=os.path.join(os.getcwd(), "DgRun64.dll"),
                        help='DGRun DLL File Location')  
    main(parser.parse_args())
