#================================================================
#================================================================
# API-CNT(WDM) for Device with sampling function
# Sampling sample
# Sampling 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 Setting")
        self.condition_window.geometry("590x400")
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Create LabelFrame for Sampling Condition
        #----------------------------------------
        self.groupbox_sampling_condition = tk.LabelFrame(self.condition_window, text="Sampling Condition")
        self.groupbox_sampling_condition.place(x=10, y=10, width=570, height=140)
        #--------------------
        # Create Label for Start Condition
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='Start Condition : ', anchor='nw').place(x=5, y=5, width=110, height=20)
        #--------------------
        # Create Combobox for Start Condition
        #--------------------
        self.combobox_start_condition = ttk.Combobox(self.groupbox_sampling_condition)
        self.combobox_start_condition.place(x=120, y=5, width=240, height=20)
        self.combobox_start_condition['value'] = ('Software Start', 'External Signal Rising', 'External Signal Falling', 
                                                  'Count-match CH0 Compare-register 0', 'Count-match CH1 Compare-register 0', 
                                                  'Count-match CH2 Compare-register 0', 'Count-match CH3 Compare-register 0', 
                                                  'Count-match CH0 Compare-register 1', 'Count-match CH1 Compare-register 1', 
                                                  'Count-match CH2 Compare-register 1', 'Count-match CH3 Compare-register 1')
        self.combobox_start_condition.current(0)
        self.combobox_start_condition['state'] = 'readonly'
        #--------------------
        # Create Label for Clock Condition
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='Clock Condition : ', anchor='nw').place(x=5, y=35, width=110, height=20)
        #--------------------
        # Create Combobox for Clock Condition
        #--------------------
        self.combobox_clock_condition = ttk.Combobox(self.groupbox_sampling_condition)
        self.combobox_clock_condition.place(x=120, y=35, width=240, height=20)
        self.combobox_clock_condition['value'] = ('Internal Clock', 'External Clock')
        self.combobox_clock_condition.current(0)
        self.combobox_clock_condition['state'] = 'readonly'
        #--------------------
        # Create Label for Stop Condition
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='Stop Condition : ', anchor='nw').place(x=5, y=65, width=110, height=20)
        #--------------------
        # Create Combobox for Stop Condition
        #--------------------
        self.combobox_stop_condition = ttk.Combobox(self.groupbox_sampling_condition)
        self.combobox_stop_condition.place(x=120, y=65, width=240, height=20)
        self.combobox_stop_condition['value'] = ('Software Stop', 'External Signal Rising', 'External Signal Falling', 'Stop Transfer by Specified Times', 
                                                 'Count-match CH0 Compare-register 0', 'Count-match CH1 Compare-register 0', 
                                                 'Count-match CH2 Compare-register 0', 'Count-match CH3 Compare-register 0', 
                                                 'Count-match CH0 Compare-register 1', 'Count-match CH1 Compare-register 1', 
                                                 'Count-match CH2 Compare-register 1', 'Count-match CH3 Compare-register 1')
        self.combobox_stop_condition.current(0)
        self.combobox_stop_condition['state'] = 'readonly'
        #--------------------
        # Create Label for Comparison Value
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='Comparison Value : ', anchor='nw').place(x=365, y=5, width=135, height=20)
        #--------------------
        # Create Entry for Comparison Value
        #--------------------
        self.textbox_start_compare_value_var = tk.StringVar(self.groupbox_sampling_condition)
        self.textbox_start_compare_value_var.set('100')
        self.textbox_start_compare_value = tk.Entry(self.groupbox_sampling_condition, textvariable=self.textbox_start_compare_value_var, justify=tk.RIGHT)
        self.textbox_start_compare_value.place(x=485, y=5, width=55, height=20)
        #--------------------
        # Create Label for Hex
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='Hex', anchor='nw').place(x=540, y=5, width=20, height=20)
        #--------------------
        # Create Label for Internal Clock
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='Internal Clock : ', anchor='nw').place(x=365, y=35, width=135, height=20)
        #--------------------
        # Create Entry for Internal Clock
        #--------------------
        self.textbox_internal_clock_value_var = tk.StringVar(self.groupbox_sampling_condition)
        self.textbox_internal_clock_value_var.set('100000')
        self.textbox_internal_clock_value = tk.Entry(self.groupbox_sampling_condition, textvariable=self.textbox_internal_clock_value_var, justify=tk.RIGHT)
        self.textbox_internal_clock_value.place(x=485, y=35, width=55, height=20)
        #--------------------
        # Create Label for ns
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='ns', anchor='nw').place(x=540, y=35, width=20, height=20)
        #--------------------
        # Create Label for Comparison Value
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='Comparison Value : ', anchor='nw').place(x=365, y=65, width=135, height=20)
        #--------------------
        # Create Entry for Comparison Value
        #--------------------
        self.textbox_stop_compare_value_var = tk.StringVar(self.groupbox_sampling_condition)
        self.textbox_stop_compare_value_var.set('200')
        self.textbox_stop_compare_value = tk.Entry(self.groupbox_sampling_condition, textvariable=self.textbox_stop_compare_value_var, justify=tk.RIGHT)
        self.textbox_stop_compare_value.place(x=485, y=65, width=55, height=20)
        #--------------------
        # Create Label for Hex
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='Hex', anchor='nw').place(x=540, y=65, width=20, height=20)
        #--------------------
        # Create Label for Stop Number
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='Stop Number : ', anchor='nw').place(x=365, y=95, width=135, height=20)
        #--------------------
        # Create Entry for Stop Number
        #--------------------
        self.textbox_stop_number_value_var = tk.StringVar(self.groupbox_sampling_condition)
        self.textbox_stop_number_value_var.set('1000')
        self.textbox_stop_number_value = tk.Entry(self.groupbox_sampling_condition, textvariable=self.textbox_stop_number_value_var, justify=tk.RIGHT)
        self.textbox_stop_number_value.place(x=485, y=95, width=55, height=20)
        #----------------------------------------
        # Create LabelFrame for Counter Start Mode
        #----------------------------------------
        self.groupbox_counter_start_mode = tk.LabelFrame(self.condition_window, text="Counter Start Mode")
        self.groupbox_counter_start_mode.place(x=10, y=160, width=570, height=60)
        #--------------------
        # Create Radiobutton for Counter Start Mode
        #--------------------
        self.radiobutton_counter_start_mode = tk.IntVar()
        self.radiobutton_counter_start_mode.set(1)
        self.radiobutton_counter_start_unrelated = tk.Radiobutton(self.groupbox_counter_start_mode, justify=tk.LEFT, text="Start counter regardless of sampling", variable=self.radiobutton_counter_start_mode, value=1)
        self.radiobutton_counter_start_unrelated.place(x=5, y=0,height=20)
        self.radiobutton_counter_start_same_as = tk.Radiobutton(self.groupbox_counter_start_mode, justify=tk.LEFT, text="Start counter when sampling starts", variable=self.radiobutton_counter_start_mode, value=2)
        self.radiobutton_counter_start_same_as.place(x=5, y=20,height=20)
        #----------------------------------------
        # Create LabelFrame for Counter Counter Stop Mode
        #----------------------------------------
        self.groupbox_counter_stop_mode = tk.LabelFrame(self.condition_window, text="Counter Stop Mode")
        self.groupbox_counter_stop_mode.place(x=10, y=230, width=570, height=60)
        #--------------------
        # Create Radiobutton for Counter Stop Mode
        #--------------------
        self.radiobutton_counter_stop_mode = tk.IntVar()
        self.radiobutton_counter_stop_mode.set(1)
        self.radiobutton_counter_stop_unrelated = tk.Radiobutton(self.groupbox_counter_stop_mode, justify=tk.LEFT, text="Stop counter regardless of sampling", variable=self.radiobutton_counter_stop_mode, value=1)
        self.radiobutton_counter_stop_unrelated.place(x=5, y=0,height=20)
        self.radiobutton_counter_stop_same_as = tk.Radiobutton(self.groupbox_counter_stop_mode, justify=tk.LEFT, text="Stop counter when sampling stops", variable=self.radiobutton_counter_stop_mode, value=2)
        self.radiobutton_counter_stop_same_as.place(x=5, y=20,height=20)
        #----------------------------------------
        # Create LabelFrame for Transfer Number Event Notification
        #----------------------------------------
        self.groupbox_transfer_event = tk.LabelFrame(self.condition_window, text="Transfer Number Event Notification")
        self.groupbox_transfer_event.place(x=10, y=310, width=570, height=50)
        #--------------------
        # Create Entry for Transfer Number Event Notification
        #--------------------
        self.textbox_transfer_event_var = tk.StringVar(self.groupbox_sampling_condition)
        self.textbox_transfer_event_var.set('500')
        self.textbox_transfer_event = tk.Entry(self.groupbox_transfer_event, textvariable=self.textbox_transfer_event_var, justify=tk.RIGHT)
        self.textbox_transfer_event.place(x=30, y=5, width=100, height=20)
        #--------------------
        # Create Label for Each data notification
        #--------------------
        tk.Label(self.groupbox_transfer_event, text='Each data notification ', anchor='nw').place(x=135, y=5, width=140, height=20)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Button for OK
        #--------------------
        self.button_condition_window_ok = tk.Button(self.condition_window, text='OK')
        self.button_condition_window_ok.place(x=430, y=370, width=70, height=20)
        #--------------------
        # Create Button for Cancel
        #--------------------
        self.button_condition_window_cancel = tk.Button(self.condition_window, text='Cancel')
        self.button_condition_window_cancel.place(x=510, y=370, width=70, height=20)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_select_start_condition,
                       command_select_clock_condition,
                       command_select_stop_condition,
                       command_condition_window_ok,
                       command_condition_window_cancel):
        #----------------------------------------
        # Set the callback function for the start condition combobox
        #----------------------------------------
        self.combobox_start_condition.bind("<<ComboboxSelected>>", command_select_start_condition)
        #----------------------------------------
        # Set the callback function for the clock condition combobox
        #----------------------------------------
        self.combobox_clock_condition.bind("<<ComboboxSelected>>", command_select_clock_condition)
        #----------------------------------------
        # Set the callback function for the stop condition combobox
        #----------------------------------------
        self.combobox_stop_condition.bind("<<ComboboxSelected>>", command_select_stop_condition)
        #----------------------------------------
        # Set the callback function for the [OK] button
        #----------------------------------------
        self.button_condition_window_ok.configure(command=command_condition_window_ok)
        #----------------------------------------
        # Set the callback function for the [Cancel] button
        #----------------------------------------
        self.button_condition_window_cancel.configure(command=command_condition_window_cancel)
 
    #========================================
    # Display the error in a message box
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.condition_window)