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


#========================================
# SamplingUI Class
#========================================
class SamplingUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Sampling')
        self.root.minsize(480,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=15, y=20, width=85, 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=110, y=20, width=100, height=20)
        #----------------------------------------
        # Transfer count, status, errors, and return value
        #----------------------------------------
        #--------------------
        # Create Label for Count
        #--------------------
        tk.Label(self.root, text='Count : ', anchor='nw').place(x=15, y=170, width=90, height=20)
        #--------------------
        # Create Entry for Count
        #--------------------
        self.textbox_transfer_count_var= tk.StringVar(self.root)
        self.textbox_transfer_count_var.set("")
        tk.Entry(self.root, textvariable=self.textbox_transfer_count_var, justify=tk.RIGHT).place(x=110, y=170, width=230, height=20)
        #--------------------
        # Create Label for Status
        #--------------------
        tk.Label(self.root, text='Status : ', anchor='nw').place(x=15, y=200, width=80, height=20)
        #--------------------
        # Create Entry for Status
        #--------------------
        self.textbox_status_var= tk.StringVar(self.root)
        self.textbox_status_var.set("")
        tk.Entry(self.root, textvariable=self.textbox_status_var).place(x=110, y=200, width=230, height=20)
        #--------------------
        # Create Label for Error
        #--------------------
        tk.Label(self.root, text='Error : ', anchor='nw').place(x=15, y=230, width=80, height=20)
        #--------------------
        # Create Entry for Error
        #--------------------
        self.textbox_error_var= tk.StringVar(self.root)
        self.textbox_error_var.set("")
        tk.Entry(self.root, textvariable=self.textbox_error_var).place(x=110, y=230, width=230, height=20)
        #--------------------
        # Create Label for Return Value
        #--------------------
        tk.Label(self.root, text='Return Value : ', anchor='nw').place(x=15, y=260, width=90, 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=110, y=260, width=230, height=20)
        #----------------------------------------
        # Button
        #----------------------------------------
        #--------------------
        # Create Button for CntInit
        #--------------------
        self.button_cnt_init = tk.Button(self.root, text='CntInit')
        self.button_cnt_init.place(x=360, 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=360, y=42, width=100, height=20)
        #--------------------
        # Create Button for Condition
        #--------------------
        self.button_cnt_condition = tk.Button(self.root, text='Condition...')
        self.button_cnt_condition.place(x=360, y=69, width=100, height=20)
        #--------------------
        # Create Button for Start
        #--------------------
        self.button_cnt_start = tk.Button(self.root, text='Start')
        self.button_cnt_start.place(x=360, y=96, width=100, height=20)
        #--------------------
        # Create Button for Stop
        #--------------------
        self.button_cnt_stop = tk.Button(self.root, text='Stop')
        self.button_cnt_stop.place(x=360, y=123, width=100, height=20)
        #--------------------
        # Create Button for Data
        #--------------------
        self.button_cnt_data = tk.Button(self.root, text='Data...')
        self.button_cnt_data.place(x=360, y=150, width=100, height=20)
        #--------------------
        # Create Button for Count
        #--------------------
        self.button_cnt_count = tk.Button(self.root, text='Count')
        self.button_cnt_count.place(x=360, y=177, width=100, height=20)
        #--------------------
        # Create Button for Status
        #--------------------
        self.button_cnt_status = tk.Button(self.root, text='Status')
        self.button_cnt_status.place(x=360, 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=360, y=231, width=100, height=20)
        #--------------------
        # Create Button for Exit
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=360, y=258, 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_condition,
                       command_cnt_start,
                       command_cnt_stop,
                       command_cnt_data,
                       command_cnt_count,
                       command_cnt_status,
                       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 [Condition...] button
        #----------------------------------------
        self.button_cnt_condition.configure(command=command_cnt_condition)
        #----------------------------------------
        # Set the callback function for the [Start] button
        #----------------------------------------
        self.button_cnt_start.configure(command=command_cnt_start)
        #----------------------------------------
        # Set the callback function for the [Stop] button
        #----------------------------------------
        self.button_cnt_stop.configure(command=command_cnt_stop)
        #----------------------------------------
        # Set the callback function for the [Data...] button
        #----------------------------------------
        self.button_cnt_data.configure(command=command_cnt_data)
        #----------------------------------------
        # Set the callback function for the [Count] button
        #----------------------------------------
        self.button_cnt_count.configure(command=command_cnt_count)
        #----------------------------------------
        # Set the callback function for the [Status] button
        #----------------------------------------
        self.button_cnt_status.configure(command=command_cnt_status)
        #----------------------------------------
        # 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()