#================================================================
#================================================================
# API-AIO(WDM)
# デバイスバッファを使用した無限サンプリングサンプル
# 条件設定UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# ConditionWindowUI クラス
#========================================
class ConditionWindowUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # 親ウィンドウの設定
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # 条件設定ウィンドウの作成
        #----------------------------------------
        self.condition_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # モーダルダイアログの設定
        #----------------------------------------
        self.condition_window.grab_set()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.condition_window.title("サンプリング条件設定")
        self.condition_window.geometry("875x510")
        #----------------------------------------
        # UIの作成を開始
        #----------------------------------------
        self._create_ui()

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # 開始条件のLabelFrameを作成
        #----------------------------------------
        self.groupbox_start_condition = tk.LabelFrame(self.condition_window, text="開始条件")
        self.groupbox_start_condition.place(x=15, y=10, width=842, height=155)
        #--------------------
        # 開始条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_start_condition, text='開始条件 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # 開始条件のComboboxを作成
        #--------------------
        self.combobox_start_condition = ttk.Combobox(self.groupbox_start_condition)
        self.combobox_start_condition.place(x=120, y=5, width=150, height=19)
        self.combobox_start_condition['value'] = ('ソフトウェア', '外部トリガ立ち上がり', '外部トリガ立ち下がり', 'レベル比較', 'インレンジ比較', 'アウトレンジ比較')
        self.combobox_start_condition.current(0)
        self.combobox_start_condition['state'] = 'readonly'
        #--------------------
        # レベル比較のLabelFrameを作成
        #--------------------
        self.groupbox_start_level_comparison = tk.LabelFrame(self.groupbox_start_condition, text="レベル比較")
        self.groupbox_start_level_comparison.place(x=300, y=0, width=530, height=50)
        #--------------------
        # チャネル番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_start_level_comparison, text='チャネル番号 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # チャネル番号のEntryを作成
        #--------------------
        self.textbox_start_level_channel_no_var = tk.StringVar(self.groupbox_start_level_comparison)
        self.textbox_start_level_channel_no_var.set('0')
        self.textbox_start_level_channel_no = tk.Entry(self.groupbox_start_level_comparison, justify=tk.RIGHT, textvariable=self.textbox_start_level_channel_no_var)
        self.textbox_start_level_channel_no.place(x=110, y=5, width=50, height=19)
        #--------------------
        # 比較値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_start_level_comparison, text='比較値 : ', anchor='nw').place(x=180, y=5, width=50, height=19)
        #--------------------
        # 比較値のEntryを作成
        #--------------------
        self.textbox_start_level_comparison_value_var = tk.StringVar(self.groupbox_start_level_comparison)
        self.textbox_start_level_comparison_value_var.set('1.0')
        self.textbox_start_level_comparison_value = tk.Entry(self.groupbox_start_level_comparison, justify=tk.RIGHT, textvariable=self.textbox_start_level_comparison_value_var)
        self.textbox_start_level_comparison_value.place(x=240, y=5, width=50, height=19)
        #--------------------
        # 比較値単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_start_level_comparison, text='[V]', anchor='nw').place(x=300, y=5, width=20, height=19)
        #--------------------
        # 比較方向のLabelを作成
        #--------------------
        tk.Label(self.groupbox_start_level_comparison, text='比較方向 : ', anchor='nw').place(x=340, y=5, width=60, height=19)
        #--------------------
        # 比較方向のComboboxを作成
        #--------------------
        self.combobox_start_level_comparison_direction = ttk.Combobox(self.groupbox_start_level_comparison)
        self.combobox_start_level_comparison_direction.place(x=410, y=5, width=100, height=19)
        self.combobox_start_level_comparison_direction['value'] = ('両方', '立ち上がり', '立ち下がり')
        self.combobox_start_level_comparison_direction.current(0)
        self.combobox_start_level_comparison_direction['state'] = 'readonly'
        #--------------------
        # インレンジ/アウトレンジ比較のLabelFrameを作成
        #--------------------
        self.groupbox_start_range_comparison = tk.LabelFrame(self.groupbox_start_condition, text="インレンジ/アウトレンジ比較")
        self.groupbox_start_range_comparison.place(x=300, y=55, width=530, height=75)
        #--------------------
        # チャネル番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_start_range_comparison, text='チャネル番号 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # チャネル番号のEntryを作成
        #--------------------
        self.textbox_start_range_channel_no_var = tk.StringVar(self.groupbox_start_range_comparison)
        self.textbox_start_range_channel_no_var.set('0')
        self.textbox_start_range_channel_no = tk.Entry(self.groupbox_start_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_start_range_channel_no_var)
        self.textbox_start_range_channel_no.place(x=110, y=5, width=50, height=19)
        #--------------------
        # 上限値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_start_range_comparison, text='上限値 : ', anchor='nw').place(x=180, y=5, width=50, height=19)
        #--------------------
        # 上限値のEntryを作成
        #--------------------
        self.textbox_start_range_upper_limit_value_var = tk.StringVar(self.groupbox_start_range_comparison)
        self.textbox_start_range_upper_limit_value_var.set('10.0')
        self.textbox_start_range_upper_limit_value = tk.Entry(self.groupbox_start_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_start_range_upper_limit_value_var)
        self.textbox_start_range_upper_limit_value.place(x=240, y=5, width=50, height=19)
        #--------------------
        # 上限値単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_start_range_comparison, text='[V]', anchor='nw').place(x=300, y=5, width=20, height=19)
        #--------------------
        # 下限値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_start_range_comparison, text='下限値 : ', anchor='nw').place(x=180, y=30, width=50, height=19)
        #--------------------
        # 下限値のEntryを作成
        #--------------------
        self.textbox_start_range_lower_limit_value_var = tk.StringVar(self.groupbox_start_range_comparison)
        self.textbox_start_range_lower_limit_value_var.set('1.0')
        self.textbox_start_range_lower_limit_value = tk.Entry(self.groupbox_start_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_start_range_lower_limit_value_var)
        self.textbox_start_range_lower_limit_value.place(x=240, y=30, width=50, height=19)
        #--------------------
        # 下限値単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_start_range_comparison, text='[V]', anchor='nw').place(x=300, y=30, width=20, height=19)
        #--------------------
        # 保持回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_start_range_comparison, text='保持回数 : ', anchor='nw').place(x=340, y=5, width=60, height=19)
        #--------------------
        # 保持回数のEntryを作成
        #--------------------
        self.textbox_start_range_state_times_var = tk.StringVar(self.groupbox_start_range_comparison)
        self.textbox_start_range_state_times_var.set('1')
        self.textbox_start_range_state_times = tk.Entry(self.groupbox_start_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_start_range_state_times_var)
        self.textbox_start_range_state_times.place(x=410, y=5, width=50, height=19)
        #----------------------------------------
        # サンプリングクロック条件のLabelFrameを作成
        #----------------------------------------
        self.groupbox_sampling_clock_condition = tk.LabelFrame(self.condition_window, text="サンプリングクロック条件")
        self.groupbox_sampling_clock_condition.place(x=15, y=170, width=842, height=75)
        #--------------------
        # クロック種類のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_clock_condition, text='クロック種類 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # クロック種類のComboboxを作成
        #--------------------
        self.combobox_clock_type = ttk.Combobox(self.groupbox_sampling_clock_condition)
        self.combobox_clock_type.place(x=120, y=5, width=150, height=19)
        self.combobox_clock_type['value'] = ('内部クロック', '外部クロック')
        self.combobox_clock_type.current(0)
        self.combobox_clock_type['state'] = 'readonly'
        #--------------------
        # サンプリングクロック周期のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_clock_condition, text='サンプリングクロック周期 : ', anchor='nw').place(x=350, y=5, width=200, height=19)
        #--------------------
        # サンプリングクロック周期のEntryを作成
        #--------------------
        self.textbox_sampling_clock_var = tk.StringVar(self.groupbox_sampling_clock_condition)
        self.textbox_sampling_clock_var.set("1000")
        self.textbox_sampling_clock = tk.Entry(self.groupbox_sampling_clock_condition, justify=tk.RIGHT, textvariable=self.textbox_sampling_clock_var)
        self.textbox_sampling_clock.place(x=543, y=5, width=150, height=19)
        self.textbox_sampling_clock.config(state="normal")
        #--------------------
        # サンプリングクロック周期単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_clock_condition, text='[μsec]', anchor='nw').place(x=703, y=5, width=50, height=19)
        #--------------------
        # 外部クロック入力タイミングのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_clock_condition, text='外部クロック入力タイミング : ', anchor='nw').place(x=350, y=30, width=200, height=19)
        #--------------------
        # 外部クロック入力タイミングのComboboxを作成
        #--------------------
        self.combobox_clock_edge = ttk.Combobox(self.groupbox_sampling_clock_condition)
        self.combobox_clock_edge.place(x=543, y=30, width=150, height=19)
        self.combobox_clock_edge['value'] = ('立ち下がりエッジ', '立ち上がりエッジ')
        self.combobox_clock_edge.current(0)
        self.combobox_clock_edge['state'] = 'readonly'
        #----------------------------------------
        # スキャンクロック条件のLabelFrameを作成
        #----------------------------------------
        self.groupbox_scan_clock_condition = tk.LabelFrame(self.condition_window, text="スキャンクロック条件")
        self.groupbox_scan_clock_condition.place(x=15, y=250, width=842, height=50)
        #--------------------
        # 変換速度のLabelを作成
        #--------------------
        tk.Label(self.groupbox_scan_clock_condition, text='変換速度 : ', anchor='nw').place(x=15, y=5, width=200, height=19)
        #--------------------
        # 変換速度のEntryを作成
        #--------------------
        self.textbox_scan_clock_var = tk.StringVar(self.groupbox_scan_clock_condition)
        self.textbox_scan_clock_var.set("10")
        self.textbox_scan_clock = tk.Entry(self.groupbox_scan_clock_condition, justify=tk.RIGHT, textvariable=self.textbox_scan_clock_var)
        self.textbox_scan_clock.place(x=120, y=5, width=150, height=19)
        #--------------------
        # 変換速度単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_scan_clock_condition, text='[μsec]', anchor='nw').place(x=280, y=5, width=50, height=19)
        #----------------------------------------
        # 停止条件のLabelFrameを作成
        #----------------------------------------
        self.groupbox_stop_condition = tk.LabelFrame(self.condition_window, text="停止条件")
        self.groupbox_stop_condition.place(x=15, y=305, width=842, height=155)
        #--------------------
        # 停止条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stop_condition, text='停止条件 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # 停止条件のComboboxを作成
        #--------------------
        self.combobox_stop_condition = ttk.Combobox(self.groupbox_stop_condition)
        self.combobox_stop_condition.place(x=120, y=5, width=150, height=19)
        self.combobox_stop_condition['value'] = ('外部トリガ立ち上がり', '外部トリガ立ち下がり', 'レベル比較', 'コマンド（AioStopAi）', 'インレンジ比較', 'アウトレンジ比較')
        self.combobox_stop_condition.current(3)
        self.combobox_stop_condition['state'] = 'readonly'
        #--------------------
        # レベル比較のLabelFrameを作成
        #--------------------
        self.groupbox_stop_level_comparison = tk.LabelFrame(self.groupbox_stop_condition, text="レベル比較")
        self.groupbox_stop_level_comparison.place(x=300, y=0, width=530, height=50)
        #--------------------
        # チャネル番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stop_level_comparison, text='チャネル番号 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # チャネル番号のEntryを作成
        #--------------------
        self.textbox_stop_level_channel_no_var = tk.StringVar(self.groupbox_stop_level_comparison)
        self.textbox_stop_level_channel_no_var.set('0')
        self.textbox_stop_level_channel_no = tk.Entry(self.groupbox_stop_level_comparison, justify=tk.RIGHT, textvariable=self.textbox_stop_level_channel_no_var)
        self.textbox_stop_level_channel_no.place(x=110, y=5, width=50, height=19)
        #--------------------
        # 比較値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stop_level_comparison, text='比較値 : ', anchor='nw').place(x=180, y=5, width=50, height=19)
        #--------------------
        # 比較値のEntryを作成
        #--------------------
        self.textbox_stop_level_comparison_value_var = tk.StringVar(self.groupbox_stop_level_comparison)
        self.textbox_stop_level_comparison_value_var.set('1.0')
        self.textbox_stop_level_comparison_value = tk.Entry(self.groupbox_stop_level_comparison, justify=tk.RIGHT, textvariable=self.textbox_stop_level_comparison_value_var)
        self.textbox_stop_level_comparison_value.place(x=240, y=5, width=50, height=19)
        #--------------------
        # 比較値単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stop_level_comparison, text='[V]', anchor='nw').place(x=300, y=5, width=20, height=19)
        #--------------------
        # 比較方向のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stop_level_comparison, text='比較方向 : ', anchor='nw').place(x=340, y=5, width=60, height=19)
        #--------------------
        # 比較方向のComboboxを作成
        #--------------------
        self.combobox_stop_level_comparison_direction = ttk.Combobox(self.groupbox_stop_level_comparison)
        self.combobox_stop_level_comparison_direction.place(x=410, y=5, width=100, height=19)
        self.combobox_stop_level_comparison_direction['value'] = ('両方', '立ち上がり', '立ち下がり')
        self.combobox_stop_level_comparison_direction.current(0)
        self.combobox_stop_level_comparison_direction['state'] = 'readonly'
        #--------------------
        # インレンジ/アウトレンジ比較のLabelFrameを作成
        #--------------------
        self.groupbox_stop_range_comparison = tk.LabelFrame(self.groupbox_stop_condition, text="インレンジ/アウトレンジ比較")
        self.groupbox_stop_range_comparison.place(x=300, y=55, width=530, height=75)
        #--------------------
        # チャネル番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stop_range_comparison, text='チャネル番号 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # チャネル番号のEntryを作成
        #--------------------
        self.textbox_stop_range_channel_no_var = tk.StringVar(self.groupbox_stop_range_comparison)
        self.textbox_stop_range_channel_no_var.set('0')
        self.textbox_stop_range_channel_no = tk.Entry(self.groupbox_stop_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_stop_range_channel_no_var)
        self.textbox_stop_range_channel_no.place(x=110, y=5, width=50, height=19)
        #--------------------
        # 上限値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stop_range_comparison, text='上限値 : ', anchor='nw').place(x=180, y=5, width=50, height=19)
        #--------------------
        # 上限値のEntryを作成
        #--------------------
        self.textbox_stop_range_upper_limit_value_var = tk.StringVar(self.groupbox_stop_range_comparison)
        self.textbox_stop_range_upper_limit_value_var.set('10.0')
        self.textbox_stop_range_upper_limit_value = tk.Entry(self.groupbox_stop_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_stop_range_upper_limit_value_var)
        self.textbox_stop_range_upper_limit_value.place(x=240, y=5, width=50, height=19)
        #--------------------
        # 上限値単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stop_range_comparison, text='[V]', anchor='nw').place(x=300, y=5, width=20, height=19)
        #--------------------
        # 下限値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stop_range_comparison, text='下限値 : ', anchor='nw').place(x=180, y=30, width=50, height=19)
        #--------------------
        # 下限値のEntryを作成
        #--------------------
        self.textbox_stop_range_lower_limit_value_var = tk.StringVar(self.groupbox_stop_range_comparison)
        self.textbox_stop_range_lower_limit_value_var.set('1.0')
        self.textbox_stop_range_lower_limit_value = tk.Entry(self.groupbox_stop_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_stop_range_lower_limit_value_var)
        self.textbox_stop_range_lower_limit_value.place(x=240, y=30, width=50, height=19)
        #--------------------
        # 下限値単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stop_range_comparison, text='[V]', anchor='nw').place(x=300, y=30, width=20, height=19)
        #--------------------
        # 保持回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stop_range_comparison, text='保持回数 : ', anchor='nw').place(x=340, y=5, width=60, height=19)
        #--------------------
        # 保持回数のEntryを作成
        #--------------------
        self.textbox_stop_range_state_times_var = tk.StringVar(self.groupbox_stop_range_comparison)
        self.textbox_stop_range_state_times_var.set('1')
        self.textbox_stop_range_state_times = tk.Entry(self.groupbox_stop_range_comparison, justify=tk.RIGHT, textvariable=self.textbox_stop_range_state_times_var)
        self.textbox_stop_range_state_times.place(x=410, y=5, width=50, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # OKのButtonを作成
        #--------------------
        self.button_ok = tk.Button(self.condition_window, text='OK')
        self.button_ok.place(x=650, y=470, width=80, height=19)
        #--------------------
        # CancelのButtonを作成
        #--------------------
        self.button_cancel = tk.Button(self.condition_window, text='Cancel')
        self.button_cancel.place(x=740, y=470, width=80, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_start_condition,
                       command_clock_type,
                       command_stop_condition,
                       command_ok,
                       command_cancel):
        #----------------------------------------
        # 「開始条件」コンボボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_start_condition.bind('<<ComboboxSelected>>', command_start_condition)
        #----------------------------------------
        # 「クロック種類」コンボボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_clock_type.bind('<<ComboboxSelected>>', command_clock_type)
        #----------------------------------------
        # 「停止条件」コンボボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_stop_condition.bind('<<ComboboxSelected>>', command_stop_condition)
        #----------------------------------------
        # 「OK」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_ok.configure(command=command_ok)
        #----------------------------------------
        # 「Cancel」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cancel.configure(command=command_cancel)

    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.condition_window)