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

namespace MultiChannelWave
{
    public partial class DeviceSetting : Form
    {
        short   m_aochannels;
        float   m_frequency;
        short   m_frequencyunit_window;
        short   m_oscilation_type;
        short   m_oscilation_start;
        float   m_oscilation_stop;

        public DeviceSetting()
        {
            InitializeComponent();
        }

        //----------------------------------------
        // Data transfer (Set)
        //----------------------------------------
        internal void devinfo_data_set(short aochannels, float frequency, short frequencyunit, short oscilationtype, short oscilationstart, float oscilationstop)
        {
            m_aochannels            = aochannels;
            m_frequency             = frequency;
            m_frequencyunit_window  = frequencyunit;
            m_oscilation_type       = oscilationtype;
            m_oscilation_start      = oscilationstart;
            m_oscilation_stop       = oscilationstop;
        }

        //----------------------------------------
        // Data transfer (Get)
        //----------------------------------------
        internal void devinfo_data_get(ref short aochannels, ref float frequency, ref short frequencyunit, ref short oscilationtype, ref short oscilationstart, ref float oscilationstop)
        {
            aochannels              = m_aochannels;
            frequency               = m_frequency;
            frequencyunit           = m_frequencyunit_window;
            oscilationtype          = m_oscilation_type;
            oscilationstart         = m_oscilation_start;
            oscilationstop          = m_oscilation_stop;
        }

        //----------------------------------------
        // Initial display
        // Value is current setting value
        //----------------------------------------
        private void DeviceSetting_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // Update the initial display of frequency
            //----------------------------------------
            textBox_Frequency.Text = string.Format("{0:F4}", m_frequency);
            //----------------------------------------
            // Update the initial display of number of oscilation waveform
            //----------------------------------------
            textBox_StartOscilation.Text = string.Format("{0}", m_oscilation_start);
            //----------------------------------------
            // Update the initial display of number of oscilation stop waveform or time of oscillation stop
            //----------------------------------------
            textBox_StopOscilation.Text = string.Format("{0:F4}", m_oscilation_stop);
            //----------------------------------------
            // Create the combobox for number of channels
            //----------------------------------------
            for (int i = 0; i < Form1.CWAVEFORMGENERATOR_CHANNELS; i++)
            {
                comboBox_ChannelNum.Items.Add((i + 1).ToString());
            }
            //----------------------------------------
            // Change the selection of number of channels combobox
            //----------------------------------------
            comboBox_ChannelNum.SelectedIndex = (m_aochannels - 1);
            //----------------------------------------
            // Create the combobox for frequency unit
            //----------------------------------------
            comboBox_FrequencyUnit.Items.Add("Hz");
            comboBox_FrequencyUnit.Items.Add("kHz");
            comboBox_FrequencyUnit.Items.Add("sec");
            comboBox_FrequencyUnit.Items.Add("msec");
            //----------------------------------------
            // Change the selection of frequency unit combobox
            //----------------------------------------
            comboBox_FrequencyUnit.SelectedIndex = m_frequencyunit_window;
            //-------------------
            // Create the combobox for oscilation type
            //-------------------
            comboBox_OscilationType.Items.Add("None");
            comboBox_OscilationType.Items.Add("Auto burst");
            comboBox_OscilationType.Items.Add("Trigger burst(rising edge)");
            comboBox_OscilationType.Items.Add("Trigger burst(falling edge)");
            //-------------------
            // Change the selection of oscilation type combobox
            //-------------------
            comboBox_OscilationType.SelectedIndex = m_oscilation_type;
        }

        //================================================================================
        // It is the process when the OK button is clicked
        // Set the setting value
        //================================================================================
        private void button_Ok_Click(object sender, EventArgs e)
        {
            float   pre_frequency;          // Frequency
            short   pre_oscilation_start;   // Oscilation
            float   pre_oscilation_stop;    // Stop
            short   pre_channel_num;        // Number of channel
            short   pre_frequency_unit;     // Frequency unit
            short   pre_oscilation_type;    // Oscilation type
    
            //----------------------------------------
            // Get the number of channel from combobox
            // Number of channel starts from 1, so +1
            //----------------------------------------
            pre_channel_num = (short)(comboBox_ChannelNum.SelectedIndex + 1);
            //----------------------------------------
            // Get the frequency from textbox
            //----------------------------------------
            if (float.TryParse(textBox_Frequency.Text, out pre_frequency) == false)
            {
                pre_frequency = 0;
            }
            //----------------------------------------
            // Get the frequency unit from combobox
            //----------------------------------------
            pre_frequency_unit = (short)comboBox_FrequencyUnit.SelectedIndex;
            //----------------------------------------
            // Get the oscilation type from combobox
            //----------------------------------------
            pre_oscilation_type = (short)comboBox_OscilationType.SelectedIndex;
            //----------------------------------------
            // Get the oscilation from textbox
            //----------------------------------------
            if (short.TryParse(textBox_StartOscilation.Text, out pre_oscilation_start) == false)
            {
                pre_oscilation_start = 0;
            }
            //----------------------------------------
            // Get the stop from textbox
            //----------------------------------------
            if (float.TryParse(textBox_StopOscilation.Text, out pre_oscilation_stop) == false)
            {
                pre_oscilation_stop = 0;
            }

            //----------------------------------------
            // Transfer the value set in the child dialog to the main
            // Set on main
            //----------------------------------------
            m_aochannels            = pre_channel_num;
            m_frequency             = pre_frequency;
            m_frequencyunit_window  = pre_frequency_unit;
            m_oscilation_type       = pre_oscilation_type;
            m_oscilation_start      = pre_oscilation_start;
            m_oscilation_stop       = pre_oscilation_stop;
            this.Close();
        }

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