﻿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
    {
        ushort m_aochannels;
        float  m_frequency;
        ushort m_frequencyunit_window;
        ushort m_oscillation_type;
        ushort m_oscillation_start;
        float  m_oscillation_stop;

        public DeviceSetting()
        {
            InitializeComponent();
        }

        //================================================================================
        // Data transfer (Set)
        //================================================================================
        internal void devinfo_data_set(ushort aochannels, float frequency, ushort frequencyunit, ushort oscillationtype, ushort oscillationstart, float oscillationstop)
        {
            m_aochannels           = aochannels;
            m_frequency            = frequency;
            m_frequencyunit_window = frequencyunit;
            m_oscillation_type     = oscillationtype;
            m_oscillation_start    = oscillationstart;
            m_oscillation_stop     = oscillationstop;
        }

        //================================================================================
        // Data transfer (Get)
        //================================================================================
        internal void devinfo_data_get(ref ushort aochannels, ref float frequency, ref ushort frequencyunit, ref ushort oscillationtype, ref ushort oscillationstart, ref float oscillationstop)
        {
            aochannels       = m_aochannels;
            frequency        = m_frequency;
            frequencyunit    = m_frequencyunit_window;
            oscillationtype  = m_oscillation_type;
            oscillationstart = m_oscillation_start;
            oscillationstop  = m_oscillation_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 oscillation waveform
            //----------------------------------------
            textBox_StartOscillation.Text = string.Format("{0}", m_oscillation_start);
            //----------------------------------------
            // Update the initial display of number of oscillation stop waveform or time of oscillation stop
            //----------------------------------------
            textBox_StopOscillation.Text = string.Format("{0:F4}", m_oscillation_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 oscillation type
            //----------------------------------------
            comboBox_OscillationType.Items.Add("None");
            comboBox_OscillationType.Items.Add("Auto burst");
            comboBox_OscillationType.Items.Add("Trigger burst(rising edge)");
            comboBox_OscillationType.Items.Add("Trigger burst(falling edge)");
            //----------------------------------------
            // Change the selection of oscillation type combobox
            //----------------------------------------
            comboBox_OscillationType.SelectedIndex = m_oscillation_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
            ushort pre_oscillation_start;    // Oscillation
            float  pre_oscillation_stop;     // Stop
            ushort pre_channel_num;          // Number of channel
            ushort pre_frequency_unit;       // Frequency unit
            ushort pre_oscillation_type;     // Oscillation type

            //----------------------------------------
            // Get the number of channel from combobox
            // Number of channel starts from 1, so +1
            //----------------------------------------
            pre_channel_num = (ushort)(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 = (ushort)comboBox_FrequencyUnit.SelectedIndex;
            //----------------------------------------
            // Get the oscillation type from combobox
            //----------------------------------------
            pre_oscillation_type = (ushort)comboBox_OscillationType.SelectedIndex;
            //----------------------------------------
            // Get the oscillation from textbox
            //----------------------------------------
            if (ushort.TryParse(textBox_StartOscillation.Text, out pre_oscillation_start) == false)
            {
                pre_oscillation_start = 0;
            }
            //----------------------------------------
            // Get the stop from textbox
            //----------------------------------------
            if (float.TryParse(textBox_StopOscillation.Text, out pre_oscillation_stop) == false)
            {
                pre_oscillation_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_oscillation_type     = pre_oscillation_type;
            m_oscillation_start    = pre_oscillation_start;
            m_oscillation_stop     = pre_oscillation_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();
        }
    }
}
