#================================================================
#================================================================
# API-CNT(WDM) for Device with sampling function
# Synchronous sampling sample
# main UI file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


#========================================
# SyncUI Class
#========================================
class SyncUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Sync Sampling')
        self.root.minsize(720,350)
        #----------------------------------------
        # 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):
        #----------------------------------------
        # Create LabelFrame for Device Name
        #----------------------------------------
        self.groupbox_device_name = tk.LabelFrame(self.root, text="Device Name")
        self.groupbox_device_name.place(x=10, y=10, width=390, height=40)
        #--------------------
        # Create Label for Master
        #--------------------
        tk.Label(self.groupbox_device_name, text='Master : ', anchor='nw').place(x=10, y=0, width=70, height=20)
        #--------------------
        # Create Entry for Master
        #--------------------
        self.textbox_master_device_name_var= tk.StringVar(self.root)
        self.textbox_master_device_name_var.set("CNT000")
        tk.Entry(self.groupbox_device_name, textvariable=self.textbox_master_device_name_var).place(x=80, y=0, width=100, height=20)
        #--------------------
        # Create Label for Slave
        #--------------------
        tk.Label(self.groupbox_device_name, text='Slave : ', anchor='nw').place(x=200, y=0, width=70, height=20)
        #--------------------
        # Create Entry for Slave
        #--------------------
        self.textbox_slave_device_name_var= tk.StringVar(self.root)
        self.textbox_slave_device_name_var.set("CNT001")
        tk.Entry(self.groupbox_device_name, textvariable=self.textbox_slave_device_name_var).place(x=270, y=0, width=100, height=20)
        #----------------------------------------
        # Create LabelFrame for Master
        #----------------------------------------
        self.groupbox_master = tk.LabelFrame(self.root, text="Master")
        self.groupbox_master.place(x=10, y=50, width=390, height=130)
        #--------------------
        # Create Label for Count
        #--------------------
        tk.Label(self.groupbox_master, text='Count : ', anchor='nw').place(x=10, y=0, width=80, height=20)
        #--------------------
        # Create Entry for Count
        #--------------------
        self.textbox_master_transfer_count_var= tk.StringVar(self.groupbox_master)
        self.textbox_master_transfer_count_var.set("")
        tk.Entry(self.groupbox_master, textvariable=self.textbox_master_transfer_count_var, justify=tk.RIGHT).place(x=100, y=0, width=270, height=20)
        #--------------------
        # Create Label for Status
        #--------------------
        tk.Label(self.groupbox_master, text='Status : ', anchor='nw').place(x=10, y=30, width=80, height=20)
        #--------------------
        # Create Entry for Status
        #--------------------
        self.textbox_master_status_var= tk.StringVar(self.groupbox_master)
        self.textbox_master_status_var.set("")
        tk.Entry(self.groupbox_master, textvariable=self.textbox_master_status_var).place(x=100, y=30, width=270, height=20)
        #--------------------
        # Create Label for Error
        #--------------------
        tk.Label(self.groupbox_master, text='Error : ', anchor='nw').place(x=10, y=60, width=80, height=20)
        #--------------------
        # Create Entry for Error
        #--------------------
        self.textbox_master_error_var= tk.StringVar(self.groupbox_master)
        self.textbox_master_error_var.set("")
        tk.Entry(self.groupbox_master, textvariable=self.textbox_master_error_var).place(x=100, y=60, width=270, height=20)
        #--------------------
        # Create Label for Return Value
        #--------------------
        tk.Label(self.groupbox_master, text='Return Value : ', anchor='nw').place(x=10, y=90, width=90, height=20)
        #--------------------
        # Create Entry for Return Value
        #--------------------
        self.textbox_master_return_string_var= tk.StringVar(self.groupbox_master)
        self.textbox_master_return_string_var.set("")
        tk.Entry(self.groupbox_master, textvariable=self.textbox_master_return_string_var).place(x=100, y=90, width=270, height=20)
        #----------------------------------------
        # Create LabelFrame for Slave
        #----------------------------------------
        self.groupbox_slave = tk.LabelFrame(self.root, text="Slave")
        self.groupbox_slave.place(x=10, y=180, width=390, height=130)
        #--------------------
        # Create Label for Count
        #--------------------
        tk.Label(self.groupbox_slave, text='Count : ', anchor='nw').place(x=10, y=0, width=80, height=20)
        #--------------------
        # Create Entry for Count
        #--------------------
        self.textbox_slave_transfer_count_var= tk.StringVar(self.groupbox_slave)
        self.textbox_slave_transfer_count_var.set("")
        tk.Entry(self.groupbox_slave, textvariable=self.textbox_slave_transfer_count_var, justify=tk.RIGHT).place(x=100, y=0, width=270, height=20)
        #--------------------
        # Create Label for Status
        #--------------------
        tk.Label(self.groupbox_slave, text='Status : ', anchor='nw').place(x=10, y=30, width=80, height=20)
        #--------------------
        # Create Entry for Status
        #--------------------
        self.textbox_slave_status_var= tk.StringVar(self.groupbox_slave)
        self.textbox_slave_status_var.set("")
        tk.Entry(self.groupbox_slave, textvariable=self.textbox_slave_status_var).place(x=100, y=30, width=270, height=20)
        #--------------------
        # Create Label for Error
        #--------------------
        tk.Label(self.groupbox_slave, text='Error : ', anchor='nw').place(x=10, y=60, width=80, height=20)
        #--------------------
        # Create Entry for Error
        #--------------------
        self.textbox_slave_error_var= tk.StringVar(self.groupbox_slave)
        self.textbox_slave_error_var.set("")
        tk.Entry(self.groupbox_slave, textvariable=self.textbox_slave_error_var).place(x=100, y=60, width=270, height=20)
        #--------------------
        # Create Label for Return Value
        #--------------------
        tk.Label(self.groupbox_slave, text='Return Value : ', anchor='nw').place(x=10, y=90, width=90, height=20)
        #--------------------
        # Create Entry for Return Value
        #--------------------
        self.textbox_slave_return_string_var= tk.StringVar(self.groupbox_slave)
        self.textbox_slave_return_string_var.set("")
        tk.Entry(self.groupbox_slave, textvariable=self.textbox_slave_return_string_var).place(x=100, y=90, width=270, height=20)
        #----------------------------------------
        # Create LabelFrame for Master Button
        #----------------------------------------
        self.groupbox_master_button = tk.LabelFrame(self.root, text="Master")
        self.groupbox_master_button.place(x=410, y=10, width=145, height=300)
        #--------------------
        # Create Button for CntInit
        #--------------------
        self.button_m_cnt_init = tk.Button(self.groupbox_master_button, text='CntInit')
        self.button_m_cnt_init.place(x=5, y=10, width=130, height=20)
        #--------------------
        # Create Button for Mode
        #--------------------
        self.button_m_cnt_mode = tk.Button(self.groupbox_master_button, text='Mode...')
        self.button_m_cnt_mode.place(x=5, y=40, width=130, height=20)
        #--------------------
        # Create Button for Sampling Mode
        #--------------------
        self.button_m_cnt_condition = tk.Button(self.groupbox_master_button, text='Sampling Mode...')
        self.button_m_cnt_condition.place(x=5, y=70, width=130, height=20)
        #--------------------
        # Create Button for Start
        #--------------------
        self.button_m_cnt_start = tk.Button(self.groupbox_master_button, text='Start')
        self.button_m_cnt_start.place(x=5, y=100, width=130, height=20)
        #--------------------
        # Create Button for Stop
        #--------------------
        self.button_m_cnt_stop = tk.Button(self.groupbox_master_button, text='Stop')
        self.button_m_cnt_stop.place(x=5, y=130, width=130, height=20)
        #--------------------
        # Create Button for Data
        #--------------------
        self.button_m_cnt_data = tk.Button(self.groupbox_master_button, text='Data...')
        self.button_m_cnt_data.place(x=5, y=160, width=130, height=20)
        #--------------------
        # Create Button for Count
        #--------------------
        self.button_m_cnt_count = tk.Button(self.groupbox_master_button, text='Count')
        self.button_m_cnt_count.place(x=5, y=190, width=130, height=20)
        #--------------------
        # Create Button for Status
        #--------------------
        self.button_m_cnt_status = tk.Button(self.groupbox_master_button, text='Status')
        self.button_m_cnt_status.place(x=5, y=220, width=130, height=20)
        #--------------------
        # Create Button for CntExit
        #--------------------
        self.button_m_cnt_exit = tk.Button(self.groupbox_master_button, text='CntExit')
        self.button_m_cnt_exit.place(x=5, y=250, width=130, height=20)
        #----------------------------------------
        # Create LabelFrame for Slave Button
        #----------------------------------------
        self.groupbox_slave_button = tk.LabelFrame(self.root, text="Slave")
        self.groupbox_slave_button.place(x=565, y=10, width=145, height=300)
        #--------------------
        # Create Button for CntInit
        #--------------------
        self.button_s_cnt_init = tk.Button(self.groupbox_slave_button, text='CntInit')
        self.button_s_cnt_init.place(x=5, y=10, width=130, height=20)
        #--------------------
        # Create Button for Mode
        #--------------------
        self.button_s_cnt_mode = tk.Button(self.groupbox_slave_button, text='Mode...')
        self.button_s_cnt_mode.place(x=5, y=40, width=130, height=20)
        #--------------------
        # Create Button for Sampling Mode
        #--------------------
        self.button_s_cnt_condition = tk.Button(self.groupbox_slave_button, text='Sampling Mode...')
        self.button_s_cnt_condition.place(x=5, y=70, width=130, height=20)
        #--------------------
        # Create Button for Start
        #--------------------
        self.button_s_cnt_start = tk.Button(self.groupbox_slave_button, text='Start')
        self.button_s_cnt_start.place(x=5, y=100, width=130, height=20)
        #--------------------
        # Create Button for Stop
        #--------------------
        self.button_s_cnt_stop = tk.Button(self.groupbox_slave_button, text='Stop')
        self.button_s_cnt_stop.place(x=5, y=130, width=130, height=20)
        #--------------------
        # Create Button for Data
        #--------------------
        self.button_s_cnt_data = tk.Button(self.groupbox_slave_button, text='Data...')
        self.button_s_cnt_data.place(x=5, y=160, width=130, height=20)
        #--------------------
        # Create Button for Count
        #--------------------
        self.button_s_cnt_count = tk.Button(self.groupbox_slave_button, text='Count')
        self.button_s_cnt_count.place(x=5, y=190, width=130, height=20)
        #--------------------
        # Create Button for Status
        #--------------------
        self.button_s_cnt_status = tk.Button(self.groupbox_slave_button, text='Status')
        self.button_s_cnt_status.place(x=5, y=220, width=130, height=20)
        #--------------------
        # Create Button for CntExit
        #--------------------
        self.button_s_cnt_exit = tk.Button(self.groupbox_slave_button, text='CntExit')
        self.button_s_cnt_exit.place(x=5, y=250, width=130, height=20)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Button for Exit
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=490, y=320, width=130, height=20)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_m_cnt_init,
                       command_m_cnt_mode,
                       command_m_cnt_condition,
                       command_m_cnt_start,
                       command_m_cnt_stop,
                       command_m_cnt_data,
                       command_m_cnt_count,
                       command_m_cnt_status,
                       command_m_cnt_exit,
                       command_s_cnt_init,
                       command_s_cnt_mode,
                       command_s_cnt_condition,
                       command_s_cnt_start,
                       command_s_cnt_stop,
                       command_s_cnt_data,
                       command_s_cnt_count,
                       command_s_cnt_status,
                       command_s_cnt_exit,
                       command_exit):
        #----------------------------------------
        # Set the callback function for the master [CntInit] button
        #----------------------------------------
        self.button_m_cnt_init.configure(command=command_m_cnt_init)
        #----------------------------------------
        # Set the callback function for the master [Counter Mode...] button
        #----------------------------------------
        self.button_m_cnt_mode.configure(command=command_m_cnt_mode)
        #----------------------------------------
        # Set the callback function for the master [Condition...] button
        #----------------------------------------
        self.button_m_cnt_condition.configure(command=command_m_cnt_condition)
        #----------------------------------------
        # Set the callback function for the master [Start] button
        #----------------------------------------
        self.button_m_cnt_start.configure(command=command_m_cnt_start)
        #----------------------------------------
        # Set the callback function for the master [Stop] button
        #----------------------------------------
        self.button_m_cnt_stop.configure(command=command_m_cnt_stop)
        #----------------------------------------
        # Set the callback function for the master [Data...] button
        #----------------------------------------
        self.button_m_cnt_data.configure(command=command_m_cnt_data)
        #----------------------------------------
        # Set the callback function for the master [Count] button
        #----------------------------------------
        self.button_m_cnt_count.configure(command=command_m_cnt_count)
        #----------------------------------------
        # Set the callback function for the master [Status] button
        #----------------------------------------
        self.button_m_cnt_status.configure(command=command_m_cnt_status)
        #----------------------------------------
        # Set the callback function for the master[CntExit] button
        #----------------------------------------
        self.button_m_cnt_exit.configure(command=command_m_cnt_exit)
        #----------------------------------------
        # Set the callback function for the slave [CntInit] button
        #----------------------------------------
        self.button_s_cnt_init.configure(command=command_s_cnt_init)
        #----------------------------------------
        # Set the callback function for the slave [Counter Mode...] button
        #----------------------------------------
        self.button_s_cnt_mode.configure(command=command_s_cnt_mode)
        #----------------------------------------
        # Set the callback function for the slave [Condition...] button
        #----------------------------------------
        self.button_s_cnt_condition.configure(command=command_s_cnt_condition)
        #----------------------------------------
        # Set the callback function for the slave [Start] button
        #----------------------------------------
        self.button_s_cnt_start.configure(command=command_s_cnt_start)
        #----------------------------------------
        # Set the callback function for the slave [Stop] button
        #----------------------------------------
        self.button_s_cnt_stop.configure(command=command_s_cnt_stop)
        #----------------------------------------
        # Set the callback function for the slave [Data...] button
        #----------------------------------------
        self.button_s_cnt_data.configure(command=command_s_cnt_data)
        #----------------------------------------
        # Set the callback function for the slave [Count] button
        #----------------------------------------
        self.button_s_cnt_count.configure(command=command_s_cnt_count)
        #----------------------------------------
        # Set the callback function for the slave [Status] button
        #----------------------------------------
        self.button_s_cnt_status.configure(command=command_s_cnt_status)
        #----------------------------------------
        # Set the callback function for the slave [CntExit] button
        #----------------------------------------
        self.button_s_cnt_exit.configure(command=command_s_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()