﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
// カウンタヘッダー
using CcntCs;

namespace CounterCallback
{
    public partial class NotifyDlg : Form
    {
        static bool                             bNotifyError = true;
        static bool                             bNotifyCarry = true;
        static uint[,]                          CompData = new uint[Form1.CHANNEL_NUM, 2]
                                                {
                                                     {   0x100,  0x150       },
                                                     {   0x200,  0x250       },
                                                     {   0x300,  0x350       },
                                                     {   0x400,  0x450       },
                                                };
        Ccnt                                    cnt = new Ccnt();
        private System.Windows.Forms.TextBox[]  txt_Reg0 = new System.Windows.Forms.TextBox[Form1.CHANNEL_NUM];    // 比較レジスタ0コントロール
        private System.Windows.Forms.TextBox[]  txt_Reg1 = new System.Windows.Forms.TextBox[Form1.CHANNEL_NUM];    // 比較レジスタ1コントロール

        public NotifyDlg()
        {
            InitializeComponent();

             //-----------------------------------
            // コントロール配列設定
            //-----------------------------------
            //-----------------------------------
            // 比較レジスタ0コントロール
            //-----------------------------------
            txt_Reg0[0] = txtCh0Reg0;
            txt_Reg0[1] = txtCh1Reg0;
            txt_Reg0[2] = txtCh2Reg0;
            txt_Reg0[3] = txtCh3Reg0;
            //-----------------------------------
            // 比較レジスタ1コントロール
            //-----------------------------------
            txt_Reg1[0] = txtCh0Reg1;
            txt_Reg1[1] = txtCh1Reg1;
            txt_Reg1[2] = txtCh2Reg1;
            txt_Reg1[3] = txtCh3Reg1;
        }

        //===================================================
        // ダイアログ初期化
        //===================================================
        private void NotifyDlg_Load(object sender, System.EventArgs e)
        {
            //-------------------------------
            // 初期データのセット
            //-------------------------------
            string szBuf = new string(' ', 100);
            short  ch;

            //-------------------------------
            // 各チャネルごとに
            //-------------------------------
            for (ch = 0; ch < Form1.CHANNEL_NUM; ch++)
            {
                //----------------
                // 比較値データのセット
                //----------------
                szBuf = string.Format("{0:X}", CompData[ch, 0]);
                txt_Reg0[ch].Text = szBuf;
                szBuf = string.Format("{0:X}", CompData[ch, 1]);
                txt_Reg1[ch].Text = szBuf;
                //----------------
                // 総チャネル数を超えた場合比較レジスタ表示を無効化
                //----------------
                if(ch >= Form1.ChannelNum){
                    txt_Reg0[ch].Enabled = false;
                    txt_Reg1[ch].Enabled = false;
                //----------------
                // 総チャネル数以下の場合表示を有効化
                //----------------
                }else{
                    txt_Reg0[ch].Enabled = true;
                    txt_Reg1[ch].Enabled = true;
                }
            }

            if (bNotifyError == true)
            {
                rdoError0.Checked = true;
            }
            else
            {
                rdoError1.Checked = true;
            }
            if (bNotifyCarry == true)
            {
                rdoCarry0.Checked = true;
            }
            else
            {
                rdoCarry1.Checked = true;
            }
        }

        //---------------------------------------------
        // 設定完了処理
        //---------------------------------------------
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);
            int    ret;
            short  ChNo, RegNo;
            string str;
            string error_string = new string(' ', 256);
            //-------------------------------
            // 設定情報の取得
            //-------------------------------
            for (ChNo = 0; ChNo < Form1.ChannelNum; ChNo++)
            {
                szBuf = txt_Reg0[ChNo].Text;
                CompData[ChNo, 0] = uint.Parse(szBuf, NumberStyles.HexNumber);
                szBuf = txt_Reg1[ChNo].Text;
                CompData[ChNo, 1] = uint.Parse(szBuf, NumberStyles.HexNumber);
            }
            bNotifyError = rdoError0.Checked;
            bNotifyCarry = rdoCarry0.Checked;

            //-------------------------------
            // 設定情報をデバイスへセット
            //-------------------------------
            for (ChNo = 0; ChNo < Form1.ChannelNum; ChNo++)
            {
                for (RegNo = 0; RegNo < 2; RegNo++)
                {
                    //-------------------------------
                    // カウント一致の通知 & 比較レジスタ 設定
                    //-------------------------------
                    ret = cnt.NotifyCountUp(Form1.Id, ChNo, RegNo, CompData[ChNo, RegNo], (IntPtr)0);
                    if (ret != 0)
                    {
                        cnt.GetErrorString(ret, out error_string);
                        str = string.Format("CntNotifyCountUp : ret = {0:X} h\n {1}", ret, error_string);
                        MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                        return;
                    }
                }
            }
            //-------------------------------
            // カウンタエラーが通知の場合
            //-------------------------------
            if (bNotifyError == true)
            {
                //-------------------------------
                // カウンタエラー通知 設定
                //-------------------------------
                ret = cnt.NotifyCounterError(Form1.Id, (IntPtr)0);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntNotifyCounterError : ret = {0:X} h\n {1}", ret, error_string);
                    MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //-------------------------------
            // キャリー/ボローが通知の場合
            //-------------------------------
            if (bNotifyCarry == true)
            {
                //-------------------------------
                // キャリー/ボロー通知 設定
                //-------------------------------
                ret = cnt.NotifyCarryBorrow(Form1.Id, (IntPtr)0);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntNotifyCarryBorrow : ret = {0:X} h\n {1}", ret, error_string);
                    MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                }
            }
            this.Close();
        }

        //---------------------------------------------
        // キャンセル処理
        //---------------------------------------------
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}
