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

namespace Counter
{
    public partial class Setting : Form
    {
        //----------------------------------------
        // クラスCaioインスタンス作成
        //----------------------------------------
        Caio aio = new Caio();

        public Setting()
        {
            InitializeComponent();
        }

        private void Setting_Load(object sender, EventArgs e)
        {
            string error_string;      // エラーコード文字列
            string message_string;    // メッセージ表示用
            int    ret;               // 戻り値取得用
            int    index;             // コンボボックスのインデックス
            short  cnt_mode;          // 動作モード
            short  err_flag;          // 関数の実行エラーフラグ

            //----------------------------------------
            // パラメータの初期化
            //----------------------------------------
            err_flag = 0;
            message_string = "";
            //----------------------------------------
            // 比較カウント一致が発生したときのカウンタの動作を取得
            //----------------------------------------
            ret = aio.GetCntComparisonMode(Form1.g_id, Form1.g_channel, out cnt_mode);
            //----------------------------------------
            // 異常終了の処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                message_string = "aio.GetCntComparisonMode = " + ret.ToString() + "：" + error_string + "\r\n\r\n※エラーが発生したため表示を更新できませんでした。";
                err_flag = 1;
                //----------------------------------------
                // 比較カウント設定
                //----------------------------------------
                checkBox_CompareLoad.Checked = false;
                //----------------------------------------
                // 比較一致プリセット設定
                //----------------------------------------
                checkBox_PresetLoad.Checked = false;
            }
            //----------------------------------------
            // 正常終了の処理
            //----------------------------------------
            else
            {
                //--------------------
                // 動作モードの設定
                //--------------------
                switch (cnt_mode)
                {
                    case 0:
                        //----------------------------------------
                        // 比較カウント設定
                        //----------------------------------------
                        checkBox_CompareLoad.Checked = false;
                        //----------------------------------------
                        // 比較一致プリセット設定
                        //----------------------------------------
                        checkBox_PresetLoad.Checked = false;
                        break;
                    case (short)CaioConst.CNT_LOADPRESET:
                        //----------------------------------------
                        // 比較カウント設定
                        //----------------------------------------
                        checkBox_CompareLoad.Checked = false;
                        //----------------------------------------
                        // 比較一致プリセット設定
                        //----------------------------------------
                        checkBox_PresetLoad.Checked = true;
                        break;
                    case (short)CaioConst.CNT_LOADCOMP:
                        //----------------------------------------
                        // 比較カウント設定
                        //----------------------------------------
                        checkBox_CompareLoad.Checked = true;
                        //----------------------------------------
                        // 比較一致プリセット設定
                        //----------------------------------------
                        checkBox_PresetLoad.Checked = false;
                        break;
                    case (short)(CaioConst.CNT_LOADPRESET | CaioConst.CNT_LOADCOMP):
                        //----------------------------------------
                        // 比較カウント設定
                        //----------------------------------------
                        checkBox_CompareLoad.Checked = true;
                        //----------------------------------------
                        // 比較一致プリセット設定
                        //----------------------------------------
                        checkBox_PresetLoad.Checked = true;
                        break;
                    default:
                        //----------------------------------------
                        // 比較カウント設定
                        //----------------------------------------
                        checkBox_CompareLoad.Checked = false;
                        //----------------------------------------
                        // 比較一致プリセット設定
                        //----------------------------------------
                        checkBox_PresetLoad.Checked = false;
                        break;
                }
            }
            //----------------------------------------
            // 動作モードによる使用可能コントロールを更新
            //----------------------------------------
            checkBox_CompareLoad_CheckedChanged(new object(), new EventArgs());
            checkBox_PresetLoad_CheckedChanged(new object(), new EventArgs());
            //----------------------------------------
            // 比較カウント設定：設定データ数用コンボボックスの作成
            //----------------------------------------
            comboBox_SettingCompareDataNum.Items.Add("1");
            comboBox_SettingCompareDataNum.Items.Add("2");
            comboBox_SettingCompareDataNum.Items.Add("3");
            comboBox_SettingCompareDataNum.Items.Add("4");
            comboBox_SettingCompareDataNum.Items.Add("5");
            //----------------------------------------
            // 比較カウント設定：設定データ数用コンボボックスの選択を変更
            //----------------------------------------
            index = (Form1.g_compare_data_num - 1);
            comboBox_SettingCompareDataNum.SelectedIndex = index;
            if ((cnt_mode & (short)CaioConst.CNT_LOADCOMP) == (short)CaioConst.CNT_LOADCOMP)
            {
                comboBox_SettingCompareDataNum_SelectedIndexChanged(new object(), new EventArgs());
            }
            else
            {
                textBox_SettingCompareData0.Enabled = false;
                textBox_SettingCompareData1.Enabled = false;
                textBox_SettingCompareData2.Enabled = false;
                textBox_SettingCompareData3.Enabled = false;
                textBox_SettingCompareData4.Enabled = false;
            }
            //----------------------------------------
            // 比較カウント設定：繰り返し設定用コンボボックスの作成
            //----------------------------------------
            comboBox_SettingCompareRepeat.Items.Add("繰り返し設定なし");
            comboBox_SettingCompareRepeat.Items.Add("繰り返し設定あり");
            //----------------------------------------
            // 比較カウント設定：繰り返し設定用コンボボックスの選択を変更
            //----------------------------------------
            index = Form1.g_compare_repeat;
            comboBox_SettingCompareRepeat.SelectedIndex = index;
            //----------------------------------------
            // 比較カウント設定：比較カウント値を更新
            //----------------------------------------
            textBox_SettingCompareData0.Text = Form1.g_compare_data_0.ToString();
            textBox_SettingCompareData1.Text = Form1.g_compare_data_1.ToString();
            textBox_SettingCompareData2.Text = Form1.g_compare_data_2.ToString();
            textBox_SettingCompareData3.Text = Form1.g_compare_data_3.ToString();
            textBox_SettingCompareData4.Text = Form1.g_compare_data_4.ToString();
            //----------------------------------------
            // 比較一致プリセット設定：設定データ数用コンボボックスの作成
            //----------------------------------------
            comboBox_SettingPresetDataNum.Items.Add("1");
            comboBox_SettingPresetDataNum.Items.Add("2");
            comboBox_SettingPresetDataNum.Items.Add("3");
            comboBox_SettingPresetDataNum.Items.Add("4");
            comboBox_SettingPresetDataNum.Items.Add("5");
            //----------------------------------------
            // 比較一致プリセット設定：設定データ数用コンボボックスの選択を変更
            //----------------------------------------
            index = (Form1.g_preset_data_num - 1);
            comboBox_SettingPresetDataNum.SelectedIndex = index;
            if ((cnt_mode & (short)CaioConst.CNT_LOADPRESET) == (short)CaioConst.CNT_LOADPRESET)
            {
                comboBox_SettingPresetDataNum_SelectedIndexChanged(new object(), new EventArgs());
            }
            else
            {
                textBox_SettingPresetData0.Enabled = false;
                textBox_SettingPresetData1.Enabled = false;
                textBox_SettingPresetData2.Enabled = false;
                textBox_SettingPresetData3.Enabled = false;
                textBox_SettingPresetData4.Enabled = false;
            }
            //----------------------------------------
            // 比較一致プリセット設定：繰り返し設定用コンボボックスの作成
            //----------------------------------------
            comboBox_SettingPresetRepeat.Items.Add("繰り返し設定なし");
            comboBox_SettingPresetRepeat.Items.Add("繰り返し設定あり");
            //----------------------------------------
            // 比較一致プリセット設定：繰り返し設定用コンボボックスの選択を変更
            //----------------------------------------
            index = Form1.g_preset_repeat;
            comboBox_SettingPresetRepeat.SelectedIndex = index;
            //----------------------------------------
            // 比較一致プリセット設定：プリセット値を更新
            //----------------------------------------
            textBox_SettingPresetData0.Text = Form1.g_preset_data_0.ToString();
            textBox_SettingPresetData1.Text = Form1.g_preset_data_1.ToString();
            textBox_SettingPresetData2.Text = Form1.g_preset_data_2.ToString();
            textBox_SettingPresetData3.Text = Form1.g_preset_data_3.ToString();
            textBox_SettingPresetData4.Text = Form1.g_preset_data_4.ToString();
            //----------------------------------------
            // エラーが発生していた場合メッセージを表示
            //----------------------------------------
            if (err_flag != 0)
            {
                MessageBox.Show(message_string, "エラー", MessageBoxButtons.OK);
            }
        }

        //================================================================================
        // 比較カウント設定チェックボックスがクリックされた際の処理になります
        // 使用可能コントロールを更新します
        //================================================================================
        private void checkBox_CompareLoad_CheckedChanged(object sender, EventArgs e)
        {
            //----------------------------------------
            // 使用可能コントロールを更新
            //----------------------------------------
            if (checkBox_CompareLoad.Checked == true)
            {
                comboBox_SettingCompareDataNum.Enabled = true;
                comboBox_SettingCompareRepeat.Enabled  = true;
                textBox_SettingCompareData0.Enabled    = true;
                textBox_SettingCompareData1.Enabled    = true;
                textBox_SettingCompareData2.Enabled    = true;
                textBox_SettingCompareData3.Enabled    = true;
                textBox_SettingCompareData4.Enabled    = true;
                comboBox_SettingCompareDataNum_SelectedIndexChanged(new object(), new EventArgs());
            }
            else
            {
                comboBox_SettingCompareDataNum.Enabled = false;
                comboBox_SettingCompareRepeat.Enabled  = false;
                textBox_SettingCompareData0.Enabled    = false;
                textBox_SettingCompareData1.Enabled    = false;
                textBox_SettingCompareData2.Enabled    = false;
                textBox_SettingCompareData3.Enabled    = false;
                textBox_SettingCompareData4.Enabled    = false;
            }
        }

        //================================================================================
        // 比較カウント設定のデータ数コンボボックスのインデックスが変更された際の処理になります
        // 使用可能コントロールを更新します
        //================================================================================
        private void comboBox_SettingCompareDataNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index;       // コンボボックスのインデックス
            int data_num;    // 設定データ数

            //----------------------------------------
            // コンボボックスからデータ数のインデックスを取得
            //----------------------------------------
            index = comboBox_SettingCompareDataNum.SelectedIndex;
            //----------------------------------------
            // データ数による分岐処理
            //----------------------------------------
            data_num = (index + 1);
            switch (data_num)
            {
                case 1:
                    textBox_SettingCompareData0.Enabled = true;
                    textBox_SettingCompareData1.Enabled = false;
                    textBox_SettingCompareData2.Enabled = false;
                    textBox_SettingCompareData3.Enabled = false;
                    textBox_SettingCompareData4.Enabled = false;
                    break;
                case 2:
                    textBox_SettingCompareData0.Enabled = true;
                    textBox_SettingCompareData1.Enabled = true;
                    textBox_SettingCompareData2.Enabled = false;
                    textBox_SettingCompareData3.Enabled = false;
                    textBox_SettingCompareData4.Enabled = false;
                    break;
                case 3:
                    textBox_SettingCompareData0.Enabled = true;
                    textBox_SettingCompareData1.Enabled = true;
                    textBox_SettingCompareData2.Enabled = true;
                    textBox_SettingCompareData3.Enabled = false;
                    textBox_SettingCompareData4.Enabled = false;
                    break;
                case 4:
                    textBox_SettingCompareData0.Enabled = true;
                    textBox_SettingCompareData1.Enabled = true;
                    textBox_SettingCompareData2.Enabled = true;
                    textBox_SettingCompareData3.Enabled = true;
                    textBox_SettingCompareData4.Enabled = false;
                    break;
                case 5:
                    textBox_SettingCompareData0.Enabled = true;
                    textBox_SettingCompareData1.Enabled = true;
                    textBox_SettingCompareData2.Enabled = true;
                    textBox_SettingCompareData3.Enabled = true;
                    textBox_SettingCompareData4.Enabled = true;
                    break;
                default:
                    textBox_SettingCompareData0.Enabled = true;
                    textBox_SettingCompareData1.Enabled = false;
                    textBox_SettingCompareData2.Enabled = false;
                    textBox_SettingCompareData3.Enabled = false;
                    textBox_SettingCompareData4.Enabled = false;
                    break;
            }
        }

        //================================================================================
        // 比較一致プリセット設定チェックボックスがクリックされた際の処理になります
        // 使用可能コントロールを更新します
        //================================================================================
        private void checkBox_PresetLoad_CheckedChanged(object sender, EventArgs e)
        {
            //----------------------------------------
            // 使用可能コントロールを更新
            //----------------------------------------
            if (checkBox_PresetLoad.Checked == true)
            {
                comboBox_SettingPresetDataNum.Enabled = true;
                comboBox_SettingPresetRepeat.Enabled  = true;
                textBox_SettingPresetData0.Enabled    = true;
                textBox_SettingPresetData1.Enabled    = true;
                textBox_SettingPresetData2.Enabled    = true;
                textBox_SettingPresetData3.Enabled    = true;
                textBox_SettingPresetData4.Enabled    = true;
                comboBox_SettingPresetDataNum_SelectedIndexChanged(new object(), new EventArgs());
            }
            else
            {
                comboBox_SettingPresetDataNum.Enabled = false;
                comboBox_SettingPresetRepeat.Enabled  = false;
                textBox_SettingPresetData0.Enabled    = false;
                textBox_SettingPresetData1.Enabled    = false;
                textBox_SettingPresetData2.Enabled    = false;
                textBox_SettingPresetData3.Enabled    = false;
                textBox_SettingPresetData4.Enabled    = false;
            }
        }

        //================================================================================
        // 比較一致プリセット設定のデータ数コンボボックスのインデックスが変更された際の処理になります
        // 使用可能コントロールを更新します
        //================================================================================
        private void comboBox_SettingPresetDataNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index;       // コンボボックスのインデックス
            int data_num;    // 設定データ数

            //----------------------------------------
            // コンボボックスからデータ数のインデックスを取得
            //----------------------------------------
            index = comboBox_SettingPresetDataNum.SelectedIndex;
            //----------------------------------------
            // データ数による分岐処理
            //----------------------------------------
            data_num = (index + 1);
            switch (data_num)
            {
                case 1:
                    textBox_SettingPresetData0.Enabled = true;
                    textBox_SettingPresetData1.Enabled = false;
                    textBox_SettingPresetData2.Enabled = false;
                    textBox_SettingPresetData3.Enabled = false;
                    textBox_SettingPresetData4.Enabled = false;
                    break;
                case 2:
                    textBox_SettingPresetData0.Enabled = true;
                    textBox_SettingPresetData1.Enabled = true;
                    textBox_SettingPresetData2.Enabled = false;
                    textBox_SettingPresetData3.Enabled = false;
                    textBox_SettingPresetData4.Enabled = false;
                    break;
                case 3:
                    textBox_SettingPresetData0.Enabled = true;
                    textBox_SettingPresetData1.Enabled = true;
                    textBox_SettingPresetData2.Enabled = true;
                    textBox_SettingPresetData3.Enabled = false;
                    textBox_SettingPresetData4.Enabled = false;
                    break;
                case 4:
                    textBox_SettingPresetData0.Enabled = true;
                    textBox_SettingPresetData1.Enabled = true;
                    textBox_SettingPresetData2.Enabled = true;
                    textBox_SettingPresetData3.Enabled = true;
                    textBox_SettingPresetData4.Enabled = false;
                    break;
                case 5:
                    textBox_SettingPresetData0.Enabled = true;
                    textBox_SettingPresetData1.Enabled = true;
                    textBox_SettingPresetData2.Enabled = true;
                    textBox_SettingPresetData3.Enabled = true;
                    textBox_SettingPresetData4.Enabled = true;
                    break;
                default:
                    textBox_SettingPresetData0.Enabled = true;
                    textBox_SettingPresetData1.Enabled = false;
                    textBox_SettingPresetData2.Enabled = false;
                    textBox_SettingPresetData3.Enabled = false;
                    textBox_SettingPresetData4.Enabled = false;
                    break;
            }
        }

        //================================================================================
        // OKボタンをクリックした際の処理になります
        // カウンタの設定を行います
        //================================================================================
        private void button_OK_Click(object sender, EventArgs e)
        {
            string error_string;                    // エラーコード文字列
            string text_string;                     // テキストボックス更新用
            int    ret;                             // 戻り値取得用
            short  cnt_mode;                        // 動作モード
            int    index;                           // コンボボックスのインデックス
            int[]  comparison_data = new int[8];    // 比較カウントデータ
            int[]  preset_data     = new int[8];    // カウンタのプリセットデータ

            //----------------------------------------
            // カウンタ動作モードの取得
            //----------------------------------------
            cnt_mode = (short)((checkBox_PresetLoad.Checked == true ? (short)CaioConst.CNT_LOADPRESET : 0) | (checkBox_CompareLoad.Checked == true ? (short)CaioConst.CNT_LOADCOMP : 0));
            //----------------------------------------
            // カウンタ動作モードの設定
            //----------------------------------------
            ret = aio.SetCntComparisonMode(Form1.g_id, Form1.g_channel, cnt_mode);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                text_string = "aio.SetCntComparisonMode = " + ret.ToString() + "：" + error_string;
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // 比較カウント値の設定
            //----------------------------------------
            if (checkBox_CompareLoad.Checked == true)
            {
                //----------------------------------------
                // コンボボックスからデータ数のインデックスを取得
                //----------------------------------------
                index = comboBox_SettingCompareDataNum.SelectedIndex;
                Form1.g_compare_data_num = (index + 1);
                //----------------------------------------
                // テキストボックスからカウンタの比較カウントデータを取得
                //----------------------------------------
                if (int.TryParse(textBox_SettingCompareData0.Text, out Form1.g_compare_data_0) == false)
                {
                    Form1.g_compare_data_0 = 0;
                }
                comparison_data[0] = Form1.g_compare_data_0;
                if (int.TryParse(textBox_SettingCompareData1.Text, out Form1.g_compare_data_1) == false)
                {
                    Form1.g_compare_data_1 = 0;
                }
                comparison_data[1] = Form1.g_compare_data_1;
                if (int.TryParse(textBox_SettingCompareData2.Text, out Form1.g_compare_data_2) == false)
                {
                    Form1.g_compare_data_2 = 0;
                }
                comparison_data[2] = Form1.g_compare_data_2;
                if (int.TryParse(textBox_SettingCompareData3.Text, out Form1.g_compare_data_3) == false)
                {
                    Form1.g_compare_data_3 = 0;
                }
                comparison_data[3] = Form1.g_compare_data_3;
                if (int.TryParse(textBox_SettingCompareData4.Text, out Form1.g_compare_data_4) == false)
                {
                    Form1.g_compare_data_4 = 0;
                }
                comparison_data[4] = Form1.g_compare_data_4;
                //----------------------------------------
                // コンボボックスから比較繰り返し設定のインデックスを取得
                //----------------------------------------
                index = comboBox_SettingCompareRepeat.SelectedIndex;
                Form1.g_compare_repeat = (short)index;
                //----------------------------------------
                // 比較カウント一致後にロードされる次回の比較カウント値を設定
                //----------------------------------------
                ret = aio.SetCntComparisonReg(Form1.g_id, Form1.g_channel, Form1.g_compare_data_num, comparison_data, Form1.g_compare_repeat);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = "aio.SetCntComparisonReg = " + ret.ToString() + "：" + error_string;
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // プリセットロード値の設定
            //----------------------------------------
            if (checkBox_PresetLoad.Checked == true)
            {
                //----------------------------------------
                // コンボボックスからデータ数のインデックスを取得
                //----------------------------------------
                index = comboBox_SettingPresetDataNum.SelectedIndex;
                Form1.g_preset_data_num = (index + 1);
                //----------------------------------------
                // テキストボックスからカウンタのプリセットデータを取得
                //----------------------------------------
                if (int.TryParse(textBox_SettingPresetData0.Text, out Form1.g_preset_data_0) == false)
                {
                    Form1.g_preset_data_0 = 0;
                }
                preset_data[0] = Form1.g_preset_data_0;
                if (int.TryParse(textBox_SettingPresetData1.Text, out Form1.g_preset_data_1) == false)
                {
                    Form1.g_preset_data_1 = 0;
                }
                preset_data[1] = Form1.g_preset_data_1;
                if (int.TryParse(textBox_SettingPresetData2.Text, out Form1.g_preset_data_2) == false)
                {
                    Form1.g_preset_data_2 = 0;
                }
                preset_data[2] = Form1.g_preset_data_2;
                if (int.TryParse(textBox_SettingPresetData3.Text, out Form1.g_preset_data_3) == false)
                {
                    Form1.g_preset_data_3 = 0;
                }
                preset_data[3] = Form1.g_preset_data_3;
                if (int.TryParse(textBox_SettingPresetData4.Text, out Form1.g_preset_data_4) == false)
                {
                    Form1.g_preset_data_4 = 0;
                }
                preset_data[4] = Form1.g_preset_data_4;
                //----------------------------------------
                // コンボボックスから比較繰り返し設定のインデックスを取得
                //----------------------------------------
                index = comboBox_SettingPresetRepeat.SelectedIndex;
                Form1.g_preset_repeat = (short)index;
                //----------------------------------------
                // 比較カウント一致後にロードされるプリセット値を設定
                //----------------------------------------
                ret = aio.SetCntPresetReg(Form1.g_id, Form1.g_channel, Form1.g_preset_data_num, preset_data, Form1.g_preset_repeat);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = "aio.SetCntPresetReg = " + ret.ToString() + "：" + error_string;
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // OK処理
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // Cancelボタンをクリックした際の処理になります
        // ダイアログを終了します
        //================================================================================
        private void button_Cancel_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Cancel処理
            //----------------------------------------
            this.Close();
        }
    }
}
