﻿using CaioCs;

namespace Counter
{
    public partial class Setting : Form
    {
        //----------------------------------------
        // Create instance Caio class
        //----------------------------------------
        Caio aio = new Caio();

        public Setting()
        {
            InitializeComponent();
        }

        private void Setting_Load(object sender, EventArgs e)
        {
            string error_string;      // Error code string
            string message_string;    // For displaying message
            int    ret;               // For return code
            int    index;             // Index of combobox
            short  cnt_mode;          // Operation mode
            short  err_flag;          // Function execution error flag

            //----------------------------------------
            // Initialize parameters
            //----------------------------------------
            err_flag = 0;
            message_string = "";
            //----------------------------------------
            // Set the counter operation as comparison count match occurs
            //----------------------------------------
            ret = aio.GetCntComparisonMode(Form1.g_id, Form1.g_channel, out cnt_mode);
            //----------------------------------------
            // Abnormal termination process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                message_string = "aio.GetCntComparisonMode = " + ret.ToString() + " : " + error_string + "\r\n\r\n*The display could not be updated since an error occurred.";
                err_flag = 1;
                //----------------------------------------
                // Set comparison count
                //----------------------------------------
                checkBox_CompareLoad.Checked = false;
                //----------------------------------------
                // Set comparison match preset
                //----------------------------------------
                checkBox_PresetLoad.Checked = false;
            }
            //----------------------------------------
            // Normal termination process
            //----------------------------------------
            else
            {
                //--------------------
                // Set operation mode
                //--------------------
                switch (cnt_mode)
                {
                    case 0:
                        //----------------------------------------
                        // Set comparison count
                        //----------------------------------------
                        checkBox_CompareLoad.Checked = false;
                        //----------------------------------------
                        // Set comparison match preset
                        //----------------------------------------
                        checkBox_PresetLoad.Checked = false;
                        break;
                    case (short)CaioConst.CNT_LOADPRESET:
                        //----------------------------------------
                        // Set comparison count
                        //----------------------------------------
                        checkBox_CompareLoad.Checked = false;
                        //----------------------------------------
                        // Set comparison match preset
                        //----------------------------------------
                        checkBox_PresetLoad.Checked = true;
                        break;
                    case (short)CaioConst.CNT_LOADCOMP:
                        //----------------------------------------
                        // Set comparison count
                        //----------------------------------------
                        checkBox_CompareLoad.Checked = true;
                        //----------------------------------------
                        // Set comparison match preset
                        //----------------------------------------
                        checkBox_PresetLoad.Checked = false;
                        break;
                    case (short)(CaioConst.CNT_LOADPRESET | CaioConst.CNT_LOADCOMP):
                        //----------------------------------------
                        // Set comparison count
                        //----------------------------------------
                        checkBox_CompareLoad.Checked = true;
                        //----------------------------------------
                        // Set comparison match preset
                        //----------------------------------------
                        checkBox_PresetLoad.Checked = true;
                        break;
                    default:
                        //----------------------------------------
                        // Set comparison count
                        //----------------------------------------
                        checkBox_CompareLoad.Checked = false;
                        //----------------------------------------
                        // Set comparison match preset
                        //----------------------------------------
                        checkBox_PresetLoad.Checked = false;
                        break;
                }
            }
            //----------------------------------------
            // Updated available controls by operation mode
            //----------------------------------------
            checkBox_CompareLoad_CheckedChanged(new object(), new EventArgs());
            checkBox_PresetLoad_CheckedChanged(new object(), new EventArgs());
            //----------------------------------------
            // Comparison Count Set: Create the combobox for the number of setting data
            //----------------------------------------
            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");
            //----------------------------------------
            // Comparison Count Set: Change the selection of the combobox for the number of setting data
            //----------------------------------------
            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;
            }
            //----------------------------------------
            // Comparison Count Set: Create the combobox for the repeat setting
            //----------------------------------------
            comboBox_SettingCompareRepeat.Items.Add("Not repeat");
            comboBox_SettingCompareRepeat.Items.Add("Repeat");
            //----------------------------------------
            // Comparison Count Set: Change the selection of the combobox for the repeat setting
            //----------------------------------------
            index = Form1.g_compare_repeat;
            comboBox_SettingCompareRepeat.SelectedIndex = index;
            //----------------------------------------
            // Comparison Count Set: Update comparison count data
            //----------------------------------------
            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();
            //----------------------------------------
            // Comparison Match Preset Set: Create the combobox for the number of setting data
            //----------------------------------------
            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");
            //----------------------------------------
            // Comparison Match Preset Set: Change the selection of the combobox for the number of setting data
            //----------------------------------------
            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;
            }
            //----------------------------------------
            // Comparison Match Preset Set: Create the combobox for the repeat setting
            //----------------------------------------
            comboBox_SettingPresetRepeat.Items.Add("Not repeat");
            comboBox_SettingPresetRepeat.Items.Add("Repeat");
            //----------------------------------------
            // Comparison Match Preset Set: Change the selection of the combobox for the repeat setting
            //----------------------------------------
            index = Form1.g_preset_repeat;
            comboBox_SettingPresetRepeat.SelectedIndex = index;
            //----------------------------------------
            // Comparison Match Preset Set: Update preset data
            //----------------------------------------
            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();
            //----------------------------------------
            // Display message if an error occurs
            //----------------------------------------
            if (err_flag != 0)
            {
                MessageBox.Show(message_string, "Error", MessageBoxButtons.OK);
            }
        }

        //================================================================================
        // It is the process when the Comparison Count Set checkobox is changed
        // Update available controls
        //================================================================================
        private void checkBox_CompareLoad_CheckedChanged(object sender, EventArgs e)
        {
            //----------------------------------------
            // Update available controls
            //----------------------------------------
            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;
            }
        }

        //================================================================================
        // It is the process when the Number of Setting Data combobox of Comparison Count Set is changed
        // Update available controls
        //================================================================================
        private void comboBox_SettingCompareDataNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index;       // Index of combobox
            int data_num;    // Number of setting data

            //----------------------------------------
            // Get the index of number of data from combobox
            //----------------------------------------
            index = comboBox_SettingCompareDataNum.SelectedIndex;
            //----------------------------------------
            // Branch processing by number of data
            //----------------------------------------
            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;
            }
        }

        //================================================================================
        // It is the process when the Comparison Match Preset Set checkobox is changed
        // Update available controls
        //================================================================================
        private void checkBox_PresetLoad_CheckedChanged(object sender, EventArgs e)
        {
            //----------------------------------------
            // Update available controls
            //----------------------------------------
            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;
            }
        }

        //================================================================================
        // It is the process when the Number of Setting Data combobox of Comparison Match Preset Set is changed
        // Update available controls
        //================================================================================
        private void comboBox_SettingPresetDataNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index;       // Index of combobox
            int data_num;    // Number of setting data

            //----------------------------------------
            // Get the index of number of data from combobox
            //----------------------------------------
            index = comboBox_SettingPresetDataNum.SelectedIndex;
            //----------------------------------------
            // Branch processing by number of data
            //----------------------------------------
            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;
            }
        }

        //================================================================================
        // It is the process when the OK button is clicked
        // Set counter
        //================================================================================
        private void button_OK_Click(object sender, EventArgs e)
        {
            string error_string;                    // Error code string
            string text_string;                     // For updating the textbox
            int    ret;                             // For return code
            short  cnt_mode;                        // Operation mode
            int    index;                           // Index of combobox
            int[]  comparison_data = new int[8];    // Comparison count data
            int[]  preset_data    = new int[8];     // Preset data of counter

            //----------------------------------------
            // Get the counter operation mode
            //----------------------------------------
            cnt_mode = (short)((checkBox_PresetLoad.Checked == true ? (short)CaioConst.CNT_LOADPRESET : 0) | (checkBox_CompareLoad.Checked == true ? (short)CaioConst.CNT_LOADCOMP : 0));
            //----------------------------------------
            // Set the counter operation mode
            //----------------------------------------
            ret = aio.SetCntComparisonMode(Form1.g_id, Form1.g_channel, cnt_mode);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                text_string = "aio.SetCntComparisonMode = " + ret.ToString() + " : " + error_string;
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Set the comparison count data
            //----------------------------------------
            if (checkBox_CompareLoad.Checked == true)
            {
                //----------------------------------------
                // Get the index of number of data from combobox
                //----------------------------------------
                index = comboBox_SettingCompareDataNum.SelectedIndex;
                Form1.g_compare_data_num = (index + 1);
                //----------------------------------------
                // Get counter comparison count data from textbox
                //----------------------------------------
                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;
                //----------------------------------------
                // Get the index of the repeat setting from combobox
                //----------------------------------------
                index = comboBox_SettingCompareRepeat.SelectedIndex;
                Form1.g_compare_repeat = (short)index;
                //----------------------------------------
                // Set the next comparison count value loaded after comparison count match
                //----------------------------------------
                ret = aio.SetCntComparisonReg(Form1.g_id, Form1.g_channel, Form1.g_compare_data_num, comparison_data, Form1.g_compare_repeat);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = "aio.SetCntComparisonReg = " + ret.ToString() + " : " + error_string;
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // Set the preset load data
            //----------------------------------------
            if (checkBox_PresetLoad.Checked == true)
            {
                //----------------------------------------
                // Get the index of number of data from combobox
                //----------------------------------------
                index = comboBox_SettingPresetDataNum.SelectedIndex;
                Form1.g_preset_data_num = (index + 1);
                //----------------------------------------
                // Get counter preset data from textbox
                //----------------------------------------
                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;
                //----------------------------------------
                // Get the index of the repeat setting from combobox
                //----------------------------------------
                index = comboBox_SettingPresetRepeat.SelectedIndex;
                Form1.g_preset_repeat = (short)index;
                //----------------------------------------
                // Set the preset value loaded after the comparison count match
                //----------------------------------------
                ret = aio.SetCntPresetReg(Form1.g_id, Form1.g_channel, Form1.g_preset_data_num, preset_data, Form1.g_preset_repeat);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = "aio.SetCntPresetReg = " + ret.ToString() + " : " + error_string;
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // OK process
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // It is the process when the Close button is clicked
        // Terminate the dialog
        //================================================================================
        private void button_Cancel_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Cancel process
            //----------------------------------------
            this.Close();
        }
    }
}
