﻿using System;
using System.Text;

namespace CollectionConditions
{
    public partial class ChannelSetting : Form
    {
        ushort m_InputType;
        ushort m_AiChannels;
        ushort[] m_ChannelNo;
        byte[] m_ChName;
        ushort[] m_Range;
        ushort[] m_ScaleEnable;
        float[] m_MinData;
        float[] m_MaxData;
        float[] m_MinScale;
        float[] m_MaxScale;
        public ushort m_sequence_num;
        ushort[] m_AiRange;

        ComboBox[] m_combobox_Hwch;
        TextBox[] m_textbox_Chname;
        ComboBox[] m_combobox_Range;
        ComboBox[] m_combobox_ScaleEnable;
        TextBox[] m_textbox_MinData;
        TextBox[] m_textbox_MaxData;
        TextBox[] m_textbox_MinScale;
        TextBox[] m_textbox_MaxScale;

        public ChannelSetting()
        {
            InitializeComponent();
            int i;
            m_sequence_num = 4;
            m_ChannelNo = new ushort[m_sequence_num];
            m_ChName = new byte[m_sequence_num * 256];
            m_Range = new ushort[m_sequence_num];
            m_ScaleEnable = new ushort[m_sequence_num];
            m_MinData = new float[m_sequence_num];
            m_MaxData = new float[m_sequence_num];
            m_MinScale = new float[m_sequence_num];
            m_MaxScale = new float[m_sequence_num];
            m_combobox_Hwch = new ComboBox[m_sequence_num];
            m_textbox_Chname = new TextBox[m_sequence_num];
            m_combobox_Range = new ComboBox[m_sequence_num];
            m_combobox_ScaleEnable = new ComboBox[m_sequence_num];
            m_textbox_MinData = new TextBox[m_sequence_num];
            m_textbox_MaxData = new TextBox[m_sequence_num];
            m_textbox_MinScale = new TextBox[m_sequence_num];
            m_textbox_MaxScale = new TextBox[m_sequence_num];
            m_AiRange = new ushort[] {  (ushort)CLoggerConst.PM10,      // +/-10V
                                        (ushort)CLoggerConst.PM5,       // +/-5V
                                        (ushort)CLoggerConst.PM25,      // +/-2.5V
                                        (ushort)CLoggerConst.PM125,     // +/-1.25V
                                        (ushort)CLoggerConst.P10,       // 0-10V
                                        (ushort)CLoggerConst.P5,        // 0-5V
                                        (ushort)CLoggerConst.P25,       // 0-2.5V
                                        (ushort)CLoggerConst.P125,      // 0-1.25V
                                        (ushort)CLoggerConst.P20MA,     // 0-20mA
                                        (ushort)CLoggerConst.P4TO20MA,  // 4-20mA
                                        (ushort)CLoggerConst.PM20MA,    // +/-20mA
                                        (ushort)CLoggerConst.P1TO5};    // 1-5V
            for (i = 0; i < m_sequence_num; i++)
            {
                switch (i)
                {
                    case 0:
                        m_combobox_Hwch[i] = comboBox_ChannelNo1;
                        m_textbox_Chname[i] = textBox_ChName1;
                        m_combobox_Range[i] = comboBox_Range1;
                        m_combobox_ScaleEnable[i] = comboBox_ScaleEnable1;
                        m_textbox_MinData[i] = textBox_MinData1;
                        m_textbox_MaxData[i] = textBox_MaxData1;
                        m_textbox_MinScale[i] = textBox_MinScale1;
                        m_textbox_MaxScale[i] = textBox_MaxScale1;
                        break;
                    case 1:
                        m_combobox_Hwch[i] = comboBox_ChannelNo2;
                        m_textbox_Chname[i] = textBox_ChName2;
                        m_combobox_Range[i] = comboBox_Range2;
                        m_combobox_ScaleEnable[i] = comboBox_ScaleEnable2;
                        m_textbox_MinData[i] = textBox_MinData2;
                        m_textbox_MaxData[i] = textBox_MaxData2;
                        m_textbox_MinScale[i] = textBox_MinScale2;
                        m_textbox_MaxScale[i] = textBox_MaxScale2;
                        break;
                    case 2:
                        m_combobox_Hwch[i] = comboBox_ChannelNo3;
                        m_textbox_Chname[i] = textBox_ChName3;
                        m_combobox_Range[i] = comboBox_Range3;
                        m_combobox_ScaleEnable[i] = comboBox_ScaleEnable3;
                        m_textbox_MinData[i] = textBox_MinData3;
                        m_textbox_MaxData[i] = textBox_MaxData3;
                        m_textbox_MinScale[i] = textBox_MinScale3;
                        m_textbox_MaxScale[i] = textBox_MaxScale3;
                        break;
                    case 3:
                        m_combobox_Hwch[i] = comboBox_ChannelNo4;
                        m_textbox_Chname[i] = textBox_ChName4;
                        m_combobox_Range[i] = comboBox_Range4;
                        m_combobox_ScaleEnable[i] = comboBox_ScaleEnable4;
                        m_textbox_MinData[i] = textBox_MinData4;
                        m_textbox_MaxData[i] = textBox_MaxData4;
                        m_textbox_MinScale[i] = textBox_MinScale4;
                        m_textbox_MaxScale[i] = textBox_MaxScale4;
                        break;
                    default:
                        m_combobox_Hwch[i] = comboBox_ChannelNo1;
                        m_textbox_Chname[i] = textBox_ChName1;
                        m_combobox_Range[i] = comboBox_Range1;
                        m_combobox_ScaleEnable[i] = comboBox_ScaleEnable1;
                        m_textbox_MinData[i] = textBox_MinData1;
                        m_textbox_MaxData[i] = textBox_MaxData1;
                        m_textbox_MinScale[i] = textBox_MinScale1;
                        m_textbox_MaxScale[i] = textBox_MaxScale1;
                        break;
                }
            }
        }

        //================================================================================
        // Pass the data (Set)
        //================================================================================
        internal void channel_data_set(ushort InputType, ushort AiChannels, ushort[] ChannelNo, byte[] ChName, ushort[] Range, ushort[] ScaleEnable, float[] MinData, float[] MaxData, float[] MinScale, float[] MaxScale)
        {
            m_InputType = InputType;
            m_AiChannels = AiChannels;
            m_ChannelNo = ChannelNo;
            m_ChName = ChName;
            m_Range = Range;
            m_ScaleEnable = ScaleEnable;
            m_MinData = MinData;
            m_MaxData = MaxData;
            m_MinScale = MinScale;
            m_MaxScale = MaxScale;
        }

        //================================================================================
        // Pass the data (Get)
        //================================================================================
        internal void channel_data_get(ref ushort InputType, ref ushort AiChannels, ref ushort[] ChannelNo, ref byte[] ChName, ref ushort[] Range, ref ushort[] ScaleEnable, ref float[] MinData, ref float[] MaxData, ref float[] MinScale, ref float[] MaxScale)
        {
            InputType = m_InputType;
            AiChannels = m_AiChannels;
            ChannelNo = m_ChannelNo;
            ChName = m_ChName;
            Range = m_Range;
            ScaleEnable = m_ScaleEnable;
            MinData = m_MinData;
            MaxData = m_MaxData;
            MinScale = m_MinScale;
            MaxScale = m_MaxScale;
        }

        //================================================================================
        // It is the process when the Apply button is clicked
        // Confirm the setting value
        //================================================================================
        private void button_OK_Click(object sender, EventArgs e)
        {
            int i;              // Loop count

            //----------------------------------------
            // Get the input method from combobox
            //----------------------------------------
            m_InputType = (ushort)comboBox_InputType.SelectedIndex;
            //----------------------------------------
            // Get the number of channels from combobox(index + 1)
            //----------------------------------------
            m_AiChannels = (ushort)(comboBox_AiChannels.SelectedIndex + 1);
            for (i = 0; i < m_sequence_num; i++)
            {
                //----------------------------------------
                // Get the HWch from combobox
                //----------------------------------------
                m_ChannelNo[i] = (ushort)m_combobox_Hwch[i].SelectedIndex;
                //----------------------------------------
                // Get the channel name from textbox
                //----------------------------------------
                byte[] bytes = Encoding.GetEncoding("shift_jis").GetBytes(m_textbox_Chname[i].Text);
                int startPos = i * 256;
                int copyLen = bytes.Length > 256 ? 256 : bytes.Length;
                Array.Clear(m_ChName, startPos, 256);
                Array.Copy(bytes, 0, m_ChName, startPos, copyLen);
                //----------------------------------------
                // Get the range from combobox
                //----------------------------------------
                m_Range[i] = m_AiRange[m_combobox_Range[i].SelectedIndex];
                //----------------------------------------
                // Get the scale enable/disable from combobox
                //----------------------------------------
                m_ScaleEnable[i] = (ushort)m_combobox_ScaleEnable[i].SelectedIndex;
                //----------------------------------------
                // Get the data 1 from textbox
                //----------------------------------------
                if (float.TryParse(m_textbox_MinData[i].Text, out m_MinData[i]) == false)
                {
                    m_MinData[i] = 0;
                }
                //----------------------------------------
                // Get the data 2 from textbox
                //----------------------------------------
                if (float.TryParse(m_textbox_MaxData[i].Text, out m_MaxData[i]) == false)
                {
                    m_MaxData[i] = 0;
                }
                //----------------------------------------
                // Get the minimum scale from textbox
                //----------------------------------------
                if (float.TryParse(m_textbox_MinScale[i].Text, out m_MinScale[i]) == false)
                {
                    m_MinScale[i] = 0;
                }
                //----------------------------------------
                // Get the maximum scale from textbox
                //----------------------------------------
                if (float.TryParse(m_textbox_MaxScale[i].Text, out m_MaxScale[i]) == false)
                {
                    m_MaxScale[i] = 0;
                }
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

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

        //================================================================================
        // Initial settings
        //================================================================================
        private void ChannelSetting_Load(object sender, EventArgs e)
        {
            int i, j;  // Loop count

            //----------------------------------------
            // Create the combobox for input method
            //----------------------------------------
            comboBox_InputType.Items.Add("Single-end input");
            comboBox_InputType.Items.Add("Differential input");
            //----------------------------------------
            // Update the selection of input method combobox
            //----------------------------------------
            comboBox_InputType.SelectedIndex = m_InputType;
            //----------------------------------------
            // Reset the combobox for number of channles
            //----------------------------------------
            comboBox_AiChannels.Items.Clear();
            //----------------------------------------
            // Create the combobox for number of channles
            //----------------------------------------
            comboBox_AiChannels.Items.Add("1");
            comboBox_AiChannels.Items.Add("2");
            //----------------------------------------
            // For single-end input
            //----------------------------------------
            if (comboBox_InputType.SelectedIndex == (int)CLoggerConst.LOGGER_PARAM_SINGLEENDED)
            {
                comboBox_AiChannels.Items.Add("3");
                comboBox_AiChannels.Items.Add("4");
            }
            //----------------------------------------
            // Update the selection of number of channles combobox(Number of channles - 1)
            //----------------------------------------
            if (m_AiChannels > 0)
            {
                comboBox_AiChannels.SelectedIndex = m_AiChannels - 1;
            }
            else
            {
                comboBox_AiChannels.SelectedIndex = 0;
            }
            for (i = 0; i < m_sequence_num; i++)
            {
                //----------------------------------------
                // Create the combobox for HWCh
                //----------------------------------------
                m_combobox_Hwch[i].Items.Add("0");
                m_combobox_Hwch[i].Items.Add("1");
                m_combobox_Hwch[i].Items.Add("2");
                m_combobox_Hwch[i].Items.Add("3");
                //----------------------------------------
                // Update the selection of HWCh combobox
                //----------------------------------------
                m_combobox_Hwch[i].SelectedIndex = m_ChannelNo[i];
                //----------------------------------------
                // Update the display of channel name
                //----------------------------------------
                int startPos = i * 256;
                int nullPos = Array.IndexOf(m_ChName, 0, startPos, 256);
                int copyLen = nullPos == -1 ? 256 : nullPos - startPos;
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                m_textbox_Chname[i].Text = Encoding.GetEncoding("shift_jis").GetString(m_ChName, startPos, copyLen);
                //----------------------------------------
                // Create the combobox for analog input range
                //----------------------------------------
                m_combobox_Range[i].Items.Add("-10 - +10V");
                m_combobox_Range[i].Items.Add("-5 - +5V");
                m_combobox_Range[i].Items.Add("-2.5 - +2.5V");
                m_combobox_Range[i].Items.Add("-1.25 - +1.25V");
                m_combobox_Range[i].Items.Add("0 - +10V");
                m_combobox_Range[i].Items.Add("0 - +5V");
                m_combobox_Range[i].Items.Add("0 - +2.5V");
                m_combobox_Range[i].Items.Add("0 - +1.25V");
                m_combobox_Range[i].Items.Add("0 - +20mA");
                m_combobox_Range[i].Items.Add("4 - +20mA");
                m_combobox_Range[i].Items.Add("-20 - +20mA");
                m_combobox_Range[i].Items.Add("1 - +5V");
                //----------------------------------------
                // Update the selection of analog input range combobox
                //----------------------------------------
                m_combobox_Range[i].SelectedIndex = 0;
                for (j = 0; j < m_AiRange.Length; j++)
                {
                    if (m_Range[i] == m_AiRange[j])
                    {
                        m_combobox_Range[i].SelectedIndex = j;
                    }
                }
                //----------------------------------------
                // Create the combobox for scale enable/disable
                //----------------------------------------
                m_combobox_ScaleEnable[i].Items.Add("Disable");
                m_combobox_ScaleEnable[i].Items.Add("Enable");
                //----------------------------------------
                // Update the selection of scale enable/disable combobox
                //----------------------------------------
                m_combobox_ScaleEnable[i].SelectedIndex = m_ScaleEnable[i];
                //----------------------------------------
                // Update the display of data 1
                //----------------------------------------
                m_textbox_MinData[i].Text = string.Format("{0}", m_MinData[i]);
                //----------------------------------------
                // Update the display of data 2
                //----------------------------------------
                m_textbox_MaxData[i].Text = string.Format("{0}", m_MaxData[i]);
                //----------------------------------------
                // Update the display of minimum scale
                //----------------------------------------
                m_textbox_MinScale[i].Text = string.Format("{0}", m_MinScale[i]);
                //----------------------------------------
                // Update the display of maximum scale
                //----------------------------------------
                m_textbox_MaxScale[i].Text = string.Format("{0}", m_MaxScale[i]);
            }
            //----------------------------------------
            // Call the number of channels combobox change event to update the display
            //----------------------------------------
            comboBox_AiChannels_SelectedIndexChanged(comboBox_AiChannels, EventArgs.Empty);
        }

        //================================================================================
        // It is the process when the value of input method combobox is changed
        // Check the setting value and change the setting range for the number of channels
        //================================================================================
        private void comboBox_InputType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int input_type; // Input method

            //----------------------------------------
            // Get the input method from combobox
            //----------------------------------------
            input_type = comboBox_InputType.SelectedIndex;
            //----------------------------------------
            // Reset the number of channels combobox
            //----------------------------------------
            comboBox_AiChannels.Items.Clear();
            //----------------------------------------
            // Create the combobox for number of channels
            //----------------------------------------
            comboBox_AiChannels.Items.Add("1");
            comboBox_AiChannels.Items.Add("2");
            //----------------------------------------
            // For single-end input
            //----------------------------------------
            if (comboBox_InputType.SelectedIndex == (int)CLoggerConst.LOGGER_PARAM_SINGLEENDED)
            {
                comboBox_AiChannels.Items.Add("3");
                comboBox_AiChannels.Items.Add("4");
            }
            //----------------------------------------
            // Select the combobox for number of channels
            //----------------------------------------
            comboBox_AiChannels.SelectedIndex = 0;
            //----------------------------------------
            // Call the number of channels combobox change event to update the display
            //----------------------------------------
            comboBox_AiChannels_SelectedIndexChanged(comboBox_AiChannels, EventArgs.Empty);
            return;
        }

        //================================================================================
        // It is the process when the value of number of channels combobox is changed
        // Check the setting value and enable/disable setting operations that are less than the number of channels
        //================================================================================
        private void comboBox_AiChannels_SelectedIndexChanged(object sender, EventArgs e)
        {
            int scale_enable;                           // Scale conversion
            int i;                                      // Loop count
            bool[] sequence = new bool[m_sequence_num]; // Sequence enable/disable
            ushort ai_channels;                         // Number of channels

            //----------------------------------------
            // Get the input method from combobox(index + 1)
            //----------------------------------------
            ai_channels = (ushort)(comboBox_AiChannels.SelectedIndex + 1);
            //----------------------------------------
            // Loop with the number of sequences
            //----------------------------------------
            for (i = 0; i < m_sequence_num; i++)
            {
                //----------------------------------------
                // Disable settings that are less than the number of channels
                //----------------------------------------
                if (i < ai_channels)
                {
                    sequence[i] = true;
                }
                else
                {
                    sequence[i] = false;
                }
                m_combobox_Hwch[i].Enabled = sequence[i];
                m_combobox_Range[i].Enabled = sequence[i];
                m_textbox_Chname[i].Enabled = sequence[i];
                m_combobox_ScaleEnable[i].Enabled = sequence[i];
                //----------------------------------------
                // Disable the scale setting operation
                //----------------------------------------
                if (sequence[i] == true)
                {
                    scale_enable = m_combobox_ScaleEnable[i].SelectedIndex;
                    if (scale_enable == (int)CLoggerConst.LOGGER_PARAM_DISABLE)
                    {
                        m_textbox_MinData[i].Enabled = false;
                        m_textbox_MaxData[i].Enabled = false;
                        m_textbox_MinScale[i].Enabled = false;
                        m_textbox_MaxScale[i].Enabled = false;
                    }
                    else if (scale_enable == (int)CLoggerConst.LOGGER_PARAM_ENABLE)
                    {
                        m_textbox_MinData[i].Enabled = true;
                        m_textbox_MaxData[i].Enabled = true;
                        m_textbox_MinScale[i].Enabled = true;
                        m_textbox_MaxScale[i].Enabled = true;
                    }
                    else
                    {
                        m_textbox_MinData[i].Enabled = true;
                        m_textbox_MaxData[i].Enabled = true;
                        m_textbox_MinScale[i].Enabled = true;
                        m_textbox_MaxScale[i].Enabled = true;
                    }
                }
                else
                {
                    m_textbox_MinData[i].Enabled = sequence[i];
                    m_textbox_MaxData[i].Enabled = sequence[i];
                    m_textbox_MinScale[i].Enabled = sequence[i];
                    m_textbox_MaxScale[i].Enabled = sequence[i];
                }
            }
        }

        //================================================================================
        // It is the process when the value of scale conversion combobox is changed
        // Check the setting value and enable/disable the scale settings
        //================================================================================
        private void comboBox_ScaleEnable1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int scale_enable;   // Scale conversion

            //----------------------------------------
            // Get the scale conversion
            //----------------------------------------
            scale_enable = comboBox_ScaleEnable1.SelectedIndex;
            if (scale_enable == (int)CLoggerConst.LOGGER_PARAM_DISABLE)
            {
                //----------------------------------------
                // Disable
                //----------------------------------------
                textBox_MinData1.Enabled = false;
                textBox_MaxData1.Enabled = false;
                textBox_MinScale1.Enabled = false;
                textBox_MaxScale1.Enabled = false;
            }
            else if (scale_enable == (int)CLoggerConst.LOGGER_PARAM_ENABLE)
            {
                //----------------------------------------
                // Enable
                //----------------------------------------
                textBox_MinData1.Enabled = true;
                textBox_MaxData1.Enabled = true;
                textBox_MinScale1.Enabled = true;
                textBox_MaxScale1.Enabled = true;
            }
            else
            {
                //----------------------------------------
                // Enable
                //----------------------------------------
                textBox_MinData1.Enabled = true;
                textBox_MaxData1.Enabled = true;
                textBox_MinScale1.Enabled = true;
                textBox_MaxScale1.Enabled = true;
            }
        }

        //================================================================================
        // It is the process when the value of scale conversion combobox is changed
        // Check the setting value and enable/disable the scale settings
        //================================================================================
        private void comboBox_ScaleEnable2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int scale_enable;   // Scale conversion

            //----------------------------------------
            // Get the scale conversion
            //----------------------------------------
            scale_enable = comboBox_ScaleEnable2.SelectedIndex;
            if (scale_enable == (int)CLoggerConst.LOGGER_PARAM_DISABLE)
            {
                //----------------------------------------
                // Disable
                //----------------------------------------
                textBox_MinData2.Enabled = false;
                textBox_MaxData2.Enabled = false;
                textBox_MinScale2.Enabled = false;
                textBox_MaxScale2.Enabled = false;
            }
            else if (scale_enable == (int)CLoggerConst.LOGGER_PARAM_ENABLE)
            {
                //----------------------------------------
                // Enable
                //----------------------------------------
                textBox_MinData2.Enabled = true;
                textBox_MaxData2.Enabled = true;
                textBox_MinScale2.Enabled = true;
                textBox_MaxScale2.Enabled = true;
            }
            else
            {
                //----------------------------------------
                // Enable
                //----------------------------------------
                textBox_MinData2.Enabled = true;
                textBox_MaxData2.Enabled = true;
                textBox_MinScale2.Enabled = true;
                textBox_MaxScale2.Enabled = true;
            }
        }

        //================================================================================
        // It is the process when the value of scale conversion combobox is changed
        // Check the setting value and enable/disable the scale settings
        //================================================================================
        private void comboBox_ScaleEnable3_SelectedIndexChanged(object sender, EventArgs e)
        {
            int scale_enable;   // Scale conversion

            //----------------------------------------
            // Get the scale conversion
            //----------------------------------------
            scale_enable = comboBox_ScaleEnable3.SelectedIndex;
            if (scale_enable == (int)CLoggerConst.LOGGER_PARAM_DISABLE)
            {
                //----------------------------------------
                // Disable
                //----------------------------------------
                textBox_MinData3.Enabled = false;
                textBox_MaxData3.Enabled = false;
                textBox_MinScale3.Enabled = false;
                textBox_MaxScale3.Enabled = false;
            }
            else if (scale_enable == (int)CLoggerConst.LOGGER_PARAM_ENABLE)
            {
                //----------------------------------------
                // Enable
                //----------------------------------------
                textBox_MinData3.Enabled = true;
                textBox_MaxData3.Enabled = true;
                textBox_MinScale3.Enabled = true;
                textBox_MaxScale3.Enabled = true;
            }
            else
            {
                //----------------------------------------
                // Enable
                //----------------------------------------
                textBox_MinData3.Enabled = true;
                textBox_MaxData3.Enabled = true;
                textBox_MinScale3.Enabled = true;
                textBox_MaxScale3.Enabled = true;
            }
        }

        //================================================================================
        // It is the process when the value of scale conversion combobox is changed
        // Check the setting value and enable/disable the scale settings
        //================================================================================
        private void comboBox_ScaleEnable4_SelectedIndexChanged(object sender, EventArgs e)
        {
            int scale_enable;   // Scale conversion

            //----------------------------------------
            // Get the scale conversion
            //----------------------------------------
            scale_enable = comboBox_ScaleEnable4.SelectedIndex;
            if (scale_enable == (int)CLoggerConst.LOGGER_PARAM_DISABLE)
            {
                //----------------------------------------
                // Disable
                //----------------------------------------
                textBox_MinData4.Enabled = false;
                textBox_MaxData4.Enabled = false;
                textBox_MinScale4.Enabled = false;
                textBox_MaxScale4.Enabled = false;
            }
            else if (scale_enable == (int)CLoggerConst.LOGGER_PARAM_ENABLE)
            {
                //----------------------------------------
                // Enable
                //----------------------------------------
                textBox_MinData4.Enabled = true;
                textBox_MaxData4.Enabled = true;
                textBox_MinScale4.Enabled = true;
                textBox_MaxScale4.Enabled = true;
            }
            else
            {
                //----------------------------------------
                // Enable
                //----------------------------------------
                textBox_MinData4.Enabled = true;
                textBox_MaxData4.Enabled = true;
                textBox_MinScale4.Enabled = true;
                textBox_MaxScale4.Enabled = true;
            }
        }
    }
}
