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


#========================================
# ModeWindowUI Class
#========================================
class ModeWindowUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # Set the parent window
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # Create the mode setting window
        #----------------------------------------
        self.mode_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # Set the modal dialog
        #----------------------------------------
        self.mode_window.grab_set()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.mode_window.title("Set Mode")
        self.mode_window.geometry("400x310")
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Create LabelFrame for Counter Mode
        #----------------------------------------
        self.groupbox_mode_setting = tk.LabelFrame(self.mode_window, text="Counter Mode")
        self.groupbox_mode_setting.place(x=10, y=10, width=380, height=240)
        #--------------------
        # Create Label for Z Phase Mode
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='Z Phase Mode : ', anchor='nw').place(x=15, y=15, width=90, height=20)
        #--------------------
        # Create Combobox for Z Phase Mode
        #--------------------
        self.combobox_z_phase_mode = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_z_phase_mode.place(x=140, y=15, width=200, height=20)
        self.combobox_z_phase_mode['value'] = ('Not Use', 'Next Time', 'Every Time')
        self.combobox_z_phase_mode.current(0)
        self.combobox_z_phase_mode['state'] = 'readonly'
        #--------------------
        # Create Label for Z Phase Logic
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='Z Phase Logic : ', anchor='nw').place(x=15, y=40, width=90, height=20)
        #--------------------
        # Create Combobox for Z Phase Logic
        #--------------------
        self.combobox_z_logic = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_z_logic.place(x=140, y=40, width=200, height=20)
        self.combobox_z_logic['value'] = ('Positive Logic', 'Negative Logic')
        self.combobox_z_logic.current(1)
        self.combobox_z_logic['state'] = 'readonly'
        #--------------------
        # Create Label for Signal Source
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='Signal Source : ', anchor='nw').place(x=15, y=65, width=90, height=20)
        #--------------------
        # Create Combobox for Signal Source
        #--------------------
        self.combobox_signal_source = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_signal_source.place(x=140, y=65, width=200, height=20)
        self.combobox_signal_source['value'] = ('Photo-coupler insulation', 'TTL', 'Differential')
        self.combobox_signal_source.current(2)
        self.combobox_signal_source['state'] = 'readonly'
        #--------------------
        # Create Label for Direction
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='Direction : ', anchor='nw').place(x=15, y=90, width=90, height=20)
        #--------------------
        # Create Combobox for Direction
        #--------------------
        self.combobox_count_direction = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_count_direction.place(x=140, y=90, width=200, height=20)
        self.combobox_count_direction['value'] = ('Down Count', 'Up Count')
        self.combobox_count_direction.current(1)
        self.combobox_count_direction['state'] = 'readonly'
        #--------------------
        # Create Label for Digital Filter
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='Digital Filter : ', anchor='nw').place(x=15, y=115, width=150, height=20)
        #--------------------
        # Create Combobox for Digital Filter
        #--------------------
        self.combobox_filter = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_filter.place(x=140, y=115, width=200, height=20)
        self.combobox_filter['value'] = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
        self.combobox_filter.current(0)
        self.combobox_filter['state'] = 'readonly'
        #--------------------
        # Create Label for Phase number
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='Phase number : ', anchor='nw').place(x=15, y=140, width=90, height=20)
        #--------------------
        # Create Combobox for Phase number
        #--------------------
        self.combobox_phase = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_phase.place(x=140, y=140, width=200, height=20)
        self.combobox_phase['value'] = ('1 Phase', '2 Phase', 'Gate Control')
        self.combobox_phase.current(1)
        self.combobox_phase['state'] = 'readonly'
        #--------------------
        # Create Label for Multiple
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='Multiple : ', anchor='nw').place(x=15, y=165, width=90, height=20)
        #--------------------
        # Create Combobox for Multiple
        #--------------------
        self.combobox_mul = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_mul.place(x=140, y=165, width=200, height=20)
        self.combobox_mul['value'] = ('x1', 'x2', 'x4')
        self.combobox_mul.current(1)
        self.combobox_mul['state'] = 'readonly'
        #--------------------
        # Create Label for Syn clear
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='Syn clear : ', anchor='nw').place(x=15, y=190, width=90, height=20)
        #--------------------
        # Create Combobox Syn clear
        #--------------------
        self.combobox_sync_clear = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_sync_clear.place(x=140, y=190, width=200, height=20)
        self.combobox_sync_clear['value'] = ('Asynchronous', 'Synchronous')
        self.combobox_sync_clear.current(1)
        self.combobox_sync_clear['state'] = 'readonly'
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for Note : All channel same mode
        #--------------------
        tk.Label(self.mode_window, text='Note : All channel same mode.', anchor='nw').place(x=20, y=250, width=300, height=20)
        #--------------------
        # Create Button for OK
        #--------------------
        self.button_mode_window_ok = tk.Button(self.mode_window, text='OK')
        self.button_mode_window_ok.place(x=220, y=275, width=70, height=20)
        #--------------------
        # Create Button for Cancel
        #--------------------
        self.button_mode_window_cancel = tk.Button(self.mode_window, text='Cancel')
        self.button_mode_window_cancel.place(x=300, y=275, width=70, height=20)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_mode_window_ok,
                       command_mode_window_cancel):
        #----------------------------------------
        # Set the callback function for the [OK] button
        #----------------------------------------
        self.button_mode_window_ok.configure(command=command_mode_window_ok)
        #----------------------------------------
        # Set the callback function for the [Cancel] button
        #----------------------------------------
        self.button_mode_window_cancel.configure(command=command_mode_window_cancel)
 
    #========================================
    # Display the error in a message box
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.mode_window)