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;

        //----------------------------------------
        // マクロ定義
        //----------------------------------------
        public const int AO_CHANNEL = 0;                        // アナログ出力チャネル番号 : 0
        public const int CWAVEFORMGENERATOR_CHANNELS = 16;      // 最大チャネル数
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // C-WaveFormGeneratorをSDKで呼び出すための初期処理
            //----------------------------------------
            try
            {
                //----------------------------------------
                // C-WaveformGeneratorのクラスを作成
                //----------------------------------------
                c_fgen = new CCWaveformGeneratorClass();
            }
            //----------------------------------------
            // エラーの時、エラーメッセージを表示
            //----------------------------------------
            catch
            {
                MessageBox.Show("Unanticipated exception creating the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                this.Close();
                return;
            }
            //----------------------------------------
            // デバイス名の初期表示を更新
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // 画面サイズコンボボックスの作成
            //----------------------------------------
            comboBox_OpenMode.Items.Add("最小化");
            comboBox_OpenMode.Items.Add("通常");
            comboBox_OpenMode.Items.Add("最大化");
            //----------------------------------------
            // 画面サイズ用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_OpenMode.SelectedIndex = 0;
            //----------------------------------------
            // チャネル番号コンボボックスの作成
            //----------------------------------------
            for (int i = 0; i < CWAVEFORMGENERATOR_CHANNELS; i++)
            {
                comboBox_AoChannel.Items.Add(i.ToString());
            }
            //----------------------------------------
            // チャネル番号コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_AoChannel.SelectedIndex = 0;
            //----------------------------------------
            // 波形タイプコンボボックスの作成
            //----------------------------------------
            comboBox_WaveType.Items.Add("正弦");
            comboBox_WaveType.Items.Add("固定");
            comboBox_WaveType.Items.Add("三角");
            comboBox_WaveType.Items.Add("のこぎり");
            comboBox_WaveType.Items.Add("矩形");
            comboBox_WaveType.Items.Add("ノイズ");
            comboBox_WaveType.Items.Add("任意");
            //----------------------------------------
            // 波形タイプコンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_WaveType.SelectedIndex = 0;
            //----------------------------------------
            // データ保存コンボボックスの作成
            //----------------------------------------
            comboBox_Param.Items.Add("単チャネル");
            comboBox_Param.Items.Add("複数チャネル");
            //----------------------------------------
            // データ保存コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_Param.SelectedIndex = 0;
        }

        //================================================================================
        // OpenApplicationボタンをクリックした際の処理になります
        // アプリの初期化処理を行います
        //================================================================================
        private void button_OpenApplication_Click(object sender, EventArgs e)
        {
            string  device_name;         // デバイス名
            string  arg1;                // 予約
            short   openmode;            // 画面サイズ
            long    ret;                 // 戻り値取得用

            //----------------------------------------
            // テキストボックスからデバイス名を取得
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // コンボボックスから画面サイズを取得
            //----------------------------------------
            openmode = (short)comboBox_OpenMode.SelectedIndex;
            //----------------------------------------
            // Arg1（予約）
            //----------------------------------------
            arg1 = "";
            //----------------------------------------
            // アプリの初期化処理を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.OpenApplication(openmode, device_name, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("OpenApplication = {0} ", ret);
        }

        //================================================================================
        // SetDeviceNameボタンをクリックした際の処理になります
        // デバイス名の設定を行います
        //================================================================================
        private void button_SetDeviceName_Click(object sender, EventArgs e)
        {
            string  device_name;         // デバイス名
            long    ret;                 // 戻り値取得用

            //----------------------------------------
            // テキストボックスから設定ファイル名を取得
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // デバイス名の設定
            //----------------------------------------
            try
            {
                ret = c_fgen.SetDeviceName(device_name);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SetDeviceName = {0} ", ret);
        }

        //================================================================================
        // ...ボタンをクリックした際の処理になります  (設定ファイル)
        // iniファイルを選択するダイアログを表示します
        //================================================================================
        private void button_SettingSelectFile_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // ファイル名が存在するかどうかをチェックしない
            //----------------------------------------
            openFileDialog1.CheckFileExists = false;
            //----------------------------------------
            // ファイルのパスが存在する場合は、ファイルのパスをダイアログの初期パスに設定します
            //----------------------------------------
            if (textBox_SettingFile.Text.Length > 0)
            {
                openFileDialog1.InitialDirectory = textBox_SettingFile.Text;
                openFileDialog1.FileName = openFileDialog1.SafeFileName;
            }
            //----------------------------------------
            // ファイルのパスが存在しない場合は、MyDocumentsをダイアログの初期パスに設定します
            //----------------------------------------
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog1.FileName = "";
            }
            //----------------------------------------
            // 選択したパスを表示
            //----------------------------------------
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_SettingFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);
            }
        }

        //================================================================================
        // ExportSettingFileボタンをクリックした際の処理になります
        // 設定をiniファイルに保存します
        //================================================================================
        private void button_ExportSettingFile_Click(object sender, EventArgs e)
        {
            string  filepath;          // ファイルパス
            short   arg1;              // 予約
            long    ret;               // 戻り値取得用

            //----------------------------------------
            // テキストボックスから設定ファイルパスを取得
            //----------------------------------------
            filepath = textBox_SettingFile.Text;
            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // 設定をiniファイルに保存
            //----------------------------------------
            try
            {
                ret = c_fgen.ExportSettingFile(filepath, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("ExportSettingFile = {0} ", ret);
        }

        //================================================================================
        // ImportSettingFileボタンをクリックした際の処理になります
        // iniファイルの設定を読み込みます。
        //================================================================================
        private void button_ImportSettingFile_Click(object sender, EventArgs e)
        {
            string  filepath;            // ファイルパス
            short   arg1;                // 予約
            long    ret;                 // 戻り値取得用

            //----------------------------------------
            // テキストボックスから設定ファイルパスを取得
            //----------------------------------------
            filepath = textBox_SettingFile.Text;
            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // iniファイルの設定読み出しを行う
            //----------------------------------------
            try
            {
                ret = c_fgen.ImportSettingFile(filepath, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("ImportSettingFile = {0} ", ret);

        }

        //================================================================================
        // SetWaveTypeボタンをクリックした際の処理になります
        // 波形タイプの設定を行います
        //================================================================================
        private void button_SetWaveType_Click(object sender, EventArgs e)
        {
            short   wavetype;         // 波形タイプ
            short   aochannel;        // チャネル番号
            short   arg1;             // 予約
            long    ret;              // 戻り値取得用

            //----------------------------------------
            // コンボボックスから波形タイプを取得
            //----------------------------------------
            wavetype = (short)comboBox_WaveType.SelectedIndex;
            //----------------------------------------
            // コンボボックスからチャネル番号を取得
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // 波形タイプの設定を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.SetWaveType(aochannel, wavetype, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SetWaveType = {0} ", ret);
        }

        //================================================================================
        // ...ボタンをクリックした際の処理になります (データファイル)
        // csvファイルを選択するダイアログを表示します
        //================================================================================
        private void button_ArSelectFile_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // ファイル名が存在するかどうかをチェックしない
            //----------------------------------------
            openFileDialog1.CheckFileExists = false;
            //----------------------------------------
            // パスが存在する場合は、現在のパスをダイアログの初期パスに設定します
            //----------------------------------------
            if (textBox_ArbitraryDataFile.Text.Length > 0)
            {
                openFileDialog1.InitialDirectory = textBox_ArbitraryDataFile.Text;
                openFileDialog1.FileName = openFileDialog1.SafeFileName;
            }
            //----------------------------------------
            // パスが存在しない場合は、MyDocumentsをダイアログの初期パスに設定します
            //----------------------------------------
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog1.FileName = "";
            }
            //----------------------------------------
            // 選択したパスを表示
            //----------------------------------------
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_ArbitraryDataFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);
            }
        }

        //================================================================================
        // SetArbitraryDataFileボタンをクリックした際の処理になります
        // データファイル（csvファイル）パスを設定します
        //================================================================================
        private void button_SetArbitraryDataFile_Click(object sender, EventArgs e)
        {
            string  filepath;          // ファイルパス
            short   aochannel;         // チャネル番号
            short   arg1;              // 予約
            long    ret;               // 戻り値取得用

            //----------------------------------------
            // テキストボックスからデータファイルパスを取得
            //----------------------------------------
            filepath = textBox_ArbitraryDataFile.Text;
            //----------------------------------------
            // コンボボックスからチャネル番号を取得
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // データファイルパスの設定
            //----------------------------------------
            try
            {
                ret = c_fgen.SetArbitraryDataFile(aochannel, filepath, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SetArbitraryDataFile = {0} ", ret);
        }

        //================================================================================
        // GetArbitraryDataFileボタンをクリックした際の処理になります
        // 設定されているデータファイル（csvファイル）パスを取得します
        //================================================================================
        private void button_GetArbitraryDataFile_Click(object sender, EventArgs e)
        {
            string  filepath;            // ファイルパス
            short   aochannel;           // チャネル番号
            short   arg1;                // 予約
            long    ret;                 // 戻り値取得用

            arg1 = 0;
            filepath = null;
            //----------------------------------------
            // 設定されているチャネルを取得
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // データファイルパスの取得
            //----------------------------------------
            try
            {
                ret = c_fgen.GetArbitraryDataFile(aochannel, ref filepath, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GetArbitraryDataFile = {0} ", ret);
            //----------------------------------------
            // データファイルパスを表示
            //----------------------------------------
            textBox_ArbitraryDataFile.Text = filepath;
        }

        //================================================================================
        // SaveDataFileボタンをクリックした際の処理になります
        // 指定したチャネルのデータをcsvファイルに保存します
        //================================================================================
        private void button_SaveDataFile_Click(object sender, EventArgs e)
        {
            string  filepath;          // ファイルパス
            short   aochannel;         // チャネル番号
            short   param;             // 単数or複数
            long    ret;               // 戻り値取得用

            //----------------------------------------
            // テキストボックスからデータファイル名を取得
            //----------------------------------------
            filepath = textBox_ArbitraryDataFile.Text;
            //----------------------------------------
            // コンボボックスからチャネルのパラメータ（単数or複数）を取得
            //----------------------------------------
            param = (short)comboBox_Param.SelectedIndex;
            //----------------------------------------
            // コンボボックスからチャネル番号を取得
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // データファイルの設定
            //----------------------------------------
            //-------------------
            // 単数チャネルの場合、第3引数をチャネル番号にする
            //-------------------
            if (param == 0)
            {
                try
                {
                    ret = c_fgen.SaveDataFile(filepath, param, aochannel);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //-------------------
            // 複数チャネルの場合、第3引数を0にする
            //-------------------
            else
            {
                try
                {
                    ret = c_fgen.SaveDataFile(filepath, param, 0);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SaveDataFile = {0} ", ret);
        }

        //================================================================================
        // StartOperationボタンをクリックした際の処理になります
        // アプリの出力開始を行います
        //================================================================================
        private void button_StartOperation_Click(object sender, EventArgs e)
        {
            short   arg1;             // 予約
            long    ret;              // 戻り値取得用

            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // アプリの出力開始を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.StartOperation(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("StartOperation = {0} ", ret);
        }

        //================================================================================
        // GetApplicationStatusボタンをクリックした際の処理になります
        // 動作状況の取得を行います
        //================================================================================
        private void button_GetApplicationStatus_Click(object sender, EventArgs e)
        {
            int     status = -1;       // ステータス
            long    ret;               // 戻り値取得用

            //----------------------------------------
            //  動作状況の取得を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.GetApplicationStatus(ref status);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GetApplicationStatus = {0} ", ret);
            //----------------------------------------
            // ステータスを表示
            //----------------------------------------
            if (status == 0)
            {
                textBox_Status.Text = "00H(停止中)";
            }
            else if (status == 1)
            {
                textBox_Status.Text = "01H(動作中)";
            }
            else if (status == 2)
            {
                textBox_Status.Text = "02H(開始トリガ待ち)";
            }
            else if (status == 3)
            {
                textBox_Status.Text = "03H(動作中、開始トリガ待ち)";
            }
            else
            {
                textBox_Status.Text = string.Format("{0} ", status);
            }
        }

        //================================================================================
        // StopOperationボタンをクリックした際の処理になります
        // アプリの出力停止を行います
        //================================================================================
        private void button_StopOperation_Click(object sender, EventArgs e)
        {
            short   arg1;             // 予約
            long    ret;              // 戻り値取得用

            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // アプリの出力停止を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.StopOperation(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("StopOperation = {0} ", ret);
        }

        //================================================================================
        // CloseApplicationボタンをクリックした際の処理になります
        // デバイスの終了処理を行います
        //================================================================================
        private void button_CloseApplicaion_Click(object sender, EventArgs e)
        {
            short   arg1;             // 予約
            long    ret;              // 戻り値取得用

            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // デバイスの終了処理を行う
            //----------------------------------------
            try
            {
                ret = c_fgen.CloseApplication(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // エラーコードを表示
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("CloseApplication = {0} ", ret);
        }

        //================================================================================
        // 閉じるボタンをクリックした際の処理になります
        // アプリケーションの終了処理を行います
        //================================================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            short arg1;             // 予約

            //----------------------------------------
            // arg1（予約）
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // デバイスの終了処理を行う
            //----------------------------------------
            try
            {
                c_fgen.CloseApplication(arg1);
            }
            catch
            {
            }
            //----------------------------------------
            // ダイアログを終了する
            //----------------------------------------
            this.Close();
        }
    }
}
