﻿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 CcntCs;

namespace Counter
{
    public partial class Comp : Form
    {
        Ccnt cnt = new Ccnt();

        private System.Windows.Forms.TextBox[] textBox_Channel = new System.Windows.Forms.TextBox[8];

        // 比較値
        static private uint[] CompValue = { 100, 200, 300, 400, 500, 600, 700, 800 };

        public Comp()
        {
            InitializeComponent();

            textBox_Channel[0] = textBox_Channel0;
            textBox_Channel[1] = textBox_Channel1;
            textBox_Channel[2] = textBox_Channel2;
            textBox_Channel[3] = textBox_Channel3;
            textBox_Channel[4] = textBox_Channel4;
            textBox_Channel[5] = textBox_Channel5;
            textBox_Channel[6] = textBox_Channel6;
            textBox_Channel[7] = textBox_Channel7;
            //-----------------------------
            // 表示の無効化
            //-----------------------------
            for (short ChNo = Form1.m_ChannelNum; ChNo < 8; ChNo++)
            {
                textBox_Channel[ChNo].Enabled = false;
            }
            //-----------------------------------
            // 画面情報を更新
            //-----------------------------------
            for (int ChNo = 0; ChNo < 8; ChNo++)
            {
                textBox_Channel[ChNo].Text = System.Convert.ToString(CompValue[ChNo]);
            }

            for (int i = 0; i < 0x10; i++)
            {
                comboBox_PulseWidth.Items.Add(System.Convert.ToString(i, 16));
            }

            int   Ret;
            short PlsWidth;
            short Ch_No = 0;
            Ret = cnt.GetPulseWidth(Form1.m_Id, Ch_No, out PlsWidth);
            comboBox_PulseWidth.SelectedIndex = PlsWidth;
        }

        private void radioButton_Dec_CheckedChanged(object sender, System.EventArgs e)
        {
            //-----------------------------------
            // 画面情報を更新
            //-----------------------------------
            for (short ChNo = 0; ChNo < 8; ChNo++)
            {
                textBox_Channel[ChNo].Text = System.Convert.ToString(CompValue[ChNo]);
            }
        }

        private void radioButton_Hex_CheckedChanged(object sender, System.EventArgs e)
        {
            //-----------------------------------
            // 画面情報を更新
            //-----------------------------------
            for (short ChNo = 0; ChNo < 8; ChNo++)
            {
                textBox_Channel[ChNo].Text = System.Convert.ToString(CompValue[ChNo], 16);
            }
        }

        private void button_OK_Click(object sender, System.EventArgs e)
        {

            //-----------------------------------
            // 比較値設定
            //-----------------------------------
            if (radioButton_Dec.Checked == true)
            {
                for (short ChNo = 0; ChNo < Form1.m_ChannelNum; ChNo++)
                {
                    try
                    {
                        CompValue[ChNo] = System.Convert.ToUInt32(textBox_Channel[ChNo].Text);
                    }
                    catch
                    {
                        CompValue[ChNo] = 0;
                    }
                }
            }
            else
            {
                for (short ChNo = 0; ChNo < Form1.m_ChannelNum; ChNo++)
                {
                    try
                    {
                        CompValue[ChNo] = System.Convert.ToUInt32(textBox_Channel[ChNo].Text, 16);
                    }
                    catch
                    {
                        CompValue[ChNo] = 0;
                    }
                }
            }

            for (short ChNo = 0; ChNo < Form1.m_ChannelNum; ChNo++)
            {
                int Ret;
                Ret = cnt.NotifyCountUp(Form1.m_Id, ChNo, 0, CompValue[ChNo], Form1.m_Handle);
                if (Ret != 0)
                {
                    string csTmp;
                    csTmp = string.Format("CntNotifyCountUp Return Code = {0:d5}", Ret);
                    MessageBox.Show(csTmp, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }

            //-----------------------------------
            // パルス長設定
            //-----------------------------------
            for (short ChNo = 0; ChNo < Form1.m_ChannelNum; ChNo++)
            {
                int   Ret;
                short PlsWidth;
                PlsWidth = System.Convert.ToInt16(comboBox_PulseWidth.Text, 16);
                Ret = cnt.SetPulseWidth(Form1.m_Id, ChNo, PlsWidth);
                if (Ret != 0)
                {
                    string csTmp;
                    csTmp = string.Format("CntSetPulseWidth Return Code = {0:d5}", Ret);
                    MessageBox.Show(csTmp, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }

            this.Close();
        }

        private void button_Cancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}
