#================================================================
#================================================================
# API-AIO(WDM)
# Simple analog input sample
# main UI file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# AiSimpleUI class
#========================================
class AiSimpleUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Simple Analog Input')
        self.root.minsize(740,510)
        #----------------------------------------
        # 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 Initialize
        #----------------------------------------
        self.groupbox_init = tk.LabelFrame(self.root, text="Initialize")
        self.groupbox_init.place(x=15, y=10, width=710, height=50)
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.groupbox_init, text='Device Name : ', anchor='nw').place(x=15, y=5, width=85, height=19)
        #--------------------
        # Create Entry for Device Name
        #--------------------
        self.textbox_device_name_var = tk.StringVar(self.groupbox_init)
        self.textbox_device_name_var.set("AIO000")
        tk.Entry(self.groupbox_init, textvariable=self.textbox_device_name_var).place(x=145, y=5, width=130, height=19)
        #--------------------
        # Create Button for AioInit
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # Create LabelFrame for Range Set
        #----------------------------------------
        self.groupbox_range_settings = tk.LabelFrame(self.root, text="Range Set")
        self.groupbox_range_settings.place(x=15, y=65, width=710, height=50)
        #--------------------
        # Create Label for Range
        #--------------------
        tk.Label(self.groupbox_range_settings, text='Range : ', anchor='nw').place(x=15, y=5, width=75, height=19)
        #--------------------
        # Create Combobox for Range
        #--------------------
        self.combobox_range = ttk.Combobox(self.groupbox_range_settings)
        self.combobox_range.place(x=145, y=5, width=130, height=19)
        self.combobox_range['value'] = ('-10 - +10V', '-5 - +5V', '-2.5 - +2.5V', '-1.25 - +1.25V', '0 - +10V', '0 - +5V', '0 - +2.5V', '0 - +1.25V', '0 - 20mA', '4 - 20mA', '-20 - +20mA', '1 - +5V')
        self.combobox_range.current(0)
        self.combobox_range['state'] = 'readonly'
        #--------------------
        # Create Button for AioSetAiRangeAll
        #--------------------
        self.button_aio_set_ai_range_all = tk.Button(self.groupbox_range_settings, text='AioSetAiRangeAll')
        self.button_aio_set_ai_range_all.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # Create LabelFrame for Input Method Set
        #----------------------------------------
        self.groupbox_input_method_settings = tk.LabelFrame(self.root, text="Input Method Set")
        self.groupbox_input_method_settings.place(x=15, y=120, width=710, height=50)
        #--------------------
        # Create Label for Input Method
        #--------------------
        tk.Label(self.groupbox_input_method_settings, text='Input Method : ', anchor='nw').place(x=15, y=5, width=85, height=19)
        #--------------------
        # Create Combobox for Input Method
        #--------------------
        self.combobox_input_method = ttk.Combobox(self.groupbox_input_method_settings)
        self.combobox_input_method.place(x=145, y=5, width=130, height=19)
        self.combobox_input_method['value'] = ('Single-end input', 'Differential input')
        self.combobox_input_method.current(0)
        self.combobox_input_method['state'] = 'readonly'
        #--------------------
        # Create Button for AioSetAiInputMethod
        #--------------------
        self.button_aio_set_ai_input_method = tk.Button(self.groupbox_input_method_settings, text='AioSetAiInputMethod')
        self.button_aio_set_ai_input_method.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # Create LabelFrame for Specify Channel Simple Input
        #----------------------------------------
        self.groupbox_single_ai_ex = tk.LabelFrame(self.root, text="Specify Channel Simple Input")
        self.groupbox_single_ai_ex.place(x=15, y=175, width=710, height=50)
        #--------------------
        # Create Label for ChannelNo
        #--------------------
        tk.Label(self.groupbox_single_ai_ex, text='ChannelNo : ', anchor='nw').place(x=15, y=5, width=85, height=19)
        #--------------------
        # Create Entry for ChannelNo
        #--------------------
        self.textbox_channel_no_var = tk.StringVar(self.groupbox_single_ai_ex)
        self.textbox_channel_no_var.set("0")
        tk.Entry(self.groupbox_single_ai_ex, justify=tk.RIGHT, textvariable=self.textbox_channel_no_var).place(x=145, y=5, width=130, height=19)
        #--------------------
        # Create Label for Input Data
        #--------------------
        tk.Label(self.groupbox_single_ai_ex, text='Input Data : ', anchor='nw').place(x=280, y=5, width=75, height=19)
        #--------------------
        # Create Entry for Input Data
        #--------------------
        self.textbox_single_ai_data_var = tk.StringVar(self.groupbox_single_ai_ex)
        tk.Entry(self.groupbox_single_ai_ex, justify=tk.RIGHT, textvariable=self.textbox_single_ai_data_var).place(x=360, y=5, width=130, height=19)
        #--------------------
        # Create Button for AioSingleAiEx
        #--------------------
        self.button_aio_single_ai_ex = tk.Button(self.groupbox_single_ai_ex, text='AioSingleAiEx')
        self.button_aio_single_ai_ex.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # Create LabelFrame for Multi Channel Simple Input
        #----------------------------------------
        self.groupbox_multi_ai_ex = tk.LabelFrame(self.root, text="Multi Channel Simple Input")
        self.groupbox_multi_ai_ex.place(x=15, y=230, width=710, height=150)
        #--------------------
        # Create Label for Number of Channels
        #--------------------
        tk.Label(self.groupbox_multi_ai_ex, text='Number of Channels : ', anchor='nw').place(x=15, y=30, width=120, height=19)
        #--------------------
        # Create Entry for Number of Channels
        #--------------------
        self.textbox_channel_num_var = tk.StringVar(self.groupbox_multi_ai_ex)
        self.textbox_channel_num_var.set("1")
        tk.Entry(self.groupbox_multi_ai_ex, justify=tk.RIGHT, textvariable=self.textbox_channel_num_var).place(x=145, y=30, width=130, height=19)
        #--------------------
        # Create Label for Input Data
        #--------------------
        tk.Label(self.groupbox_multi_ai_ex, text='Input Data : ', anchor='nw').place(x=280, y=30, width=75, height=19)
        #--------------------
        # Create Label for ChannelNo
        #--------------------
        tk.Label(self.groupbox_multi_ai_ex, text='ChannelNo', anchor='nw').place(x=360, y=5, width=160, height=19)
        #--------------------
        # Create Label for Voltage/Current
        #--------------------
        tk.Label(self.groupbox_multi_ai_ex, text='Voltage/Current', anchor='nw').place(x=440, y=5, width=160, height=19)
        #--------------------
        # Create Listbox for Input Data
        #--------------------
        self.listbox_multi_ai_data_var = tk.StringVar(self.groupbox_multi_ai_ex)
        self.listbox_multi_ai_data = tk.Listbox(self.groupbox_multi_ai_ex, listvariable=self.listbox_multi_ai_data_var)
        self.listbox_y_scrollbar = tk.Scrollbar(self.groupbox_multi_ai_ex, orient="vertical", command=self.listbox_multi_ai_data.yview)
        self.listbox_multi_ai_data.config(yscrollcommand=self.listbox_y_scrollbar.set)
        self.listbox_multi_ai_data.place(x=360, y=30, width=170, height=90)
        self.listbox_y_scrollbar.place(x=530, y=30, width=10, height=90)
        #--------------------
        # Create Button for AioMultiAiEx
        #--------------------
        self.button_aio_multi_ai_ex = tk.Button(self.groupbox_multi_ai_ex, text='AioMultiAiEx')
        self.button_aio_multi_ai_ex.place(x=550, y=30, width=150, height=19)
        #----------------------------------------
        # Create LabelFrame for Exit
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="Exit")
        self.groupbox_exit.place(x=15, y=385, width=710, height=50)
        #--------------------
        # Create Button for AioExit
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=33, y=450, width=160, height=19)
        #--------------------
        # 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=142, y=450, width=330, height=19)
        #--------------------
        # Create Button for Close
        #--------------------
        self.button_close = tk.Button(self.root, text='Close')
        self.button_close.place(x=567, y=475, width=150, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_set_ai_range_all,
                       command_aio_set_ai_input_method,
                       command_aio_single_ai_ex,
                       command_aio_multi_ai_ex,
                       command_aio_exit,
                       command_close):
        #----------------------------------------
        # Set the callback function for the [AioInit] button
        #----------------------------------------
        self.button_aio_init.configure(command=command_aio_init)
        #----------------------------------------
        # Set the callback function for the [AioSetAiRangeAll] button
        #----------------------------------------
        self.button_aio_set_ai_range_all.configure(command=command_aio_set_ai_range_all)
        #----------------------------------------
        # Set the callback function for the [AioSetAiInputMethod] button
        #----------------------------------------
        self.button_aio_set_ai_input_method.configure(command=command_aio_set_ai_input_method)
        #----------------------------------------
        # Set the callback function for the [AioSingleAiEx] button
        #----------------------------------------
        self.button_aio_single_ai_ex.configure(command=command_aio_single_ai_ex)
        #----------------------------------------
        # Set the callback function for the [AioMultiAiEx] button
        #----------------------------------------
        self.button_aio_multi_ai_ex.configure(command=command_aio_multi_ai_ex)
        #----------------------------------------
        # Set the callback function for the [AioExit] button
        #----------------------------------------
        self.button_aio_exit.configure(command=command_aio_exit)
        #----------------------------------------
        # Set the callback function for the [Close] button
        #----------------------------------------
        self.button_close.configure(command=command_close)

    #========================================
    # 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()