#================================================================
#================================================================
# API-CNT(WDM) for サンプリング機能搭載デバイス
# カウンタサンプル
# 通知設定UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


#========================================
# NotifyWindowUI クラス
#========================================
class NotifyWindowUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # 親ウィンドウの設定
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # 通知設定ウィンドウの作成
        #----------------------------------------
        self.notify_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # モーダルダイアログの設定
        #----------------------------------------
        self.notify_window.grab_set()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.notify_window.title("通知設定")
        self.notify_window.geometry("460x200")
        #----------------------------------------
        # UIの作成を開始
        #----------------------------------------
        self._create_ui()

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # カウント一致のLabelFrameを作成
        #----------------------------------------
        self.groupbox_count_match = tk.LabelFrame(self.notify_window, text="カウント一致")
        self.groupbox_count_match.place(x=10, y=10, width=240, height=180)
        #--------------------
        # Reg0のLabelを作成
        #--------------------
        tk.Label(self.groupbox_count_match, text='Reg0', anchor='nw').place(x=40, y=15, width=50, height=20)
        #--------------------
        # Reg1のLabelを作成
        #--------------------
        tk.Label(self.groupbox_count_match, text='Reg1', anchor='nw').place(x=120, y=15, width=50, height=20)
        #--------------------
        # Ch0のLabelを作成
        #--------------------
        tk.Label(self.groupbox_count_match, text='Ch0 :', anchor='nw').place(x=1, y=37, width=30, height=20)
        #--------------------
        # Ch0 Reg0のEntryを作成
        #--------------------
        self.textbox_ch0_reg0_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch0_reg0_value_var.set('100')
        self.textbox_ch0_reg0_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch0_reg0_value_var, justify=tk.RIGHT)
        self.textbox_ch0_reg0_value.place(x=40, y=37, width=60, height=20)
        #--------------------
        # Ch0 Reg1のEntryを作成
        #--------------------
        self.textbox_ch0_reg1_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch0_reg1_value_var.set('150')
        self.textbox_ch0_reg1_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch0_reg1_value_var, justify=tk.RIGHT)
        self.textbox_ch0_reg1_value.place(x=120, y=37, width=60, height=20)
        #--------------------
        # Ch1のLabelを作成
        #--------------------
        tk.Label(self.groupbox_count_match, text='Ch1 :', anchor='nw').place(x=1, y=59, width=30, height=20)
        #--------------------
        # Ch1 Reg0のEntryを作成
        #--------------------
        self.textbox_ch1_reg0_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch1_reg0_value_var.set('200')
        self.textbox_ch1_reg0_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch1_reg0_value_var, justify=tk.RIGHT)
        self.textbox_ch1_reg0_value.place(x=40, y=59, width=60, height=20)
        #--------------------
        # Ch1 Reg1のEntryを作成
        #--------------------
        self.textbox_ch1_reg1_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch1_reg1_value_var.set('250')
        self.textbox_ch1_reg1_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch1_reg1_value_var, justify=tk.RIGHT)
        self.textbox_ch1_reg1_value.place(x=120, y=59, width=60, height=20)
        #--------------------
        # Ch2のLabelを作成
        #--------------------
        tk.Label(self.groupbox_count_match, text='Ch2 :', anchor='nw').place(x=1, y=81, width=30, height=20)
        #--------------------
        # Ch2 Reg0のEntryを作成
        #--------------------
        self.textbox_ch2_reg0_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch2_reg0_value_var.set('300')
        self.textbox_ch2_reg0_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch2_reg0_value_var, justify=tk.RIGHT)
        self.textbox_ch2_reg0_value.place(x=40, y=81, width=60, height=20)
        #--------------------
        # Ch2 Reg1のEntryを作成
        #--------------------
        self.textbox_ch2_reg1_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch2_reg1_value_var.set('350')
        self.textbox_ch2_reg1_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch2_reg1_value_var, justify=tk.RIGHT)
        self.textbox_ch2_reg1_value.place(x=120, y=81, width=60, height=20)
        #--------------------
        # Ch3のLabelを作成
        #--------------------
        tk.Label(self.groupbox_count_match, text='Ch3 :', anchor='nw').place(x=1, y=103, width=30, height=20)
        #--------------------
        # Ch3 Reg0のEntryを作成
        #--------------------
        self.textbox_ch3_reg0_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch3_reg0_value_var.set('400')
        self.textbox_ch3_reg0_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch3_reg0_value_var, justify=tk.RIGHT)
        self.textbox_ch3_reg0_value.place(x=40, y=103, width=60, height=20)
        #--------------------
        # Ch3 Reg1のEntryを作成
        #--------------------
        self.textbox_ch3_reg1_value_var = tk.StringVar(self.groupbox_count_match)
        self.textbox_ch3_reg1_value_var.set('450')
        self.textbox_ch3_reg1_value = tk.Entry(self.groupbox_count_match, textvariable=self.textbox_ch3_reg1_value_var, justify=tk.RIGHT)
        self.textbox_ch3_reg1_value.place(x=120, y=103, width=60, height=20)
        #----------------------------------------
        # エラーのLabelFrameを作成
        #----------------------------------------
        self.groupbox_error = tk.LabelFrame(self.notify_window, text="エラー")
        self.groupbox_error.place(x=260, y=10, width=190, height=70)
        #--------------------
        # エラーのRadiobuttonを作成
        #--------------------
        self.radiobutton_error_var = tk.IntVar()
        self.radiobutton_error_var.set(1)
        tk.Radiobutton(self.groupbox_error, text="通知", variable=self.radiobutton_error_var, value=1).place(x=1, y=20, width=90, height=20)
        tk.Radiobutton(self.groupbox_error, text="非通知", variable=self.radiobutton_error_var, value=2).place(x=80, y=20, width=90, height=20)
        #----------------------------------------
        # キャリー / ボローのLabelFrameを作成
        #----------------------------------------
        self.groupbox_carry_borrow = tk.LabelFrame(self.notify_window, text="キャリー / ボロー")
        self.groupbox_carry_borrow.place(x=260, y=90, width=190, height=70)
        #--------------------
        # キャリー / ボローのRadiobuttonを作成
        #--------------------
        self.radiobutton_carry_borrow_var = tk.IntVar()
        self.radiobutton_carry_borrow_var.set(1)
        tk.Radiobutton(self.groupbox_carry_borrow, text="通知", variable=self.radiobutton_carry_borrow_var, value=1).place(x=1, y=20, width=90, height=20)
        tk.Radiobutton(self.groupbox_carry_borrow, text="非通知", variable=self.radiobutton_carry_borrow_var, value=2).place(x=80, y=20, width=90, height=20)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # OKのButtonを作成
        #--------------------
        self.button_notify_window_ok = tk.Button(self.notify_window, text='OK')
        self.button_notify_window_ok.place(x=300, y=170, width=70, height=20)
        #--------------------
        # キャンセルのButtonを作成
        #--------------------
        self.button_notify_window_cancel = tk.Button(self.notify_window, text='キャンセル')
        self.button_notify_window_cancel.place(x=380, y=170, width=70, height=20)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_notify_window_ok,
                       command_notify_window_cancel):
        #----------------------------------------
        # 「OK」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_notify_window_ok.configure(command=command_notify_window_ok)
        #----------------------------------------
        # 「キャンセル」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_notify_window_cancel.configure(command=command_notify_window_cancel)
 
    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.notify_window)