#================================================================
#================================================================
# API-CNT(WDM)
# Counter Sample
# Comparison setting UI file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# CompareWindowUI Class
#========================================
class CompareWindowUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # Set the parent window
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # Create the compare setting window
        #----------------------------------------
        self.compare_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # Set the modal dialog
        #----------------------------------------
        self.compare_window.grab_set()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.compare_window.title("Comparison Set")
        self.compare_window.geometry("270x400")
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Create LabelFrame for Comparison register set
        #----------------------------------------
        self.groupbox_count_match = tk.LabelFrame(self.compare_window, text="Comparison register set")
        self.groupbox_count_match.place(x=10, y=10, width=250, height=260)
        #--------------------
        # Create Radiobutton for Comparison register set
        #--------------------
        self.radiobutton_counter_value_base_var = tk.IntVar()
        self.radiobutton_counter_value_base_var.set(1)
        self.radiobutton_count_value_dec = tk.Radiobutton(self.groupbox_count_match, text="Dec", variable=self.radiobutton_counter_value_base_var, value=1)
        self.radiobutton_count_value_dec.place(x=90, y=0, width=60, height=20)
        self.radiobutton_count_value_hex = tk.Radiobutton(self.groupbox_count_match, text="Hex", variable=self.radiobutton_counter_value_base_var, value=2)
        self.radiobutton_count_value_hex.place(x=90, y=20, width=60, height=20)
        #--------------------
        # Create Label for Channel0 to Channel7
        #--------------------
        tk.Label(self.groupbox_count_match, text='Channel0', anchor='nw').place(x=35, y=45, width=50, height=20)
        tk.Label(self.groupbox_count_match, text='Channel1', anchor='nw').place(x=35, y=70, width=50, height=20)
        tk.Label(self.groupbox_count_match, text='Channel2', anchor='nw').place(x=35, y=95, width=50, height=20)
        tk.Label(self.groupbox_count_match, text='Channel3', anchor='nw').place(x=35, y=120, width=50, height=20)
        tk.Label(self.groupbox_count_match, text='Channel4', anchor='nw').place(x=35, y=145, width=50, height=20)
        tk.Label(self.groupbox_count_match, text='Channel5', anchor='nw').place(x=35, y=170, width=50, height=20)
        tk.Label(self.groupbox_count_match, text='Channel6', anchor='nw').place(x=35, y=195, width=50, height=20)
        tk.Label(self.groupbox_count_match, text='Channel7', anchor='nw').place(x=35, y=220, width=50, height=20)
        #--------------------
        # Create Entry for Channel0 to Channel7
        #--------------------
        self.textbox_ch0_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch0_value_var.set('100')
        self.textbox_ch0_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch0_value_var, justify=tk.RIGHT)
        self.textbox_ch0_value.place(x=90, y=45, width=90, height=20)
        self.textbox_ch1_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch1_value_var.set('150')
        self.textbox_ch1_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch1_value_var, justify=tk.RIGHT)
        self.textbox_ch1_value.place(x=90, y=70, width=90, height=20)
        self.textbox_ch2_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch2_value_var.set('200')
        self.textbox_ch2_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch2_value_var, justify=tk.RIGHT)
        self.textbox_ch2_value.place(x=90, y=95, width=90, height=20)
        self.textbox_ch3_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch3_value_var.set('250')
        self.textbox_ch3_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch3_value_var, justify=tk.RIGHT)
        self.textbox_ch3_value.place(x=90, y=120, width=90, height=20)
        self.textbox_ch4_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch4_value_var.set('300')
        self.textbox_ch4_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch4_value_var, justify=tk.RIGHT)
        self.textbox_ch4_value.place(x=90, y=145, width=90, height=20)
        self.textbox_ch5_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch5_value_var.set('350')
        self.textbox_ch5_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch5_value_var, justify=tk.RIGHT)
        self.textbox_ch5_value.place(x=90, y=170, width=90, height=20)
        self.textbox_ch6_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch6_value_var.set('400')
        self.textbox_ch6_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch6_value_var, justify=tk.RIGHT)
        self.textbox_ch6_value.place(x=90, y=195, width=90, height=20)
        self.textbox_ch7_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch7_value_var.set('450')
        self.textbox_ch7_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch7_value_var, justify=tk.RIGHT)
        self.textbox_ch7_value.place(x=90, y=220, width=90, height=20)
        #----------------------------------------
        # Create LabelFrame for Pulse set consistent with count
        #----------------------------------------
        self.groupbox_count_match_pulse = tk.LabelFrame(self.compare_window, text="Pulse set consistent with count")
        self.groupbox_count_match_pulse.place(x=10, y=280, width=250, height=60)
        #--------------------
        # Create Label for Pulse width
        #--------------------
        tk.Label(self.groupbox_count_match_pulse, text='Pulse width : ', anchor='nw').place(x=5, y=10, width=80, height=20)
        #--------------------
        # Create Combobox for Pulse width
        #--------------------
        self.combobox_count_match_pulse = ttk.Combobox(self.groupbox_count_match_pulse)
        self.combobox_count_match_pulse.place(x=90, y=10, width=90, height=20)
        self.combobox_count_match_pulse['value'] = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
        self.combobox_count_match_pulse.current(0)
        self.combobox_count_match_pulse['state'] = 'readonly'
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Button for OK
        #--------------------
        self.button_compare_window_ok = tk.Button(self.compare_window, text='OK')
        self.button_compare_window_ok.place(x=110, y=360, width=70, height=20)
        #--------------------
        # Create Button for Cancel
        #--------------------
        self.button_compare_window_cancel = tk.Button(self.compare_window, text='Cancel')
        self.button_compare_window_cancel.place(x=190, y=360, width=70, height=20)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_count_value_dec,
                       command_count_value_hex,
                       command_compare_window_ok,
                       command_compare_window_cancel):
        #----------------------------------------
        # Set the callback function for the [Dec] radiobutton
        #----------------------------------------
        self.radiobutton_count_value_dec.configure(command=command_count_value_dec)
        #----------------------------------------
        # Set the callback function for the [Hex] radiobutton
        #----------------------------------------
        self.radiobutton_count_value_hex.configure(command=command_count_value_hex)
        #----------------------------------------
        # Set the callback function for the [OK] button
        #----------------------------------------
        self.button_compare_window_ok.configure(command=command_compare_window_ok)
        #----------------------------------------
        # Set the callback function for the [Cancel] button
        #----------------------------------------
        self.button_compare_window_cancel.configure(command=command_compare_window_cancel)
 
    #========================================
    # Display the error in a message box
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.compare_window)