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

namespace Sync
{
    public partial class SCondition : Form
    {
        struct StrToNum
        {                                                   // structure of condition and parameters
            public string Str;
            public short  Num;
            public StrToNum(string Str, short Num)
            {
                this.Str = Str;
                this.Num = Num;
            }
        };
        // Start Condition
        static StrToNum[] StartTrgTable =
        {
            new StrToNum("ExtSig1", (short)CdioConst.DIODM_START_EXTSIG_1),
            new StrToNum("ExtSig2", (short)CdioConst.DIODM_START_EXTSIG_2),
            new StrToNum("ExtSig3", (short)CdioConst.DIODM_START_EXTSIG_3),
            new StrToNum("End",     0)
        };
        // Clock Condition
        static StrToNum[] ClockTrgTable =
        {
            new StrToNum("ExtSig1", (short)CdioConst.DIODM_CLK_EXTSIG_1),
            new StrToNum("ExtSig2", (short)CdioConst.DIODM_CLK_EXTSIG_2),
            new StrToNum("ExtSig3", (short)CdioConst.DIODM_CLK_EXTSIG_3),
            new StrToNum("End",     0)
        };
        // Stop Condition
        static StrToNum[] StopTrgTable =
        {
            new StrToNum("ExtSig1", (short)CdioConst.DIODM_STOP_EXTSIG_1),
            new StrToNum("ExtSig2", (short)CdioConst.DIODM_STOP_EXTSIG_2),
            new StrToNum("ExtSig3", (short)CdioConst.DIODM_STOP_EXTSIG_3),
            new StrToNum("Number",  (short)CdioConst.DIODM_STOP_NUM),
            new StrToNum("End",     0)
        };

        Cdio                dio            = new Cdio();
        static string       szStart        = "ExtSig2";
        static string       szClock        = "ExtSig1";
        static string       szStop         = "ExtSig3";
        static uint         StopNumber     = 1000;
        public static uint  S_NumberOfData = 500;
        public static short S_IsRing       = (short)CdioConst.DIODM_WRITE_ONCE;

        public SCondition()
        {
            InitializeComponent();
        }

        //===================================================
        // ダイアログ初期化
        //===================================================
        private void SCondition_Load(object sender, System.EventArgs e)
        {
            //--------------------------------
            // 表示初期化
            //--------------------------------
            uint i;
            string strTmp;

            for (i = 0; string.Compare(StartTrgTable[i].Str, "End") != 0; i++)
            {
                cboStartTrg.Items.Add(StartTrgTable[i].Str);
            }
            for (i = 0; string.Compare(ClockTrgTable[i].Str, "End") != 0; i++)
            {
                cboClockTrg.Items.Add(ClockTrgTable[i].Str);
            }
            for (i = 0; string.Compare(StopTrgTable[i].Str, "End") != 0; i++)
            {
                cboStopTrg.Items.Add(StopTrgTable[i].Str);
            }

            cboStartTrg.Text = szStart;
            cboClockTrg.Text = szClock;
            cboStopTrg.Text  = szStop;

            strTmp = string.Format("{0:d}", StopNumber);
            txtStopNum.Text = strTmp;
            strTmp = string.Format("{0:d}", S_NumberOfData);
            txtNumEvent.Text = strTmp;
            if (S_IsRing == (short)CdioConst.DIODM_WRITE_ONCE)
            {
                rdoOnce.Checked = true;
                rdoRing.Checked = false;
            }
            else
            {
                rdoOnce.Checked = false;
                rdoRing.Checked = true;
            }

            cboStopTrg_SelectedIndexChanged(cboStopTrg, new System.EventArgs());
        }

        //===================================================
        // ストップ条件設定変更
        //===================================================
        private void cboStopTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuff = new string(' ', 100);
            //--------------------------------
            // Stop Numberの有効、無効を切り替え
            //--------------------------------;
            szBuff = cboStopTrg.Text;
            if (string.Compare(szBuff, "Number") == 0)
            {
                txtStopNum.Enabled = true;
            }
            else
            {
                txtStopNum.Enabled = false;
            }
        }

        //===================================================
        // OKボタン処理
        //===================================================
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            string error_string = new string(' ', 256);
            string szTmp = new string(' ', 100);
            short  StartTrg;
            short  ClockTrg;
            short  StopTrg;
            short  ExtSig1, ExtSig2, ExtSig3;
            short  i;
            short  Direction = (short)CdioConst.DIODM_DIR_IN;
            int    ret;
            string strMsg;

            //--------------------------------
            // 表示データを取得
            //--------------------------------
            szStart = cboStartTrg.Text;
            szClock = cboClockTrg.Text;
            szStop  = cboStopTrg.Text;
            szTmp   = txtNumEvent.Text;
            try
            {
                S_NumberOfData = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                S_NumberOfData = 500;
            }
            szTmp = txtStopNum.Text;
            try
            {
                StopNumber = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                StopNumber = 1000;
            }
            if (rdoOnce.Checked == true)
            {
                S_IsRing = (short)CdioConst.DIODM_WRITE_ONCE;
            }
            else
            {
                S_IsRing = (short)CdioConst.DIODM_WRITE_RING;
            }
            //--------------------------------
            // 設定値を数値に変換
            //--------------------------------
            StartTrg = 0;
            for (i = 0; string.Compare(StartTrgTable[i].Str, "End") != 0; i++)
            {
                if (string.Compare(szStart, StartTrgTable[i].Str) == 0)
                {
                    StartTrg = StartTrgTable[i].Num;
                    break;
                }
            }
            ClockTrg = 0;
            for (i = 0; string.Compare(ClockTrgTable[i].Str, "End") != 0; i++)
            {
                if (string.Compare(szClock, ClockTrgTable[i].Str) == 0)
                {
                    ClockTrg = ClockTrgTable[i].Num;
                    break;
                }
            }
            StopTrg = 0;
            for (i = 0; string.Compare(StopTrgTable[i].Str, "End") != 0; i++)
            {
                if (string.Compare(szStop, StopTrgTable[i].Str) == 0)
                {
                    StopTrg = StopTrgTable[i].Num;
                    break;
                }
            }
            //--------------------------------
            // サンプリング条件を設定
            //--------------------------------
            // 入出力方向設定(全点入力)
            ret = dio.DmSetDirection(Form1.S_Id, (short)CdioConst.PI_32);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetDirection ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // スレーブ設定
            ExtSig1 = 1;
            ExtSig2 = 1;
            ExtSig3 = 1;
            ret = dio.DmSetSlave(Form1.S_Id, ExtSig1, ExtSig2, ExtSig3, 1, 1);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetSlave ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // スタート条件設定
            ret = dio.DmSetStartTrigger(Form1.S_Id, Direction, StartTrg);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetStartTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // クロック条件設定
            ret = dio.DmSetClockTrigger(Form1.S_Id, Direction, ClockTrg);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetClockTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // ストップ条件設定
            ret = dio.DmSetStopTrigger(Form1.S_Id, Direction, StopTrg);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetStopTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // 停止個数設定
            ret = dio.DmSetStopNumber(Form1.S_Id, Direction, StopNumber);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetStopNumber ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }

            this.Close();
        }

        //===================================================
        // キャンセルボタン処理
        //===================================================
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}
