using CWaveformGenerator;

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

        //----------------------------------------
        // }N`
        //----------------------------------------
        public const int AO_CHANNEL = 0;                        // AiOo̓`lԍ : 0
        public const int CWAVEFORMGENERATOR_CHANNELS = 16;      // ő`l
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // C-WaveFormGeneratorSDKŌĂяo߂̏
            //----------------------------------------
            try
            {
                //----------------------------------------
                // C-WaveformGenerator̃NX쐬
                //----------------------------------------
                c_fgen = new CCWaveformGeneratorClass();
            }
            //----------------------------------------
            // G[̎AG[bZ[W\
            //----------------------------------------
            catch
            {
                MessageBox.Show("Unanticipated exception creating the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                this.Close();
                return;
            }
            //----------------------------------------
            // foCX̏\XV
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // ʃTCYR{{bNX̍쐬
            //----------------------------------------
            comboBox_OpenMode.Items.Add("ŏ");
            comboBox_OpenMode.Items.Add("ʏ");
            comboBox_OpenMode.Items.Add("ő剻");
            //----------------------------------------
            // ʃTCYpR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_OpenMode.SelectedIndex = 0;
            //----------------------------------------
            // `lԍR{{bNX̍쐬
            //----------------------------------------
            for (int i = 0; i < CWAVEFORMGENERATOR_CHANNELS; i++)
            {
                comboBox_AoChannel.Items.Add(i.ToString());
            }
            //----------------------------------------
            // `lԍR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_AoChannel.SelectedIndex = 0;
            //----------------------------------------
            // g`^CvR{{bNX̍쐬
            //----------------------------------------
            comboBox_WaveType.Items.Add("");
            comboBox_WaveType.Items.Add("Œ");
            comboBox_WaveType.Items.Add("Op");
            comboBox_WaveType.Items.Add("̂");
            comboBox_WaveType.Items.Add("`");
            comboBox_WaveType.Items.Add("mCY");
            comboBox_WaveType.Items.Add("C");
            //----------------------------------------
            // g`^CvR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_WaveType.SelectedIndex = 0;
            //----------------------------------------
            // f[^ۑR{{bNX̍쐬
            //----------------------------------------
            comboBox_Param.Items.Add("P`l");
            comboBox_Param.Items.Add("`l");
            //----------------------------------------
            // f[^ۑR{{bNX̑I0ԂɕύX
            //----------------------------------------
            comboBox_Param.SelectedIndex = 0;
        }

        //================================================================================
        // OpenApplication{^NbNۂ̏ɂȂ܂
        // Av̏s܂
        //================================================================================
        private void button_OpenApplication_Click(object sender, EventArgs e)
        {
            string  device_name;         // foCX
            string  arg1;                // \
            short   openmode;            // ʃTCY
            long    ret;                 // ߂l擾p

            //----------------------------------------
            // eLXg{bNXfoCX擾
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // R{{bNXʃTCY擾
            //----------------------------------------
            openmode = (short)comboBox_OpenMode.SelectedIndex;
            //----------------------------------------
            // Arg1i\j
            //----------------------------------------
            arg1 = "";
            //----------------------------------------
            // Av̏s
            //----------------------------------------
            try
            {
                ret = c_fgen.OpenApplication(openmode, device_name, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("OpenApplication = {0} ", ret);
        }

        //================================================================================
        // SetDeviceName{^NbNۂ̏ɂȂ܂
        // foCX̐ݒs܂
        //================================================================================
        private void button_SetDeviceName_Click(object sender, EventArgs e)
        {
            string  device_name;         // foCX
            long    ret;                 // ߂l擾p

            //----------------------------------------
            // eLXg{bNXݒt@C擾
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // foCX̐ݒ
            //----------------------------------------
            try
            {
                ret = c_fgen.SetDeviceName(device_name);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SetDeviceName = {0} ", ret);
        }

        //================================================================================
        // ...{^NbNۂ̏ɂȂ܂  (ݒt@C)
        // init@CI_CAO\܂
        //================================================================================
        private void button_SettingSelectFile_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // t@C݂邩ǂ`FbNȂ
            //----------------------------------------
            openFileDialog1.CheckFileExists = false;
            //----------------------------------------
            // t@C̃pX݂ꍇ́At@C̃pX_CAȌpXɐݒ肵܂
            //----------------------------------------
            if (textBox_SettingFile.Text.Length > 0)
            {
                openFileDialog1.InitialDirectory = textBox_SettingFile.Text;
                openFileDialog1.FileName = openFileDialog1.SafeFileName;
            }
            //----------------------------------------
            // t@C̃pX݂Ȃꍇ́AMyDocuments_CAȌpXɐݒ肵܂
            //----------------------------------------
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog1.FileName = "";
            }
            //----------------------------------------
            // IpX\
            //----------------------------------------
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_SettingFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);
            }
        }

        //================================================================================
        // ExportSettingFile{^NbNۂ̏ɂȂ܂
        // ݒinit@Cɕۑ܂
        //================================================================================
        private void button_ExportSettingFile_Click(object sender, EventArgs e)
        {
            string  filepath;          // t@CpX
            short   arg1;              // \
            long    ret;               // ߂l擾p

            //----------------------------------------
            // eLXg{bNXݒt@CpX擾
            //----------------------------------------
            filepath = textBox_SettingFile.Text;
            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // ݒinit@Cɕۑ
            //----------------------------------------
            try
            {
                ret = c_fgen.ExportSettingFile(filepath, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("ExportSettingFile = {0} ", ret);
        }

        //================================================================================
        // ImportSettingFile{^NbNۂ̏ɂȂ܂
        // init@C̐ݒǂݍ݂܂B
        //================================================================================
        private void button_ImportSettingFile_Click(object sender, EventArgs e)
        {
            string  filepath;            // t@CpX
            short   arg1;                // \
            long    ret;                 // ߂l擾p

            //----------------------------------------
            // eLXg{bNXݒt@CpX擾
            //----------------------------------------
            filepath = textBox_SettingFile.Text;
            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // init@C̐ݒǂݏos
            //----------------------------------------
            try
            {
                ret = c_fgen.ImportSettingFile(filepath, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("ImportSettingFile = {0} ", ret);

        }

        //================================================================================
        // SetWaveType{^NbNۂ̏ɂȂ܂
        // g`^Cv̐ݒs܂
        //================================================================================
        private void button_SetWaveType_Click(object sender, EventArgs e)
        {
            short   wavetype;         // g`^Cv
            short   aochannel;        // `lԍ
            short   arg1;             // \
            long    ret;              // ߂l擾p

            //----------------------------------------
            // R{{bNXg`^Cv擾
            //----------------------------------------
            wavetype = (short)comboBox_WaveType.SelectedIndex;
            //----------------------------------------
            // R{{bNX`lԍ擾
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // g`^Cv̐ݒs
            //----------------------------------------
            try
            {
                ret = c_fgen.SetWaveType(aochannel, wavetype, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SetWaveType = {0} ", ret);
        }

        //================================================================================
        // ...{^NbNۂ̏ɂȂ܂ (f[^t@C)
        // csvt@CI_CAO\܂
        //================================================================================
        private void button_ArSelectFile_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // t@C݂邩ǂ`FbNȂ
            //----------------------------------------
            openFileDialog1.CheckFileExists = false;
            //----------------------------------------
            // pX݂ꍇ́Ã݂pX_CAȌpXɐݒ肵܂
            //----------------------------------------
            if (textBox_ArbitraryDataFile.Text.Length > 0)
            {
                openFileDialog1.InitialDirectory = textBox_ArbitraryDataFile.Text;
                openFileDialog1.FileName = openFileDialog1.SafeFileName;
            }
            //----------------------------------------
            // pX݂Ȃꍇ́AMyDocuments_CAȌpXɐݒ肵܂
            //----------------------------------------
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog1.FileName = "";
            }
            //----------------------------------------
            // IpX\
            //----------------------------------------
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_ArbitraryDataFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);
            }
        }

        //================================================================================
        // SetArbitraryDataFile{^NbNۂ̏ɂȂ܂
        // f[^t@Cicsvt@CjpXݒ肵܂
        //================================================================================
        private void button_SetArbitraryDataFile_Click(object sender, EventArgs e)
        {
            string  filepath;          // t@CpX
            short   aochannel;         // `lԍ
            short   arg1;              // \
            long    ret;               // ߂l擾p

            //----------------------------------------
            // eLXg{bNXf[^t@CpX擾
            //----------------------------------------
            filepath = textBox_ArbitraryDataFile.Text;
            //----------------------------------------
            // R{{bNX`lԍ擾
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // f[^t@CpX̐ݒ
            //----------------------------------------
            try
            {
                ret = c_fgen.SetArbitraryDataFile(aochannel, filepath, arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SetArbitraryDataFile = {0} ", ret);
        }

        //================================================================================
        // GetArbitraryDataFile{^NbNۂ̏ɂȂ܂
        // ݒ肳Ăf[^t@Cicsvt@CjpX擾܂
        //================================================================================
        private void button_GetArbitraryDataFile_Click(object sender, EventArgs e)
        {
            string  filepath;            // t@CpX
            short   aochannel;           // `lԍ
            short   arg1;                // \
            long    ret;                 // ߂l擾p

            arg1 = 0;
            filepath = "";
            //----------------------------------------
            // ݒ肳Ă`l擾
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // f[^t@CpX̎擾
            //----------------------------------------
            try
            {
                ret = c_fgen.GetArbitraryDataFile(aochannel, ref filepath, ref arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GetArbitraryDataFile = {0} ", ret);
            //----------------------------------------
            // f[^t@CpX\
            //----------------------------------------
            textBox_ArbitraryDataFile.Text = filepath;
        }

        //================================================================================
        // SaveDataFile{^NbNۂ̏ɂȂ܂
        // w肵`l̃f[^csvt@Cɕۑ܂
        //================================================================================
        private void button_SaveDataFile_Click(object sender, EventArgs e)
        {
            string  filepath;          // t@CpX
            short   aochannel;         // `lԍ
            short   param;             // Por
            long    ret;               // ߂l擾p

            //----------------------------------------
            // eLXg{bNXf[^t@C擾
            //----------------------------------------
            filepath = textBox_ArbitraryDataFile.Text;
            //----------------------------------------
            // R{{bNX`l̃p[^iPorj擾
            //----------------------------------------
            param = (short)comboBox_Param.SelectedIndex;
            //----------------------------------------
            // R{{bNX`lԍ擾
            //----------------------------------------
            aochannel = (short)comboBox_AoChannel.SelectedIndex;
            //----------------------------------------
            // f[^t@C̐ݒ
            //----------------------------------------
            //-------------------
            // P`l̏ꍇA3`lԍɂ
            //-------------------
            if (param == 0)
            {
                try
                {
                    ret = c_fgen.SaveDataFile(filepath, param, aochannel);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                    return;
                }
            }
            //-------------------
            // `l̏ꍇA30ɂ
            //-------------------
            else
            {
                try
                {
                    ret = c_fgen.SaveDataFile(filepath, param, 0);
                }
                catch
                {
                    MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("SaveDataFile = {0} ", ret);
        }

        //================================================================================
        // StartOperation{^NbNۂ̏ɂȂ܂
        // Av̏o͊Jns܂
        //================================================================================
        private void button_StartOperation_Click(object sender, EventArgs e)
        {
            short   arg1;             // \
            long    ret;              // ߂l擾p

            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Av̏o͊Jns
            //----------------------------------------
            try
            {
                ret = c_fgen.StartOperation(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("StartOperation = {0} ", ret);
        }

        //================================================================================
        // GetApplicationStatus{^NbNۂ̏ɂȂ܂
        // 󋵂̎擾s܂
        //================================================================================
        private void button_GetApplicationStatus_Click(object sender, EventArgs e)
        {
            int     status = -1;       // Xe[^X
            long    ret;               // ߂l擾p

            //----------------------------------------
            //  󋵂̎擾s
            //----------------------------------------
            try
            {
                ret = c_fgen.GetApplicationStatus(ref status);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("GetApplicationStatus = {0} ", ret);
            //----------------------------------------
            // Xe[^X\
            //----------------------------------------
            if (status == 0)
            {
                textBox_Status.Text = "00H(~)";
            }
            else if (status == 1)
            {
                textBox_Status.Text = "01H(쒆)";
            }
            else if (status == 2)
            {
                textBox_Status.Text = "02H(JngK҂)";
            }
            else if (status == 3)
            {
                textBox_Status.Text = "03H(쒆AJngK҂)";
            }
            else
            {
                textBox_Status.Text = string.Format("{0} ", status);
            }
        }

        //================================================================================
        // StopOperation{^NbNۂ̏ɂȂ܂
        // Av̏o͒~s܂
        //================================================================================
        private void button_StopOperation_Click(object sender, EventArgs e)
        {
            short   arg1;             // \
            long    ret;              // ߂l擾p

            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // Av̏o͒~s
            //----------------------------------------
            try
            {
                ret = c_fgen.StopOperation(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("StopOperation = {0} ", ret);
        }

        //================================================================================
        // CloseApplication{^NbNۂ̏ɂȂ܂
        // foCX̏Is܂
        //================================================================================
        private void button_CloseApplicaion_Click(object sender, EventArgs e)
        {
            short   arg1;             // \
            long    ret;              // ߂l擾p

            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // foCX̏Is
            //----------------------------------------
            try
            {
                ret = c_fgen.CloseApplication(arg1);
            }
            catch
            {
                MessageBox.Show("Unanticipated exception cannot found the CCWaveformGeneratorClass.", "G[", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // G[R[h\
            //----------------------------------------
            textBox_ReturnNum.Text = string.Format("CloseApplication = {0} ", ret);
        }

        //================================================================================
        // {^NbNۂ̏ɂȂ܂
        // AvP[V̏Is܂
        //================================================================================
        private void button_Exit_Click(object sender, EventArgs e)
        {
            short arg1;             // \

            //----------------------------------------
            // arg1i\j
            //----------------------------------------
            arg1 = 0;
            //----------------------------------------
            // foCX̏Is
            //----------------------------------------
            try
            {
                c_fgen.CloseApplication(arg1);
            }
            catch
            {
            }
            //----------------------------------------
            // _CAOI
            //----------------------------------------
            this.Close();
        }
    }
}
