#================================================================
#================================================================
# API-CNT(WDM) for Device with sampling function
# 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')
        self.root.minsize(420,300)
        #----------------------------------------
        # 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=10, 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 LabelFrame for Count value/status
        #----------------------------------------
        self.groupbox_counter_value_status = tk.LabelFrame(self.root, text="Count Value/Status : ")
        self.groupbox_counter_value_status.place(x=10, y=30, width=280, height=110)
        #--------------------
        # Create Label for CH0
        #--------------------
        tk.Label(self.groupbox_counter_value_status, text='CH0 : ', anchor='nw').place(x=10, y=5, width=50, height=20)
        #--------------------
        # Create Entry for CH0 Count value
        #--------------------
        self.textbox_ch0_value_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch0_value_var.set('')
        self.textbox_ch0_value = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch0_value_var, justify=tk.RIGHT)
        self.textbox_ch0_value.place(x=70, y=5, width=80, height=20)
        #--------------------
        # Create Entry for CH0 Count status
        #--------------------
        self.textbox_ch0_status_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch0_status_var.set('')
        self.textbox_ch0_status = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch0_status_var, justify=tk.RIGHT)
        self.textbox_ch0_status.place(x=170, y=5, width=80, height=20)
        #--------------------
        # Create Label for CH1
        #--------------------
        tk.Label(self.groupbox_counter_value_status, text='CH1 : ', anchor='nw').place(x=10, y=25, width=71, height=20)
        #--------------------
        # Create Entry for CH1 Count value
        #--------------------
        self.textbox_ch1_value_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch1_value_var.set('')
        self.textbox_ch1_value = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch1_value_var, justify=tk.RIGHT)
        self.textbox_ch1_value.place(x=70, y=25, width=80, height=20)
        #--------------------
        # Create Entry for CH1 Count status
        #--------------------
        self.textbox_ch1_status_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch1_status_var.set('')
        self.textbox_ch1_status = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch1_status_var, justify=tk.RIGHT)
        self.textbox_ch1_status.place(x=170, y=25, width=80, height=20)
        #--------------------
        # Create Label for CH2
        #--------------------
        tk.Label(self.groupbox_counter_value_status, text='CH2 : ', anchor='nw').place(x=10, y=45, width=50, height=20)
        #--------------------
        # Create Entry for CH2 Count value
        #--------------------
        self.textbox_ch2_value_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch2_value_var.set('')
        self.textbox_ch2_value = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch2_value_var, justify=tk.RIGHT)
        self.textbox_ch2_value.place(x=70, y=45, width=80, height=20)
        #--------------------
        # Create Entry for CH2 Count status
        #--------------------
        self.textbox_ch2_status_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch2_status_var.set('')
        self.textbox_ch2_status = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch2_status_var, justify=tk.RIGHT)
        self.textbox_ch2_status.place(x=170, y=45, width=80, height=20)
        #--------------------
        # Create Label for CH3
        #--------------------
        tk.Label(self.groupbox_counter_value_status, text='CH3 : ', anchor='nw').place(x=10, y=65, width=50, height=20)
        #--------------------
        # Create Entry for CH3 Count value
        #--------------------
        self.textbox_ch3_value_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch3_value_var.set('')
        self.textbox_ch3_value = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch3_value_var, justify=tk.RIGHT)
        self.textbox_ch3_value.place(x=70, y=65, width=80, height=20)
        #--------------------
        # Create Entry for CH3 Count status
        #--------------------
        self.textbox_ch3_status_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch3_status_var.set('')
        self.textbox_ch3_status = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch3_status_var, justify=tk.RIGHT)
        self.textbox_ch3_status.place(x=170, y=65, width=80, height=20)
        #----------------------------------------
        # Create LabelFrame for Notification
        #----------------------------------------
        self.groupbox_notification = tk.LabelFrame(self.root, text="Notification : ")
        self.groupbox_notification.place(x=10, y=145, width=280, height=110)
        #--------------------
        # Create Label for Notification
        #--------------------
        self.label_notification = tk.Label(self.groupbox_notification, text='Notification:', anchor='nw')
        self.label_notification.place(x=10, y=5, width=265, height=80)
        #----------------------------------------
        # Return value
        #----------------------------------------
        #--------------------
        # Create Label for Return Value
        #--------------------
        tk.Label(self.root, text='Return Value : ', anchor='nw').place(x=10, y=260, width=100, height=20)
        #--------------------
        # Create Entry for Return Value
        #--------------------
        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=105, y=260, width=185, height=20)
        #----------------------------------------
        # Button
        #----------------------------------------
        #--------------------
        # Create Button for CntInit
        #--------------------
        self.button_cnt_init = tk.Button(self.root, text='CntInit')
        self.button_cnt_init.place(x=305, y=15, width=100, height=20)
        #--------------------
        # Create Button for Counter Mode
        #--------------------
        self.button_cnt_mode = tk.Button(self.root, text='Counter Mode...')
        self.button_cnt_mode.place(x=305, y=42, width=100, height=20)
        #--------------------
        # Create Button for Notification
        #--------------------
        self.button_cnt_notify = tk.Button(self.root, text='Notification...')
        self.button_cnt_notify.place(x=305, y=69, width=100, height=20)
        #--------------------
        # Create Button for Event
        #--------------------
        self.button_cnt_event = tk.Button(self.root, text='Event...')
        self.button_cnt_event.place(x=305, y=96, width=100, height=20)
        #--------------------
        # Create Button for CntStart
        #--------------------
        self.button_cnt_start = tk.Button(self.root, text='CntStart')
        self.button_cnt_start.place(x=305, y=123, width=100, height=20)
        #--------------------
        # Create Button for CntPreset
        #--------------------
        self.button_cnt_preset = tk.Button(self.root, text='CntPreset')
        self.button_cnt_preset.place(x=305, y=150, width=100, height=20)
        #--------------------
        # Create Button for CntReset
        #--------------------
        self.button_cnt_reset = tk.Button(self.root, text='CntReset')
        self.button_cnt_reset.place(x=305, y=177, width=100, height=20)
        #--------------------
        # Create Button for CntStop
        #--------------------
        self.button_cnt_stop = tk.Button(self.root, text='CntStop')
        self.button_cnt_stop.place(x=305, y=204, width=100, height=20)
        #--------------------
        # Create Button for CntExit
        #--------------------
        self.button_cnt_exit = tk.Button(self.root, text='CntExit')
        self.button_cnt_exit.place(x=305, y=231, width=100, height=20)
        #--------------------
        # Create Button for Exit
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=305, y=260, width=100, height=20)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_cnt_init,
                       command_cnt_mode,
                       command_cnt_notify,
                       command_cnt_event,
                       command_cnt_start,
                       command_cnt_preset,
                       command_cnt_reset,
                       command_cnt_stop,
                       command_cnt_exit,
                       command_exit):
        #----------------------------------------
        # Set the callback function for the [CntInit] button
        #----------------------------------------
        self.button_cnt_init.configure(command=command_cnt_init)
        #----------------------------------------
        # Set the callback function for the [Counter Mode...] button
        #----------------------------------------
        self.button_cnt_mode.configure(command=command_cnt_mode)
        #----------------------------------------
        # Set the callback function for the [Notification...] button
        #----------------------------------------
        self.button_cnt_notify.configure(command=command_cnt_notify)
        #----------------------------------------
        # Set the callback function for the [Event...] button
        #----------------------------------------
        self.button_cnt_event.configure(command=command_cnt_event)
        #----------------------------------------
        # 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 [CntReset] button
        #----------------------------------------
        self.button_cnt_reset.configure(command=command_cnt_reset)
        #----------------------------------------
        # 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()