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


#========================================
# MSamplingModeWindowUI Class
#========================================
class MSamplingModeWindowUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # Set the parent window
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # Create the master condition setting window
        #----------------------------------------
        self.m_sampling_mode_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # Set the modal dialog
        #----------------------------------------
        self.m_sampling_mode_window.grab_set()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.m_sampling_mode_window.title("Sampling Condition Setting for Master")
        self.m_sampling_mode_window.geometry("690x300")
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Create LabelFrame for Sampling Condition
        #----------------------------------------
        self.groupbox_master_sampling_mode = tk.LabelFrame(self.m_sampling_mode_window, text="Sampling Condition Setting for Master")
        self.groupbox_master_sampling_mode.place(x=10, y=10, width=670, height=180)
        #--------------------
        # Create Label for Start
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Start : ', anchor='nw').place(x=5, y=5, width=150, height=20)
        #--------------------
        # Create Label for ->ExtSig2
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='->ExtSig2', anchor='nw').place(x=405, y=5, width=80, height=20)
        #--------------------
        # Create Combobox for Start
        #--------------------
        self.combobox_start_condition = ttk.Combobox(self.groupbox_master_sampling_mode)
        self.combobox_start_condition.place(x=160, 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 CH4 Compare-register 0', 'Count-match CH5 Compare-register 0', 
                                                  'Count-match CH6 Compare-register 0', 'Count-match CH7 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', 
                                                  'Count-match CH4 Compare-register 1', 'Count-match CH5 Compare-register 1', 
                                                  'Count-match CH6 Compare-register 1', 'Count-match CH7 Compare-register 1')
        self.combobox_start_condition.current(0)
        self.combobox_start_condition['state'] = 'readonly'
        #--------------------
        # Create Label for Counter Start Mode
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Counter Start Mode : ', anchor='nw').place(x=5, y=35, width=150, height=20)
        #--------------------
        # Create Combobox for Counter Start Mode
        #--------------------
        self.combobox_start_with = ttk.Combobox(self.groupbox_master_sampling_mode)
        self.combobox_start_with.place(x=160, y=35, width=240, height=20)
        self.combobox_start_with['value'] = ('Start counter regardless of sampling', 'Start counter when sampling starts')
        self.combobox_start_with.current(0)
        self.combobox_start_with['state'] = 'readonly'
        #--------------------
        # Create Label for Counter Clock
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Clock : ', anchor='nw').place(x=5, y=65, width=150, height=20)
        #--------------------
        # Create Label for ->ExtSig1
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='->ExtSig1', anchor='nw').place(x=405, y=65, width=80, height=20)
        #--------------------
        # Create Combobox for Counter Clock
        #--------------------
        self.combobox_clock_condition = ttk.Combobox(self.groupbox_master_sampling_mode)
        self.combobox_clock_condition.place(x=160, y=65, 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
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Stop : ', anchor='nw').place(x=5, y=95, width=150, height=20)
        #--------------------
        # Create Label for ->ExtSig3
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='->ExtSig3', anchor='nw').place(x=405, y=95, width=80, height=20)
        #--------------------
        # Create Combobox for Stop
        #--------------------
        self.combobox_stop_condition = ttk.Combobox(self.groupbox_master_sampling_mode)
        self.combobox_stop_condition.place(x=160, y=95, 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 CH4 Compare-register 0', 'Count-match CH5 Compare-register 0', 
                                                 'Count-match CH6 Compare-register 0', 'Count-match CH7 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', 
                                                 'Count-match CH4 Compare-register 1', 'Count-match CH5 Compare-register 1', 
                                                 'Count-match CH6 Compare-register 1', 'Count-match CH7 Compare-register 1')
        self.combobox_stop_condition.current(0)
        self.combobox_stop_condition['state'] = 'readonly'
        #--------------------
        # Create Label for Counter Stop Mode
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Counter Stop Mode : ', anchor='nw').place(x=5, y=125, width=150, height=20)
        #--------------------
        # Create Combobox for Counter Stop Mode
        #--------------------
        self.combobox_stop_with = ttk.Combobox(self.groupbox_master_sampling_mode)
        self.combobox_stop_with.place(x=160, y=125, width=240, height=20)
        self.combobox_stop_with['value'] = ('Stop counter regardless of sampling', 'Stop counter when sampling stops')
        self.combobox_stop_with.current(0)
        self.combobox_stop_with['state'] = 'readonly'
        #--------------------
        # Create Label for Compare Value
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Compare Value : ', anchor='nw').place(x=475, y=5, width=105, height=20)
        #--------------------
        # Create Entry for Compare Value
        #--------------------
        self.textbox_start_compare_value_var = tk.StringVar(self.groupbox_master_sampling_mode)
        self.textbox_start_compare_value_var.set('100')
        self.textbox_start_compare_value = tk.Entry(self.groupbox_master_sampling_mode, textvariable=self.textbox_start_compare_value_var, justify=tk.RIGHT)
        self.textbox_start_compare_value.place(x=585, y=5, width=55, height=20)
        #--------------------
        # Create Label for Hex
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Hex', anchor='nw').place(x=640, y=5, width=20, height=20)
        #--------------------
        # Create Label for Internal Clock
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Internal Clock : ', anchor='nw').place(x=475, y=65, width=105, height=20)
        #--------------------
        # Create Entry for Internal Clock
        #--------------------
        self.textbox_internal_clock_value_var = tk.StringVar(self.groupbox_master_sampling_mode)
        self.textbox_internal_clock_value_var.set('1000')
        self.textbox_internal_clock_value = tk.Entry(self.groupbox_master_sampling_mode, textvariable=self.textbox_internal_clock_value_var, justify=tk.RIGHT)
        self.textbox_internal_clock_value.place(x=585, y=65, width=55, height=20)
        #--------------------
        # Create Label for ns
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='ns', anchor='nw').place(x=640, y=65, width=20, height=20)
        #--------------------
        # Create Label for Compare Value
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Compare Value : ', anchor='nw').place(x=475, y=95, width=105, height=20)
        #--------------------
        # Create Entry for Compare Value
        #--------------------
        self.textbox_stop_compare_value_var = tk.StringVar(self.groupbox_master_sampling_mode)
        self.textbox_stop_compare_value_var.set('200')
        self.textbox_stop_compare_value = tk.Entry(self.groupbox_master_sampling_mode, textvariable=self.textbox_stop_compare_value_var, justify=tk.RIGHT)
        self.textbox_stop_compare_value.place(x=585, y=95, width=55, height=20)
        #--------------------
        # Create Label for Hex
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Hex', anchor='nw').place(x=640, y=95, width=20, height=20)
        #--------------------
        # Create Label for Stop Number
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Stop Number : ', anchor='nw').place(x=475, y=125, width=105, height=20)
        #--------------------
        # Create Entry for Stop Number
        #--------------------
        self.textbox_stop_number_value_var = tk.StringVar(self.groupbox_master_sampling_mode)
        self.textbox_stop_number_value_var.set('1000')
        self.textbox_stop_number_value = tk.Entry(self.groupbox_master_sampling_mode, textvariable=self.textbox_stop_number_value_var, justify=tk.RIGHT)
        self.textbox_stop_number_value.place(x=585, y=125, width=55, height=20)
        #----------------------------------------
        # Create LabelFrame for Transfer Number Event Notification
        #----------------------------------------
        self.groupbox_transfer_event = tk.LabelFrame(self.m_sampling_mode_window, text="Transfer Number Event Notification")
        self.groupbox_transfer_event.place(x=10, y=200, width=330, height=50)
        #--------------------
        # Create Entry for Transfer Number Event Notification
        #--------------------
        self.textbox_transfer_event_var = tk.StringVar(self.groupbox_transfer_event)
        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=150, y=5, width=100, height=20)
        #--------------------
        # Create Label for Number of data
        #--------------------
        tk.Label(self.groupbox_transfer_event, text='Number of data : ', anchor='nw').place(x=10, y=5, width=100, height=20)
        #----------------------------------------
        # Create LabelFrame for Transfer Mode
        #----------------------------------------
        self.groupbox_transfer_type = tk.LabelFrame(self.m_sampling_mode_window, text="Transfer Mode")
        self.groupbox_transfer_type.place(x=350, y=200, width=330, height=50)
        #--------------------
        # Create Radiobutton for Transfer Mode
        #--------------------
        self.radiobutton_transfer_type = tk.IntVar()
        self.radiobutton_transfer_type.set(1)
        self.radiobutton_transfer_once = tk.Radiobutton(self.groupbox_transfer_type, justify=tk.LEFT, text="Once", variable=self.radiobutton_transfer_type, value=1)
        self.radiobutton_transfer_once.place(x=5, y=5,height=20)
        self.radiobutton_transfer_infinite = tk.Radiobutton(self.groupbox_transfer_type, justify=tk.LEFT, text="Ring", variable=self.radiobutton_transfer_type, value=2)
        self.radiobutton_transfer_infinite.place(x=150, y=5,height=20)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Button for OK
        #--------------------
        self.button_m_sampling_mode_window_ok = tk.Button(self.m_sampling_mode_window, text='OK')
        self.button_m_sampling_mode_window_ok.place(x=510, y=270, width=70, height=20)
        #--------------------
        # Create Button for Cancel
        #--------------------
        self.button_m_sampling_mode_window_cancel = tk.Button(self.m_sampling_mode_window, text='Cancel')
        self.button_m_sampling_mode_window_cancel.place(x=590, y=270, 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_m_sampling_mode_window_ok,
                       command_m_sampling_mode_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_m_sampling_mode_window_ok.configure(command=command_m_sampling_mode_window_ok)
        #----------------------------------------
        # Set the callback function for the [Cancel] button
        #----------------------------------------
        self.button_m_sampling_mode_window_cancel.configure(command=command_m_sampling_mode_window_cancel)
 
    #========================================
    # Display the error in a message box
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.m_sampling_mode_window)