﻿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;
// Counter header
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];    // Compare register 0 control
        private System.Windows.Forms.TextBox[]  txt_Reg1 = new System.Windows.Forms.TextBox[Form1.CHANNEL_NUM];    // Compare register 1 control

        public NotifyDlg()
        {
            InitializeComponent();
            
            //-----------------------------------
            // Control array settings
            //-----------------------------------
            //-----------------------------------
            // Compare register 0
            //-----------------------------------
            txt_Reg0[0] = txtCh0Reg0;
            txt_Reg0[1] = txtCh1Reg0;
            txt_Reg0[2] = txtCh2Reg0;
            txt_Reg0[3] = txtCh3Reg0;
            //-----------------------------------
            // Compare register 1
            //-----------------------------------
            txt_Reg1[0] = txtCh0Reg1;
            txt_Reg1[1] = txtCh1Reg1;
            txt_Reg1[2] = txtCh2Reg1;
            txt_Reg1[3] = txtCh3Reg1;
        }

        //===================================================
        // Dialog initialization
        //===================================================
        private void NotifyDlg_Load(object sender, System.EventArgs e)
        {
            //-------------------------------
            // Set initial data
            //-------------------------------
            string szBuf = new string(' ', 100);
            short  ch;

            //-------------------------------
            // For each channel
            //-------------------------------
            for (ch = 0; ch < Form1.CHANNEL_NUM; ch++)
            {
                //----------------
                // Sets comparison value data.
                //----------------
                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;
                //----------------
                // Disables the display when the number of loops exceeds the total number of channels.
                //----------------
                if(ch >= Form1.ChannelNum){
                    txt_Reg0[ch].Enabled = false;
                    txt_Reg1[ch].Enabled = false;
                //----------------
                // Enables the display when the number of loops is less than or equal to the total number of channels.
                //----------------
                }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;
            }
        }

        //---------------------------------------------
        // OK button processing
        //---------------------------------------------
        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);
            //-------------------------------
            // Get setting information
            //-------------------------------
            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;

            //-------------------------------
            // Set setting information to device
            //-------------------------------
            for (ChNo = 0; ChNo < Form1.ChannelNum; ChNo++)
            {
                for (RegNo = 0; RegNo < 2; RegNo++)
                {
                    //---------------------------------------------
                    // Set count match notification & compare register
                    //---------------------------------------------
                    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;
                    }
                }
            }
            //---------------------------------------------
            // Counter error is a notification
            //---------------------------------------------
            if (bNotifyError == true)
            {
                //---------------------------------------------
                // Set counter error notification
                //---------------------------------------------
                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;
                }
            }
            //---------------------------------------------
            // Carry/borrow is a notification
            //---------------------------------------------
            if (bNotifyCarry == true)
            {
                //---------------------------------------------
                // Set carry/borrow notification
                //---------------------------------------------
                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();
        }

        //---------------------------------------------
        // Cancel button processing
        //---------------------------------------------
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}
