#================================================================
#================================================================
# API-AIO(WDM)
# Infinite sampling using device buffer sample
# Condition setting UI file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# ConditionWindowUI class
#========================================
class ConditionWindowUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # Set the parent window
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # Create the condition setting window
        #----------------------------------------
        self.condition_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # Set the modal dialog
        #----------------------------------------
        self.condition_window.grab_set()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.condition_window.title("Sampling Condition Settings")
        self.condition_window.geometry("910x510")
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Create LabelFrame for Start Condition
        #----------------------------------------
        self.groupbox_start_condition = tk.LabelFrame(self.condition_window, text="Start Condition")
        self.groupbox_start_condition.place(x=15, y=10, width=882, height=155)
        #--------------------
        # Create Label for Start Condition
        #--------------------
        tk.Label(self.groupbox_start_condition, text='Start Condition : ', anchor='nw').place(x=5, y=5, width=110, height=19)
        #--------------------
        # Create Combobox for Start Condition
        #--------------------
        self.combobox_start_condition = ttk.Combobox(self.groupbox_start_condition)
        self.combobox_start_condition.place(x=120, y=5, width=200, height=19)
        self.combobox_start_condition['value'] = ('Software', 'External trigger rising edge', 'External trigger falling edge', 'Level comparison', 'In Range comparison', 'Out Range comparison')
        self.combobox_start_condition.current(0)
        self.combobox_start_condition['state'] = 'readonly'
        #--------------------
        # Create LabelFrame for Level Comparison
        #--------------------
        self.groupbox_start_level_comparison = tk.LabelFrame(self.groupbox_start_condition, text="Level Comparison")
        self.groupbox_start_level_comparison.place(x=330, y=0, width=540, height=50)
        #--------------------
        # Create Label for Channel
        #--------------------
        tk.Label(self.groupbox_start_level_comparison, text='Channel : ', anchor='nw').place(x=5, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Channel
        #--------------------
        self.textbox_start_level_channel_no_var = tk.StringVar(self.groupbox_start_level_comparison)
        self.textbox_start_level_channel_no_var.set('0')
        self.textbox_start_level_channel_no = tk.Entry(self.groupbox_start_level_comparison, justify=tk.RIGHT, textvariable=self.textbox_start_level_channel_no_var)
        self.textbox_start_level_channel_no.place(x=65, y=5, width=40, height=19)
        #--------------------
        # Create Label for Comparison Level
        #--------------------
        tk.Label(self.groupbox_start_level_comparison, text='Comparison Level : ', anchor='nw').place(x=110, y=5, width=110, height=19)
        #--------------------
        # Create Entry for Comparison Level
        #--------------------
        self.textbox_start_level_comparison_value_var = tk.StringVar(self.groupbox_start_level_comparison)
        self.textbox_start_level_comparison_value_var.set('1.0')
        self.textbox_start_level_comparison_value = tk.Entry(self.groupbox_start_level_comparison, justify=tk.RIGHT, textvariable=self.textbox_start_level_comparison_value_var)
        self.textbox_start_level_comparison_value.place(x=225, y=5, width=40, height=19)
        #--------------------
        # Create Label for Comparison Level Unit
        #--------------------
        tk.Label(self.groupbox_start_level_comparison, text='[V]', anchor='nw').place(x=265, y=5, width=20, height=19)
        #--------------------
        # Create Label for Comparison Direction
        #--------------------
        tk.Label(self.groupbox_start_level_comparison, text='Comparison Direction : ', anchor='nw').place(x=295, y=5, width=150, height=19)
        #--------------------
        # Create Combobox for Comparison Direction
        #--------------------
        self.combobox_start_level_comparison_direction = ttk.Combobox(self.groupbox_start_level_comparison)
        self.combobox_start_level_comparison_direction.place(x=433, y=5, width=95, height=19)
        self.combobox_start_level_comparison_direction['value'] = ('Both edges', 'Rising edge', 'Falling edge')
        self.combobox_start_level_comparison_direction.current(0)
        self.combobox_start_level_comparison_direction['state'] = 'readonly'
        #--------------------
        # Create LabelFrame for In Range / Out Range Comparison
        #--------------------
        self.groupbox_start_range_comparison = tk.LabelFrame(self.groupbox_start_condition, text="In Range / Out Range Comparison")
        self.groupbox_start_range_comparison.place(x=330, y=55, width=540, height=75)
        #--------------------
        # Create Label for Channel
        #--------------------
        tk.Label(self.groupbox_start_range_comparison, text='Channel : ', anchor='nw').place(x=5, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Channel
        #--------------------
        self.textbox_start_range_channel_no_var = tk.StringVar(self.groupbox_start_range_comparison)
        self.textbox_start_range_channel_no_var.set('0')
        self.textbox_start_range_channel_no = tk.Entry(self.groupbox_start_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_start_range_channel_no_var)
        self.textbox_start_range_channel_no.place(x=65, y=5, width=40, height=19)
        #--------------------
        # Create Label for Upper Limit
        #--------------------
        tk.Label(self.groupbox_start_range_comparison, text='Upper Limit : ', anchor='nw').place(x=110, y=5, width=80, height=19)
        #--------------------
        # Create Entry for Upper Limit
        #--------------------
        self.textbox_start_range_upper_limit_value_var = tk.StringVar(self.groupbox_start_range_comparison)
        self.textbox_start_range_upper_limit_value_var.set('10.0')
        self.textbox_start_range_upper_limit_value = tk.Entry(self.groupbox_start_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_start_range_upper_limit_value_var)
        self.textbox_start_range_upper_limit_value.place(x=225, y=5, width=40, height=19)
        #--------------------
        # Create Label for Upper Limit Unit
        #--------------------
        tk.Label(self.groupbox_start_range_comparison, text='[V]', anchor='nw').place(x=265, y=5, width=20, height=19)
        #--------------------
        # Create Label for Lower Limit
        #--------------------
        tk.Label(self.groupbox_start_range_comparison, text='Lower Limit : ', anchor='nw').place(x=110, y=30, width=80, height=19)
        #--------------------
        # Create Entry for Lower Limit
        #--------------------
        self.textbox_start_range_lower_limit_value_var = tk.StringVar(self.groupbox_start_range_comparison)
        self.textbox_start_range_lower_limit_value_var.set('1.0')
        self.textbox_start_range_lower_limit_value = tk.Entry(self.groupbox_start_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_start_range_lower_limit_value_var)
        self.textbox_start_range_lower_limit_value.place(x=225, y=30, width=40, height=19)
        #--------------------
        # Create Label for Lower Limit Unit
        #--------------------
        tk.Label(self.groupbox_start_range_comparison, text='[V]', anchor='nw').place(x=265, y=30, width=20, height=19)
        #--------------------
        # Create Label for State-holding Times
        #--------------------
        tk.Label(self.groupbox_start_range_comparison, text='State-holding Times : ', anchor='nw').place(x=295, y=5, width=150, height=19)
        #--------------------
        # Create Entry for State-holding Times
        #--------------------
        self.textbox_start_range_state_times_var = tk.StringVar(self.groupbox_start_range_comparison)
        self.textbox_start_range_state_times_var.set('1')
        self.textbox_start_range_state_times = tk.Entry(self.groupbox_start_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_start_range_state_times_var)
        self.textbox_start_range_state_times.place(x=433, y=5, width=40, height=19)
        #--------------------
        # Create LabelFrame for Sampling Clock Condition
        #--------------------
        self.groupbox_sampling_clock_condition = tk.LabelFrame(self.condition_window, text="Sampling Clock Condition")
        self.groupbox_sampling_clock_condition.place(x=15, y=170, width=882, height=75)
        #--------------------
        # Create Label for Clock Type
        #--------------------
        tk.Label(self.groupbox_sampling_clock_condition, text='Clock Type : ', anchor='nw').place(x=5, y=5, width=90, height=19)
        #--------------------
        # Create Combobox for Clock Type
        #--------------------
        self.combobox_clock_type = ttk.Combobox(self.groupbox_sampling_clock_condition)
        self.combobox_clock_type.place(x=120, y=5, width=200, height=19)
        self.combobox_clock_type['value'] = ('Internal clock', 'External clock')
        self.combobox_clock_type.current(0)
        self.combobox_clock_type['state'] = 'readonly'
        #--------------------
        # Create Label for Sampling Clock Period
        #--------------------
        tk.Label(self.groupbox_sampling_clock_condition, text='Sampling Clock Period : ', anchor='nw').place(x=350, y=5, width=200, height=19)
        #--------------------
        # Create Entry for Sampling Clock Period
        #--------------------
        self.textbox_sampling_clock_var = tk.StringVar(self.groupbox_sampling_clock_condition)
        self.textbox_sampling_clock_var.set("1000")
        self.textbox_sampling_clock = tk.Entry(self.groupbox_sampling_clock_condition, justify=tk.RIGHT, textvariable=self.textbox_sampling_clock_var)
        self.textbox_sampling_clock.place(x=548, y=5, width=190, height=19)
        self.textbox_sampling_clock.config(state="normal")
        #--------------------
        # Create Label for Sampling Clock Period Unit
        #--------------------
        tk.Label(self.groupbox_sampling_clock_condition, text='[usec]', anchor='nw').place(x=748, y=5, width=50, height=19)
        #--------------------
        # Create Label for Timing of External Clock Input
        #--------------------
        tk.Label(self.groupbox_sampling_clock_condition, text='Timing of External Clock Input : ', anchor='nw').place(x=350, y=30, width=200, height=19)
        #--------------------
        # Create Combobox for Timing of External Clock Input
        #--------------------
        self.combobox_clock_edge = ttk.Combobox(self.groupbox_sampling_clock_condition)
        self.combobox_clock_edge.place(x=548, y=30, width=190, height=19)
        self.combobox_clock_edge['value'] = ('External input falling edge', 'External input rising edge')
        self.combobox_clock_edge.current(0)
        self.combobox_clock_edge['state'] = 'readonly'
        #--------------------
        # Create LabelFrame for Scan Clock Condition
        #--------------------
        self.groupbox_scan_clock_condition = tk.LabelFrame(self.condition_window, text="Scan Clock Condition")
        self.groupbox_scan_clock_condition.place(x=15, y=250, width=882, height=50)
        #--------------------
        # Create Label for Conversion Speed
        #--------------------
        tk.Label(self.groupbox_scan_clock_condition, text='Conversion Speed : ', anchor='nw').place(x=5, y=5, width=220, height=19)
        #--------------------
        # Create Entry for Conversion Speed
        #--------------------
        self.textbox_scan_clock_var = tk.StringVar(self.groupbox_scan_clock_condition)
        self.textbox_scan_clock_var.set("10")
        self.textbox_scan_clock = tk.Entry(self.groupbox_scan_clock_condition, justify=tk.RIGHT, textvariable=self.textbox_scan_clock_var)
        self.textbox_scan_clock.place(x=120, y=5, width=200, height=19)
        #--------------------
        # Create Label for Conversion Speed Unit
        #--------------------
        tk.Label(self.groupbox_scan_clock_condition, text='[usec]', anchor='nw').place(x=330, y=5, width=50, height=19)
        #----------------------------------------
        # Create LabelFrame for Stop Condition
        #----------------------------------------
        self.groupbox_stop_condition = tk.LabelFrame(self.condition_window, text="Stop Condition")
        self.groupbox_stop_condition.place(x=15, y=305, width=882, height=155)
        #--------------------
        # Create Label for Stop Condition
        #--------------------
        tk.Label(self.groupbox_stop_condition, text='Stop Condition : ', anchor='nw').place(x=5, y=5, width=100, height=19)
        #--------------------
        # Create Combobox for Stop Condition
        #--------------------
        self.combobox_stop_condition = ttk.Combobox(self.groupbox_stop_condition)
        self.combobox_stop_condition.place(x=120, y=5, width=200, height=19)
        self.combobox_stop_condition['value'] = ('External trigger rising edge', 'External trigger falling edge', 'Level comparison', 'Command (AioStopAi)', 'In Range comparison', 'Out Range comparison')
        self.combobox_stop_condition.current(3)
        self.combobox_stop_condition['state'] = 'readonly'
        #--------------------
        # Create LabelFrame for Level Comparison
        #--------------------
        self.groupbox_stop_level_comparison = tk.LabelFrame(self.groupbox_stop_condition, text="Level Comparison")
        self.groupbox_stop_level_comparison.place(x=330, y=0, width=540, height=50)
        #--------------------
        # Create Label for Channel
        #--------------------
        tk.Label(self.groupbox_stop_level_comparison, text='Channel : ', anchor='nw').place(x=5, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Channel
        #--------------------
        self.textbox_stop_level_channel_no_var = tk.StringVar(self.groupbox_stop_level_comparison)
        self.textbox_stop_level_channel_no_var.set('0')
        self.textbox_stop_level_channel_no = tk.Entry(self.groupbox_stop_level_comparison, justify=tk.RIGHT, textvariable=self.textbox_stop_level_channel_no_var)
        self.textbox_stop_level_channel_no.place(x=65, y=5, width=40, height=19)
        #--------------------
        # Create Label for Comparison Level
        #--------------------
        tk.Label(self.groupbox_stop_level_comparison, text='Comparison Level : ', anchor='nw').place(x=110, y=5, width=110, height=19)
        #--------------------
        # Create Entry for Comparison Level
        #--------------------
        self.textbox_stop_level_comparison_value_var = tk.StringVar(self.groupbox_stop_level_comparison)
        self.textbox_stop_level_comparison_value_var.set('1.0')
        self.textbox_stop_level_comparison_value = tk.Entry(self.groupbox_stop_level_comparison, justify=tk.RIGHT, textvariable=self.textbox_stop_level_comparison_value_var)
        self.textbox_stop_level_comparison_value.place(x=225, y=5, width=40, height=19)
        #--------------------
        # Create Label for Comparison Level Unit
        #--------------------
        tk.Label(self.groupbox_stop_level_comparison, text='[V]', anchor='nw').place(x=265, y=5, width=20, height=19)
        #--------------------
        # Create Label for Comparison Direction
        #--------------------
        tk.Label(self.groupbox_stop_level_comparison, text='Comparison Direction : ', anchor='nw').place(x=295, y=5, width=150, height=19)
        #--------------------
        # Create Combobox for Comparison Direction
        #--------------------
        self.combobox_stop_level_comparison_direction = ttk.Combobox(self.groupbox_stop_level_comparison)
        self.combobox_stop_level_comparison_direction.place(x=433, y=5, width=95, height=19)
        self.combobox_stop_level_comparison_direction['value'] = ('Both edges', 'Rising edge', 'Falling edge')
        self.combobox_stop_level_comparison_direction.current(0)
        self.combobox_stop_level_comparison_direction['state'] = 'readonly'
        #--------------------
        # Create LabelFrame for In Range / Out Range Comparison
        #--------------------
        self.groupbox_stop_range_comparison = tk.LabelFrame(self.groupbox_stop_condition, text="In Range / Out Range Comparison")
        self.groupbox_stop_range_comparison.place(x=330, y=55, width=540, height=75)
        #--------------------
        # Create Label for Channel
        #--------------------
        tk.Label(self.groupbox_stop_range_comparison, text='Channel : ', anchor='nw').place(x=5, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Channel
        #--------------------
        self.textbox_stop_range_channel_no_var = tk.StringVar(self.groupbox_stop_range_comparison)
        self.textbox_stop_range_channel_no_var.set('0')
        self.textbox_stop_range_channel_no = tk.Entry(self.groupbox_stop_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_stop_range_channel_no_var)
        self.textbox_stop_range_channel_no.place(x=65, y=5, width=40, height=19)
        #--------------------
        # Create Label for Upper Limit
        #--------------------
        tk.Label(self.groupbox_stop_range_comparison, text='Upper Limit : ', anchor='nw').place(x=110, y=5, width=80, height=19)
        #--------------------
        # Create Entry for Upper Limit
        #--------------------
        self.textbox_stop_range_upper_limit_value_var = tk.StringVar(self.groupbox_stop_range_comparison)
        self.textbox_stop_range_upper_limit_value_var.set('10.0')
        self.textbox_stop_range_upper_limit_value = tk.Entry(self.groupbox_stop_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_stop_range_upper_limit_value_var)
        self.textbox_stop_range_upper_limit_value.place(x=225, y=5, width=40, height=19)
        #--------------------
        # Create Label for Upper Limit Unit
        #--------------------
        tk.Label(self.groupbox_stop_range_comparison, text='[V]', anchor='nw').place(x=265, y=5, width=20, height=19)
        #--------------------
        # Create Label for Lower Limit
        #--------------------
        tk.Label(self.groupbox_stop_range_comparison, text='Lower Limit : ', anchor='nw').place(x=110, y=30, width=80, height=19)
        #--------------------
        # Create Entry for Lower Limit
        #--------------------
        self.textbox_stop_range_lower_limit_value_var = tk.StringVar(self.groupbox_stop_range_comparison)
        self.textbox_stop_range_lower_limit_value_var.set('1.0')
        self.textbox_stop_range_lower_limit_value = tk.Entry(self.groupbox_stop_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_stop_range_lower_limit_value_var)
        self.textbox_stop_range_lower_limit_value.place(x=225, y=30, width=40, height=19)
        #--------------------
        # Create Label for Lower Limit Unit
        #--------------------
        tk.Label(self.groupbox_stop_range_comparison, text='[V]', anchor='nw').place(x=265, y=30, width=20, height=19)
        #--------------------
        # Create Label for State-holding Times
        #--------------------
        tk.Label(self.groupbox_stop_range_comparison, text='State-holding Times : ', anchor='nw').place(x=295, y=5, width=150, height=19)
        #--------------------
        # Create Entry for State-holding Times
        #--------------------
        self.textbox_stop_range_state_times_var = tk.StringVar(self.groupbox_stop_range_comparison)
        self.textbox_stop_range_state_times_var.set('1')
        self.textbox_stop_range_state_times = tk.Entry(self.groupbox_stop_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_stop_range_state_times_var)
        self.textbox_stop_range_state_times.place(x=433, y=5, width=40, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Button for OK
        #--------------------
        self.button_ok = tk.Button(self.condition_window, text='OK')
        self.button_ok.place(x=670, y=470, width=80, height=19)
        #--------------------
        # Create Button for Cancel
        #--------------------
        self.button_cancel = tk.Button(self.condition_window, text='Cancel')
        self.button_cancel.place(x=760, y=470, width=80, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_start_condition,
                       command_clock_type,
                       command_stop_condition,
                       command_ok,
                       command_cancel):
        #----------------------------------------
        # Set the callback function for the [Start Condition] button
        #----------------------------------------
        self.combobox_start_condition.bind('<<ComboboxSelected>>', command_start_condition)
        #----------------------------------------
        # Set the callback function for the [Clock Type] button
        #----------------------------------------
        self.combobox_clock_type.bind('<<ComboboxSelected>>', command_clock_type)
        #----------------------------------------
        # Set the callback function for the [Stop Condition] button
        #----------------------------------------
        self.combobox_stop_condition.bind('<<ComboboxSelected>>', command_stop_condition)
        #----------------------------------------
        # Set the callback function for the [OK] button
        #----------------------------------------
        self.button_ok.configure(command=command_ok)
        #----------------------------------------
        # Set the callback function for the [Cancel] button
        #----------------------------------------
        self.button_cancel.configure(command=command_cancel)

    #========================================
    # Display the error in a message box
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.condition_window)