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


#========================================
# EventWindowUI Class
#========================================
class EventWindowUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # Set the parent window
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # Create the event setting window
        #----------------------------------------
        self.event_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # Set the modal dialog
        #----------------------------------------
        self.event_window.grab_set()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.event_window.title("Hardware Event Setting")
        self.event_window.geometry("740x280")
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Create LabelFrame for Event signal input
        #----------------------------------------
        self.groupbox_input_event = tk.LabelFrame(self.event_window, text="Event Signal Input")
        self.groupbox_input_event.place(x=10, y=10, width=240, height=260)
        #--------------------
        # Create Label for Type
        #--------------------
        tk.Label(self.groupbox_input_event, text='Type : ', anchor='nw').place(x=5, y=5, width=50, height=20)
        #--------------------
        # Create Combobox for Type
        #--------------------
        self.combobox_input_type = ttk.Combobox(self.groupbox_input_event)
        self.combobox_input_type.place(x=58, y=5, width=150, height=20)
        self.combobox_input_type['value'] = ('General Input', 'Counter Start/Stop', 'Counter Preset', 'Counter Clear')
        self.combobox_input_type.current(0)
        self.combobox_input_type['state'] = 'readonly'
        #--------------------
        # Create LabelFrame for RF0 (Clear/Preset/Start)
        #--------------------
        self.groupbox_input_event_rf0 = tk.LabelFrame(self.groupbox_input_event, text="RF0 (Clear/Preset/Start)")
        self.groupbox_input_event_rf0.place(x=5, y=30, width=227, height=80)
        #--------------------
        # Create Combobox for RF0 (Clear/Preset/Start)
        #--------------------
        self.combobox_input_event_rf0 = ttk.Combobox(self.groupbox_input_event_rf0)
        self.combobox_input_event_rf0.place(x=15, y=5, width=185, height=20)
        self.combobox_input_event_rf0['value'] = ('Rise', 'Fall', 'None')
        self.combobox_input_event_rf0.current(2)
        self.combobox_input_event_rf0['state'] = 'readonly'
        #--------------------
        # Create Label for Preset Register
        #--------------------
        tk.Label(self.groupbox_input_event_rf0, text='Preset Register : ', anchor='nw').place(x=5, y=30, width=130, height=20)
        #--------------------
        # Create Entry for Preset Register
        #--------------------
        self.textbox_input_preset_register_value_var = tk.StringVar(self.groupbox_input_event_rf0)
        self.textbox_input_preset_register_value_var.set('1000')
        self.textbox_input_preset_register_value = tk.Entry(self.groupbox_input_event_rf0, textvariable=self.textbox_input_preset_register_value_var, justify=tk.RIGHT)
        self.textbox_input_preset_register_value.place(x=140, y=30, width=55, height=20)
        #--------------------
        # Create Label for Hex
        #--------------------
        tk.Label(self.groupbox_input_event_rf0, text='Hex', anchor='nw').place(x=195, y=30, width=25, height=20)
        #--------------------
        # Create LabelFrame for RF1 (Stop)
        #--------------------
        self.groupbox_input_event_rf1 = tk.LabelFrame(self.groupbox_input_event, text="RF1 (Stop)")
        self.groupbox_input_event_rf1.place(x=5, y=120, width=227, height=50)
        #--------------------
        # Create Combobox for RF1 (Stop)
        #--------------------
        self.combobox_input_event_rf1 = ttk.Combobox(self.groupbox_input_event_rf1)
        self.combobox_input_event_rf1.place(x=15, y=5, width=185, height=20)
        self.combobox_input_event_rf1['value'] = ('Rise', 'Fall', 'None')
        self.combobox_input_event_rf1.current(2)
        self.combobox_input_event_rf1['state'] = 'readonly'
        #----------------------------------------
        # Create LabelFrame for Event signal output
        #----------------------------------------
        self.groupbox_output_event = tk.LabelFrame(self.event_window, text="Event Signal Output")
        self.groupbox_output_event.place(x=255, y=10, width=260, height=260)
        #--------------------
        # Create LabelFrame for Type
        #--------------------
        self.groupbox_output_event_type = tk.LabelFrame(self.groupbox_output_event, text="Type : ")
        self.groupbox_output_event_type.place(x=5, y=5, width=247, height=140)
        #--------------------
        # Create Radiobutton for Event Signal Output Type
        #--------------------
        self.radiobutton_output_event_type_var = tk.IntVar()
        self.radiobutton_output_event_type_var.set(1)
        self.radiobutton_general_output = tk.Radiobutton(self.groupbox_output_event_type, justify=tk.LEFT, text="General Output", variable=self.radiobutton_output_event_type_var, value=1)
        self.radiobutton_general_output.place(x=5, y=0,height=20)
        self.radiobutton_count_match_reg0 = tk.Radiobutton(self.groupbox_output_event_type, justify=tk.LEFT, text="Count Match REG0", variable=self.radiobutton_output_event_type_var, value=2)
        self.radiobutton_count_match_reg0.place(x=5, y=20, height=20)
        self.radiobutton_count_match_reg1 = tk.Radiobutton(self.groupbox_output_event_type, justify=tk.LEFT, text="Count Match REG1", variable=self.radiobutton_output_event_type_var, value=3)
        self.radiobutton_count_match_reg1.place(x=5, y=40, height=20)
        self.radiobutton_abnormal_input_error = tk.Radiobutton(self.groupbox_output_event_type, justify=tk.LEFT, text="Abnormal Input Error", variable=self.radiobutton_output_event_type_var, value=4)
        self.radiobutton_abnormal_input_error.place(x=5, y=60, height=20)
        self.radiobutton_filter_error = tk.Radiobutton(self.groupbox_output_event_type, justify=tk.LEFT, text="Digital Filter Error", variable=self.radiobutton_output_event_type_var, value=5)
        self.radiobutton_filter_error.place(x=5, y=80, height=20)
        self.radiobutton_alarm_error = tk.Radiobutton(self.groupbox_output_event_type, justify=tk.LEFT, text="Alarm Error", variable=self.radiobutton_output_event_type_var, value=6)
        self.radiobutton_alarm_error.place(x=5, y=100, height=20)
        #--------------------
        # Create Label for Pulse Width
        #--------------------
        tk.Label(self.groupbox_output_event, text='Pulse Width (Coefficient): ', anchor='nw').place(x=5, y=150, width=160, height=20)
        #--------------------
        # Create Combobox for Pulse Width
        #--------------------
        self.combobox_output_pulse_width = ttk.Combobox(self.groupbox_output_event)
        self.combobox_output_pulse_width.place(x=170, y=150, width=60, height=20)
        self.combobox_output_pulse_width['value'] = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
        self.combobox_output_pulse_width.current(0)
        self.combobox_output_pulse_width['state'] = 'readonly'
        #--------------------
        # Create Label for Hex
        #--------------------
        tk.Label(self.groupbox_output_event, text='Hex', anchor='nw').place(x=230, y=150, width=25, height=20)
        #--------------------
        # Create LabelFrame for General Output
        #--------------------
        self.groupbox_general_output = tk.LabelFrame(self.groupbox_output_event, text="General Output")
        self.groupbox_general_output.place(x=5, y=175, width=247, height=65)
        #--------------------
        # Create Label for Channel No.
        #--------------------
        tk.Label(self.groupbox_general_output, text='Channel No. : ', anchor='nw').place(x=5, y=0, height=20)
        #--------------------
        # Create Entry for Channel No.
        #--------------------
        self.textbox_output_preset_register_value_ch_var = tk.StringVar(self.groupbox_general_output)
        self.textbox_output_preset_register_value_ch_var.set('0')
        self.textbox_output_preset_register_value_ch = tk.Entry(self.groupbox_general_output, textvariable=self.textbox_output_preset_register_value_ch_var, justify=tk.RIGHT)
        self.textbox_output_preset_register_value_ch.place(x=95, y=0, width=40, height=20)
        #--------------------
        # Create Label for Data
        #--------------------
        tk.Label(self.groupbox_general_output, text='Data : ', anchor='nw').place(x=5, y=25, height=20)
        #--------------------
        # Create Entry for Data
        #--------------------
        self.textbox_output_preset_register_value_data_var = tk.StringVar(self.groupbox_general_output)
        self.textbox_output_preset_register_value_data_var.set('1')
        self.textbox_output_preset_register_value_data = tk.Entry(self.groupbox_general_output, textvariable=self.textbox_output_preset_register_value_data_var, justify=tk.RIGHT)
        self.textbox_output_preset_register_value_data.place(x=95, y=25, width=40, height=20)
        #--------------------
        # Create Button for Signal Output
        #--------------------
        self.button_signal_output = tk.Button(self.groupbox_general_output, text='Signal Output')
        self.button_signal_output.place(x=145, y=15, width=90, height=20)
        #----------------------------------------
        # Create LabelFrame for Count match event
        #----------------------------------------
        self.groupbox_count_match_event = tk.LabelFrame(self.event_window, text="Count Match Event")
        self.groupbox_count_match_event.place(x=520, y=10, width=210, height=70)
        #--------------------
        # Create Label for Type
        #--------------------
        tk.Label(self.groupbox_count_match_event, text='Type : ', anchor='nw').place(x=15, y=15, height=20)
        #--------------------
        # Create Combobox for Type
        #--------------------
        self.combobox_count_match_type = ttk.Combobox(self.groupbox_count_match_event)
        self.combobox_count_match_type.place(x=68, y=15, width=100, height=20)
        self.combobox_count_match_type['value'] = ('None', 'Preset', 'Clear')
        self.combobox_count_match_type.current(0)
        self.combobox_count_match_type['state'] = 'readonly'
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for All channels use the same setting
        #--------------------
        tk.Label(self.event_window, justify=tk.LEFT, text='All channels use the same setting.', anchor='nw').place(x=520, y=90, height=40)
        #--------------------
        # Create Label for The count match register is set in\nthe Notification Setting dialog
        #--------------------
        tk.Label(self.event_window, justify=tk.LEFT, text='The count match register is set in\nthe Notification Setting dialog.', anchor='nw').place(x=520, y=150, height=40)
        #--------------------
        # Create Button for OK
        #--------------------
        self.button_event_window_ok = tk.Button(self.event_window, text='OK')
        self.button_event_window_ok.place(x=560, y=250, width=70, height=20)
        #--------------------
        # Create Button for Cancel
        #--------------------
        self.button_event_window_cancel = tk.Button(self.event_window, text='Cancel')
        self.button_event_window_cancel.place(x=640, y=250, width=70, height=20)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_select_count_match_type,
                       command_select_input_type,
                       command_general_output,
                       command_count_match_reg0,
                       command_count_match_reg1,
                       command_abnormal_input_error,
                       command_filter_error,
                       command_alarm_error,
                       command_signal_output,
                       command_event_window_ok,
                       command_event_window_cancel):
        #----------------------------------------
        # Set the callback function for the count match type combobox
        #----------------------------------------
        self.combobox_count_match_type.bind("<<ComboboxSelected>>", command_select_count_match_type)
        #----------------------------------------
        # Set the callback function for the input type combobox
        #----------------------------------------
        self.combobox_input_type.bind("<<ComboboxSelected>>", command_select_input_type)
        #----------------------------------------
        # Set the callback function for the [General Output] radiobutton
        #----------------------------------------
        self.radiobutton_general_output.configure(command=command_general_output)
        #----------------------------------------
        # Set the callback function for the [Count Match REG0] radiobutton
        #----------------------------------------
        self.radiobutton_count_match_reg0.configure(command=command_count_match_reg0)
        #----------------------------------------
        # Set the callback function for the [Count Match REG1] radiobutton
        #----------------------------------------
        self.radiobutton_count_match_reg1.configure(command=command_count_match_reg1)
        #----------------------------------------
        # Set the callback function for the [Abnormal Error] radiobutton
        #----------------------------------------
        self.radiobutton_abnormal_input_error.configure(command=command_abnormal_input_error)
        #----------------------------------------
        # Set the callback function for the [Filter Error] radiobutton
        #----------------------------------------
        self.radiobutton_filter_error.configure(command=command_filter_error)
        #----------------------------------------
        # Set the callback function for the [Alarm Error] radiobutton
        #----------------------------------------
        self.radiobutton_alarm_error.configure(command=command_alarm_error)
        #----------------------------------------
        # Set the callback function for the [Signal Output] button
        #----------------------------------------
        self.button_signal_output.configure(command=command_signal_output)
        #----------------------------------------
        # Set the callback function for the [OK] button
        #----------------------------------------
        self.button_event_window_ok.configure(command=command_event_window_ok)
        #----------------------------------------
        # Set the callback function for the [Cancel] button
        #----------------------------------------
        self.button_event_window_cancel.configure(command=command_event_window_cancel)
 
    #========================================
    # Display the error in a message box
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.event_window)