#================================================================
#================================================================
# API-CNT(WDM)
# Counter Sample
# main UI file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


#========================================
# CounterUI class
#========================================
class CounterUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Counter Sample')
        self.root.minsize(530,400)
        #----------------------------------------
        # Set the font for controls
        #----------------------------------------
        self.root.option_add("*Font", ("MS Gothic", 9))
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Initialization process
        #----------------------------------------
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.root, text='Device Name : ', anchor='nw').place(x=5, y=5, width=95, height=20)
        #--------------------
        # Create Entry for Device Name
        #--------------------
        self.textbox_device_name_var= tk.StringVar(self.root)
        self.textbox_device_name_var.set("CNT000")
        tk.Entry(self.root, textvariable=self.textbox_device_name_var).place(x=100, y=5, width=100, height=20)
        #--------------------
        # Create Label for Channel0 to Channel7
        #--------------------
        tk.Label(self.root, text='Channel0', anchor='nw').place(x=5, y=100, width=50, height=20)
        tk.Label(self.root, text='Channel1', anchor='nw').place(x=5, y=125, width=50, height=20)
        tk.Label(self.root, text='Channel2', anchor='nw').place(x=5, y=150, width=50, height=20)
        tk.Label(self.root, text='Channel3', anchor='nw').place(x=5, y=175, width=50, height=20)
        tk.Label(self.root, text='Channel4', anchor='nw').place(x=5, y=200, width=50, height=20)
        tk.Label(self.root, text='Channel5', anchor='nw').place(x=5, y=225, width=50, height=20)
        tk.Label(self.root, text='Channel6', anchor='nw').place(x=5, y=250, width=50, height=20)
        tk.Label(self.root, text='Channel7', anchor='nw').place(x=5, y=275, width=50, height=20)
        #----------------------------------------
        # Create LabelFrame for Count value
        #----------------------------------------
        self.groupbox_counter_value = tk.LabelFrame(self.root, text="Count value")
        self.groupbox_counter_value.place(x=65, y=30, width=130, height=280)
        #--------------------
        # Create Radiobutton for Count value
        #--------------------
        self.radiobutton_counter_value_base_var = tk.IntVar()
        self.radiobutton_counter_value_base_var.set(1)
        self.radiobutton_count_value_dec = tk.Radiobutton(self.groupbox_counter_value, text="Dec", variable=self.radiobutton_counter_value_base_var, value=1)
        self.radiobutton_count_value_dec.place(x=10, y=0, width=60, height=20)
        self.radiobutton_count_value_hex = tk.Radiobutton(self.groupbox_counter_value, text="Hex", variable=self.radiobutton_counter_value_base_var, value=2)
        self.radiobutton_count_value_hex.place(x=10, y=20, width=60, height=20)
        #--------------------
        # Create Entry for Channel0 to Channel7 Count value
        #--------------------
        self.textbox_ch0_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch0_value_var.set('')
        self.textbox_ch0_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch0_value_var, justify=tk.RIGHT)
        self.textbox_ch0_value.place(x=10, y=55, width=105, height=20)
        self.textbox_ch1_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch1_value_var.set('')
        self.textbox_ch1_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch1_value_var, justify=tk.RIGHT)
        self.textbox_ch1_value.place(x=10, y=80, width=105, height=20)
        self.textbox_ch2_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch2_value_var.set('')
        self.textbox_ch2_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch2_value_var, justify=tk.RIGHT)
        self.textbox_ch2_value.place(x=10, y=105, width=105, height=20)
        self.textbox_ch3_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch3_value_var.set('')
        self.textbox_ch3_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch3_value_var, justify=tk.RIGHT)
        self.textbox_ch3_value.place(x=10, y=130, width=105, height=20)
        self.textbox_ch4_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch4_value_var.set('')
        self.textbox_ch4_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch4_value_var, justify=tk.RIGHT)
        self.textbox_ch4_value.place(x=10, y=155, width=105, height=20)
        self.textbox_ch5_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch5_value_var.set('')
        self.textbox_ch5_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch5_value_var, justify=tk.RIGHT)
        self.textbox_ch5_value.place(x=10, y=180, width=105, height=20)
        self.textbox_ch6_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch6_value_var.set('')
        self.textbox_ch6_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch6_value_var, justify=tk.RIGHT)
        self.textbox_ch6_value.place(x=10, y=205, width=105, height=20)
        self.textbox_ch7_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch7_value_var.set('')
        self.textbox_ch7_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch7_value_var, justify=tk.RIGHT)
        self.textbox_ch7_value.place(x=10, y=230, width=105, height=20)
        #----------------------------------------
        # Create LabelFrame for Status
        #----------------------------------------
        self.groupbox_counter_status = tk.LabelFrame(self.root, text="status")
        self.groupbox_counter_status.place(x=200, y=30, width=90, height=280)
        #--------------------
        # Create Entry for Channel0 to Channel7 Count status
        #--------------------
        self.textbox_ch0_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch0_status_var.set('')
        self.textbox_ch0_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch0_status_var, justify=tk.RIGHT)
        self.textbox_ch0_status.place(x=10, y=55, width=65, height=20)
        self.textbox_ch1_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch1_status_var.set('')
        self.textbox_ch1_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch1_status_var, justify=tk.RIGHT)
        self.textbox_ch1_status.place(x=10, y=80, width=65, height=20)
        self.textbox_ch2_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch2_status_var.set('')
        self.textbox_ch2_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch2_status_var, justify=tk.RIGHT)
        self.textbox_ch2_status.place(x=10, y=105, width=65, height=20)
        self.textbox_ch3_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch3_status_var.set('')
        self.textbox_ch3_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch3_status_var, justify=tk.RIGHT)
        self.textbox_ch3_status.place(x=10, y=130, width=65, height=20)
        self.textbox_ch4_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch4_status_var.set('')
        self.textbox_ch4_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch4_status_var, justify=tk.RIGHT)
        self.textbox_ch4_status.place(x=10, y=155, width=65, height=20)
        self.textbox_ch5_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch5_status_var.set('')
        self.textbox_ch5_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch5_status_var, justify=tk.RIGHT)
        self.textbox_ch5_status.place(x=10, y=180, width=65, height=20)
        self.textbox_ch6_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch6_status_var.set('')
        self.textbox_ch6_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch6_status_var, justify=tk.RIGHT)
        self.textbox_ch6_status.place(x=10, y=205, width=65, height=20)
        self.textbox_ch7_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch7_status_var.set('')
        self.textbox_ch7_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch7_status_var, justify=tk.RIGHT)
        self.textbox_ch7_status.place(x=10, y=230, width=65, height=20)
        #----------------------------------------
        # Create LabelFrame for Preset value
        #----------------------------------------
        self.groupbox_preset_value = tk.LabelFrame(self.root, text="Preset value")
        self.groupbox_preset_value.place(x=295, y=30, width=100, height=280)
        #--------------------
        # Create Radiobutton for Preset value
        #--------------------
        self.radiobutton_preset_value_base_var = tk.IntVar()
        self.radiobutton_preset_value_base_var.set(1)
        self.radiobutton_preset_value_dec = tk.Radiobutton(self.groupbox_preset_value, text="Dec", variable=self.radiobutton_preset_value_base_var, value=1)
        self.radiobutton_preset_value_dec.place(x=10, y=0, width=60, height=20)
        self.radiobutton_preset_value_hex = tk.Radiobutton(self.groupbox_preset_value, text="Hex", variable=self.radiobutton_preset_value_base_var, value=2)
        self.radiobutton_preset_value_hex.place(x=10, y=20, width=60, height=20)
        #--------------------
        # Create Entry for Channel0 to Channel7 Preset value
        #--------------------
        self.textbox_ch0_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch0_preset_value_var.set('0')
        self.textbox_ch0_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch0_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch0_preset_value.place(x=10, y=55, width=75, height=20)
        self.textbox_ch1_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch1_preset_value_var.set('100')
        self.textbox_ch1_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch1_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch1_preset_value.place(x=10, y=80, width=75, height=20)
        self.textbox_ch2_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch2_preset_value_var.set('200')
        self.textbox_ch2_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch2_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch2_preset_value.place(x=10, y=105, width=75, height=20)
        self.textbox_ch3_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch3_preset_value_var.set('300')
        self.textbox_ch3_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch3_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch3_preset_value.place(x=10, y=130, width=75, height=20)
        self.textbox_ch4_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch4_preset_value_var.set('400')
        self.textbox_ch4_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch4_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch4_preset_value.place(x=10, y=155, width=75, height=20)
        self.textbox_ch5_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch5_preset_value_var.set('500')
        self.textbox_ch5_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch5_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch5_preset_value.place(x=10, y=180, width=75, height=20)
        self.textbox_ch6_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch6_preset_value_var.set('600')
        self.textbox_ch6_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch6_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch6_preset_value.place(x=10, y=205, width=75, height=20)
        self.textbox_ch7_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch7_preset_value_var.set('700')
        self.textbox_ch7_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch7_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch7_preset_value.place(x=10, y=230, width=75, height=20)
        #----------------------------------------
        # Comparison result
        #----------------------------------------
        #--------------------
        # Create Label for Comparison result
        #--------------------
        tk.Label(self.root, text='Comparison result : ', anchor='nw').place(x=5, y=330, width=140, height=20)
        #--------------------
        # Create Entry for Comparison result
        #--------------------
        self.textbox_compare_result_string_var= tk.StringVar(self.root)
        self.textbox_compare_result_string_var.set("")
        tk.Entry(self.root, textvariable=self.textbox_compare_result_string_var).place(x=130, y=330, width=265, height=20)
        #----------------------------------------
        # Return value
        #----------------------------------------
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=5, y=360, width=140, height=20)
        #--------------------
        # Create Entry for Return Code
        #--------------------
        self.textbox_return_string_var= tk.StringVar(self.root)
        self.textbox_return_string_var.set("")
        tk.Entry(self.root, textvariable=self.textbox_return_string_var).place(x=130, y=360, width=265, height=20)
        #----------------------------------------
        # Button
        #----------------------------------------
        #--------------------
        # Create Button for CntInit
        #--------------------
        self.button_cnt_init = tk.Button(self.root, text='CntInit')
        self.button_cnt_init.place(x=405, y=15, width=110, height=20)
        #--------------------
        # Create Button for Mode Set
        #--------------------
        self.button_cnt_mode = tk.Button(self.root, text='Mode Set...')
        self.button_cnt_mode.place(x=405, y=60, width=110, height=20)
        #--------------------
        # Create Button for Comparison Set
        #--------------------
        self.button_cnt_compare = tk.Button(self.root, text='Comparison Set...')
        self.button_cnt_compare.place(x=405, y=95, width=110, height=20)
        #--------------------
        # Create Button for CntStartCount
        #--------------------
        self.button_cnt_start = tk.Button(self.root, text='CntStartCount')
        self.button_cnt_start.place(x=405, y=150, width=110, height=20)
        #--------------------
        # Create Button for CntPreset
        #--------------------
        self.button_cnt_preset = tk.Button(self.root, text='CntPreset')
        self.button_cnt_preset.place(x=405, y=185, width=110, height=20)
        #--------------------
        # Create Button for CntStopCount
        #--------------------
        self.button_cnt_stop = tk.Button(self.root, text='CntStopCount')
        self.button_cnt_stop.place(x=405, y=220, width=110, height=20)
        #--------------------
        # Create Button for CntExit
        #--------------------
        self.button_cnt_exit = tk.Button(self.root, text='CntExit')
        self.button_cnt_exit.place(x=405, y=290, width=110, height=20)
        #--------------------
        # Create Button for Exit
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=405, y=350, width=110, height=20)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_count_value_dec,
                       command_count_value_hex,
                       command_preset_value_dec,
                       command_preset_value_hex,
                       command_cnt_init,
                       command_cnt_mode,
                       command_cnt_compare,
                       command_cnt_start,
                       command_cnt_preset,
                       command_cnt_stop,
                       command_cnt_exit,
                       command_exit):
        #----------------------------------------
        # Set the callback function for the [Dec] radiobutton
        #----------------------------------------
        self.radiobutton_count_value_dec.configure(command=command_count_value_dec)
        #----------------------------------------
        # Set the callback function for the [Hex] radiobutton
        #----------------------------------------
        self.radiobutton_count_value_hex.configure(command=command_count_value_hex)
        #----------------------------------------
        # Set the callback function for the [Dec] radiobutton
        #----------------------------------------
        self.radiobutton_preset_value_dec.configure(command=command_preset_value_dec)
        #----------------------------------------
        # Set the callback function for the [Hex] radiobutton
        #----------------------------------------
        self.radiobutton_preset_value_hex.configure(command=command_preset_value_hex)
        #----------------------------------------
        # Set the callback function for the [CntInit] button
        #----------------------------------------
        self.button_cnt_init.configure(command=command_cnt_init)
        #----------------------------------------
        # Set the callback function for the [Mode Set...] button
        #----------------------------------------
        self.button_cnt_mode.configure(command=command_cnt_mode)
        #----------------------------------------
        # Set the callback function for the [Comparison Set...] button
        #----------------------------------------
        self.button_cnt_compare.configure(command=command_cnt_compare)
        #----------------------------------------
        # Set the callback function for the [CntStartCount] button
        #----------------------------------------
        self.button_cnt_start.configure(command=command_cnt_start)
        #----------------------------------------
        # Set the callback function for the [CntPreset] button
        #----------------------------------------
        self.button_cnt_preset.configure(command=command_cnt_preset)
        #----------------------------------------
        # Set the callback function for the [CntStopCount] button
        #----------------------------------------
        self.button_cnt_stop.configure(command=command_cnt_stop)
        #----------------------------------------
        # Set the callback function for the [CntExit] button
        #----------------------------------------
        self.button_cnt_exit.configure(command=command_cnt_exit)
        #----------------------------------------
        # Set the callback function for the [Exit] button
        #----------------------------------------
        self.button_exit.configure(command=command_exit)
 
    #========================================
    # Display the error in a message box
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.root)

    #========================================
    # Run the main event loop
    #========================================
    def run(self):
        self.root.mainloop()