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

namespace FileOperation
{
    public partial class Form1 : Form
    {
        CCWaveformGeneratorClass c_fgen;

        //----------------------------------------
        // Macro definition
        //----------------------------------------
        public const int AO_CHANNEL = 0;                        // Analog output channel number: 0
        public const int CWAVEFORMGENERATOR_CHANNELS = 16;      // Max channels
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // Initial processing to call C-WaveFormGenerator with SDK
            //----------------------------------------
            try
            {
                //----------------------------------------
                // Create the class of the C-WaveformGenerator
                //----------------------------------------
                c_fgen = new CCWaveformGeneratorClass();
            }
            //----------------------------------------
            // Display error message in case of errors
            //----------------------------------------
            catch
            {
                MessageBox.Show("Unanticipated exception creating CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                this.Close();
                return;
            }
            //----------------------------------------
            // Update the initial display of device name
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // Create the combobox for screen size
            //----------------------------------------
            comboBox_OpenMode.Items.Add("Minimize");
            comboBox_OpenMode.Items.Add("Normal");
            comboBox_OpenMode.Items.Add("Maximize");
            //----------------------------------------
            // Change the selection of screen size combobox to 0
            //----------------------------------------
            comboBox_OpenMode.SelectedIndex = 0;
            //----------------------------------------
            // Create the combobox for channel
            //----------------------------------------
            for (int i = 0; i < CWAVEFORMGENERATOR_CHANNELS; i++)
            {
                comboBox_AoChannel.Items.Add(i.ToString());
            }
            //----------------------------------------
            // Change the selection of channel combobox to 0
            //----------------------------------------
            comboBox_AoChannel.SelectedIndex = 0;
            //----------------------------------------
            // Create the combobox for wave type
            //----------------------------------------
            comboBox_WaveType.Items.Add("Sinewave");
            comboBox_WaveType.Items.Add("Constant");
            comboBox_WaveType.Items.Add("Triangular");
            comboBox_WaveType.Items.Add("Ramp");
            comboBox_WaveType.Items.Add("Square");
            comboBox_WaveType.Items.Add("Noise");
            comboBox_WaveType.Items.Add("Arbitrary");
            //----------------------------------------
            // Change the selection of wave type combobox to 0
            //----------------------------------------
            comboBox_WaveType.SelectedIndex = 0;
            //----------------------------------------
            // Create the combobox for data save
            //----------------------------------------
            comboBox_Param.Items.Add("Single channel");
            comboBox_Param.Items.Add("Multiple channels");
            //----------------------------------------
            // Change the selection of data save combobox to 0
            //----------------------------------------
            comboBox_Param.SelectedIndex = 0;
        }

        //================================================================================
        // It is the process when the OpenApplication button is clicked
        // Initialize the application
        //================================================================================
        private void button_OpenApplication_Click(object sender, EventArgs e)
        {
            string  device_name;         // Device name
            string  arg1;                // Reserve
            short   openmode;            // Screen size
            long    ret;                 // For return code

            //----------------------------------------
            // Get device name from textbox
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // Get screen size from textbox
            //----------------------------------------
            openmode = (short)comboBox_OpenMode.SelectedIndex;
            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = "";
            //----------------------------------------
            // Perform application initialization process
            //----------------------------------------
            try
            {
                ret = c_fgen.OpenApplication(openmode, device_name, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("OpenApplication = {0} ", ret);
        }

        //================================================================================
        // It is the process when the SetDeviceName button is clicked
        // Set the device name
        //================================================================================
        private void button_SetDeviceName_Click(object sender, EventArgs e)
        {
            string  device_name;         // Device name
            long    ret;                 // For return code

            //----------------------------------------
            // Get setting file name from textbox
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // Set device name
            //----------------------------------------
            try
            {
                ret = c_fgen.SetDeviceName(device_name);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SetDeviceName = {0} ", ret);
        }

        //================================================================================
        // It is the process when the ... button is clicked (Setting file)
        // Displays the dialog to select the ini file
        //================================================================================
        private void button_SettingSelectFile_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Don't check if filename exists
            //----------------------------------------
            openFileDialog1.CheckFileExists = false;
            //----------------------------------------
            // If the path exists, set the current path to the initial path of dialog
            //----------------------------------------
            if (textBox_SettingFile.Text.Length > 0)
            {
                openFileDialog1.InitialDirectory = textBox_SettingFile.Text;
                openFileDialog1.FileName = openFileDialog1.SafeFileName;
            }
            //----------------------------------------
            // If the path not exists, set the MyDocuments path to the initial path of dialog
            //----------------------------------------
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog1.FileName = "";
            }
            //----------------------------------------
            // Display the selected path
            //----------------------------------------
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_SettingFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);
            }
        }

        //================================================================================
        // It is the process when the ExportSettingFile button is clicked
        // Save the settings to ini file
        //================================================================================
        private void button_ExportSettingFile_Click(object sender, EventArgs e)
        {
            string  filepath;          // File path
            short   arg1;              // Reserve
            long    ret;               // For return code

            //----------------------------------------
            // Get setting file path from text box
            //----------------------------------------
            filepath = textBox_SettingFile.Text;
            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Save the settings to ini file
            //----------------------------------------
            try
            {
                ret = c_fgen.ExportSettingFile(filepath, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("ExportSettingFile = {0} ", ret);
        }

        //================================================================================
        // It is the process when the ImportSettingFile button is clicked
        // Load the settings from ini file.
        //================================================================================
        private void button_ImportSettingFile_Click(object sender, EventArgs e)
        {
            string  filepath;            // File path
            short   arg1;                // Reserve
            long    ret;                 // For return code

            //----------------------------------------
            // Get setting file path from text box
            //----------------------------------------
            filepath = textBox_SettingFile.Text;
            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Load the settings from ini file.
            //----------------------------------------
            try
            {
                ret = c_fgen.ImportSettingFile(filepath, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("ImportSettingFile = {0} ", ret);
        }

        //================================================================================
        // It is the process when the SetWaveType button is clicked
        // Set the waveform type
        //================================================================================
        private void button_SetWaveType_Click(object sender, EventArgs e)
        {
            short   wavetype;         // Waveform type
            short   aochannel;        // Channel
            short   arg1;             // Reserve
            long    ret;              // For return code

            //----------------------------------------
            // Get the wave type from combobox
            //----------------------------------------
            wavetype = (short)comboBox_WaveType.SelectedIndex;
            //----------------------------------------
            // Get the channel from combobox
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Set the wave type
            //----------------------------------------
            try
            {
                ret = c_fgen.SetWaveType(aochannel, wavetype, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SetWaveType = {0} ", ret);
        }

        //================================================================================
        // It is the process when the ... button is clicked (Data file)
        // Displays the dialog to select the csv file
        //================================================================================
        private void button_ArSelectFile_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Don't check if filename exists
            //----------------------------------------
            openFileDialog1.CheckFileExists = false;
            //----------------------------------------
            // If the path exists, set the current path to the initial path of dialog
            //----------------------------------------
            if (textBox_ArbitraryDataFile.Text.Length > 0)
            {
                openFileDialog1.InitialDirectory = textBox_ArbitraryDataFile.Text;
                openFileDialog1.FileName = openFileDialog1.SafeFileName;
            }
            //----------------------------------------
            // If the path not exists, set the MyDocuments path to the initial path of dialog
            //----------------------------------------
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog1.FileName = "";
            }
            //----------------------------------------
            // Display the selected path
            //----------------------------------------
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_ArbitraryDataFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);
            }
        }

        //================================================================================
        // It is the process when the SetArbitraryDataFile button is clicked
        // Set the data file (csv file) path
        //================================================================================
        private void button_SetArbitraryDataFile_Click(object sender, EventArgs e)
        {
            string  filepath;          // File path
            short   aochannel;         // Channel
            short   arg1;              // Reserve
            long    ret;               // For return code

            //----------------------------------------
            // Get data file path from text box
            //----------------------------------------
            filepath = textBox_ArbitraryDataFile.Text;
            //----------------------------------------
            // Get the channel from combobox
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Set the data file path
            //----------------------------------------
            try
            {
                ret = c_fgen.SetArbitraryDataFile(aochannel, filepath, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SetArbitraryDataFile = {0} ", ret);
        }

        //================================================================================
        // It is the process when the GetArbitraryDataFile button is clicked
        // Get the setting data file (csv file) path
        //================================================================================
        private void button_GetArbitraryDataFile_Click(object sender, EventArgs e)
        {
            string  filepath;            // File path
            short   aochannel;           // Channel
            short   arg1;                // Reserve
            long    ret;                 // For return code

            arg1 = 0;
            filepath = null;
            //----------------------------------------
            // Get the channel from combobox
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // Get the data file path
            //----------------------------------------
            try
            {
                ret = c_fgen.GetArbitraryDataFile(aochannel, ref filepath, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GetArbitraryDataFile = {0} ", ret);
            //----------------------------------------
            // Display the data file path
            //----------------------------------------
            textBox_ArbitraryDataFile.Text = filepath;
        }

        //================================================================================
        // It is the process when the SaveDataFile button is clicked
        // Save the specified channel data to a csv file
        //================================================================================
        private void button_SaveDataFile_Click(object sender, EventArgs e)
        {
            string  filepath;          // File path
            short   aochannel;         // Channel
            short   param;             // Single or Multiple
            long    ret;               // For return code

            //----------------------------------------
            // Get the data file name from text box
            //----------------------------------------
            filepath = textBox_ArbitraryDataFile.Text;
            //----------------------------------------
            // Get the channel parameters (single or multiple) from combo box
            //----------------------------------------
            param = (short)comboBox_Param.SelectedIndex;
            //----------------------------------------
            // Get the channel from combobox
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // Data file settings
            //----------------------------------------
            //-------------------
            // For the single channel, set the third argument to the channel
            //-------------------
            if (param == 0)
            {
                try
                {
                    ret = c_fgen.SaveDataFile(filepath, param, aochannel);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //-------------------
            // For the multiple channels, set the third argument to 0
            //-------------------
            else
            {
                try
                {
                    ret = c_fgen.SaveDataFile(filepath, param, 0);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SaveDataFile = {0} ", ret);
        }

        //================================================================================
        // It is the process when the StartOperation button is clicked
        // Start the output of the application
        //================================================================================
        private void button_StartOperation_Click(object sender, EventArgs e)
        {
            short   arg1;             // Reserve
            long    ret;              // For return code

            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Start the output of the application
            //----------------------------------------
            try
            {
                ret = c_fgen.StartOperation(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("StartOperation = {0} ", ret);
        }

        //================================================================================
        // It is the process when the GetApplicationStatus button is clicked
        // Get the operating status
        //================================================================================
        private void button_GetApplicationStatus_Click(object sender, EventArgs e)
        {
            int     status = -1;       // Startus
            long    ret;               // For return code

            //----------------------------------------
            //  Get the operating status
            //----------------------------------------
            try
            {
                ret = c_fgen.GetApplicationStatus(ref status);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GetApplicationStatus = {0} ", ret);
            //----------------------------------------
            // Display the status
            //----------------------------------------
            if (status == 0)
            {
                textBox_Status.Text = "00H(Stopped)";
            }
            else if (status == 1)
            {
                textBox_Status.Text = "01H(Running)";
            }
            else if (status == 2)
            {
                textBox_Status.Text = "02H(Waiting for start trigger)";
            }
            else if (status == 3)
            {
                textBox_Status.Text = "03H(Running, Waiting for start trigger)";
            }
            else
            {
                textBox_Status.Text = string.Format("{0} ", status);
            }
        }

        //================================================================================
        // It is the process when the StopOperation button is clicked
        // Stop the output of the application
        //================================================================================
        private void button_StopOperation_Click(object sender, EventArgs e)
        {
            short   arg1;             // Reserve
            long    ret;              // For return code

            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Stop the output of the application
            //----------------------------------------
            try
            {
                ret = c_fgen.StopOperation(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("StopOperation = {0} ", ret);
        }

        //================================================================================
        // It is the process when the CloseApplication button is clicked
        // Close the application
        //================================================================================
        private void button_CloseApplicaion_Click(object sender, EventArgs e)
        {
            short   arg1;             // Reserve
            long    ret;              // For return code

            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Perform application close process
            //----------------------------------------
            try
            {
                ret = c_fgen.CloseApplication(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Display the error code
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("CloseApplication = {0} ", ret);
        }

        //================================================================================
        // It is the process when the Exit button is clicked
        // Terminate the application
        //================================================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            short arg1;             // Reserve

            //----------------------------------------
            // arg1 (Reserve)
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Perform application termination process
            //----------------------------------------
            try
            {
                c_fgen.CloseApplication(arg1);
            }
            catch
            {
            }
            //----------------------------------------
            // Close dialog
            //----------------------------------------
            this.Close();
        }
    }
}
