﻿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 System.Globalization;
using CdioCs;

namespace Infinite
{
    public partial class Condition : 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("Software Start",  (short)CdioConst.DIODM_START_SOFT),
            new StrToNum("External UP",     (short)CdioConst.DIODM_START_EXT_RISE),
            new StrToNum("External Down",   (short)CdioConst.DIODM_START_EXT_FALL),
            new StrToNum("Pattern",         (short)CdioConst.DIODM_START_PATTERN),
            new StrToNum("End",             0)
        };
        // Clock Condition
        static StrToNum[] ClockTrgTable =
        {
            new StrToNum("Internal Clock",  (short)CdioConst.DIODM_CLK_CLOCK),
            new StrToNum("External Clock",  (short)CdioConst.DIODM_CLK_EXT_TRG),
            new StrToNum("Hand Shake",      (short)CdioConst.DIODM_CLK_HANDSHAKE),
            new StrToNum("End",             0)
        };
        // Stop Condition
        static StrToNum[] StopTrgTable =
        {
            new StrToNum("Software Stop",   (short)CdioConst.DIODM_STOP_SOFT),
            new StrToNum("External UP",     (short)CdioConst.DIODM_STOP_EXT_RISE),
            new StrToNum("External Down",   (short)CdioConst.DIODM_STOP_EXT_FALL),
            new StrToNum("Number",          (short)CdioConst.DIODM_STOP_NUM),
            new StrToNum("End",             0)
        };

        Cdio dio = new Cdio();
        static string szStart = "Software Start";
        static string szClock = "Internal Clock";
        static string szStop = "Software Stop";
        static uint Pattern = 0x55;
        static uint Mask = 0xFF;
        static uint InternalClock = 100000;
        static uint StopNumber = 1000;
        public static uint NumberOfData = 500;

        public Condition()
        {
            InitializeComponent();
        }

        //===================================================
        // Dialog init
        //===================================================
        private void Condition_Load(object sender, System.EventArgs e)
        {
            //--------------------------------
            // Init 
            //--------------------------------
            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:X}", Pattern);
            txtCmpPtn.Text = strTmp;
            strTmp = string.Format("{0:X}", Mask);
            txtPtnMask.Text = strTmp;
            strTmp = string.Format("{0:d}", InternalClock);
            txtInternalClock.Text = strTmp;
            strTmp = string.Format("{0:d}", StopNumber);
            txtStopNum.Text = strTmp;
            strTmp = string.Format("{0:d}", NumberOfData);
            txtNumEvent.Text = strTmp;

            cboStartTrg_SelectedIndexChanged(cboStartTrg, new System.EventArgs());
            cboClockTrg_SelectedIndexChanged(cboClockTrg, new System.EventArgs());
            cboStopTrg_SelectedIndexChanged(cboStopTrg, new System.EventArgs());
        }

        //===================================================
        // Modify start condition 
        //===================================================
        private void cboStartTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuff = new string(' ', 100);
            //--------------------------------
            // Enable/disable Pattern and Mask
            //--------------------------------
            szBuff = cboStartTrg.Text;
            if (string.Compare(szBuff, "Pattern") == 0)
            {
                txtCmpPtn.Enabled = true;
                txtPtnMask.Enabled = true;
            }
            else
            {
                txtCmpPtn.Enabled = false;
                txtPtnMask.Enabled = false;
            }
        }

        //===================================================
        // Modify clock condition 
        //===================================================
        private void cboClockTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuff = new string(' ', 100);
            //--------------------------------
            // Enable/disable Internal Clock
            //--------------------------------
            szBuff = cboClockTrg.Text;
            if (string.Compare(szBuff, "Internal Clock") == 0)
            {
                txtInternalClock.Enabled = true;
            }
            else
            {
                txtInternalClock.Enabled = false;
            }
        }

        //===================================================
        // Modify stop condition 
        //===================================================
        private void cboStopTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuff = new string(' ', 100);
            //--------------------------------
            // Enable/disable Stop Number
            //--------------------------------
            szBuff = cboStopTrg.Text;
            if (string.Compare(szBuff, "Number") == 0)
            {
                txtStopNum.Enabled = true;
            }
            else
            {
                txtStopNum.Enabled = false;
            }
        }

        //===================================================
        // OK button handle
        //===================================================
        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 i;
            short Direction = (short)CdioConst.DIODM_DIR_IN;
            int ret;
            string strMsg;

            //--------------------------------
            // Get the displayed data
            //--------------------------------
            szStart = cboStartTrg.Text;
            szClock = cboClockTrg.Text;
            szStop = cboStopTrg.Text;
            szTmp = txtCmpPtn.Text;
            try
            {
                Pattern = uint.Parse(szTmp, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                Pattern = 0x55;
            }
            szTmp = txtPtnMask.Text;
            try
            {
                Mask = uint.Parse(szTmp, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                Mask = 0xFF;
            }
            szTmp = txtInternalClock.Text;
            try
            {
                InternalClock = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                InternalClock = 100000;
            }
            szTmp = txtStopNum.Text;
            try
            {
                StopNumber = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                StopNumber = 1000;
            }
            szTmp = txtNumEvent.Text;
            try
            {
                NumberOfData = uint.Parse(szTmp);
            }
            catch (System.FormatException)
            {
                NumberOfData = 500;
            }
            //--------------------------------
            // Convert data to number 
            //--------------------------------
            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;
                }
            }
            //--------------------------------
            // Set sampling condition
            //--------------------------------
            // Specify input/output direction (32-point input)
            ret = dio.DmSetDirection(Form1.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;
            }
            // Start alone
            ret = dio.DmSetStandAlone(Form1.Id);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetStandAlone ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // Set start condition
            ret = dio.DmSetStartTrigger(Form1.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;
            }
            // Set clock condition
            ret = dio.DmSetClockTrigger(Form1.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;
            }
            // Set stop condition
            ret = dio.DmSetStopTrigger(Form1.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;
            }
            // pattern/mask
            ret = dio.DmSetStartPattern(Form1.Id, Pattern, Mask);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetStartPattern ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // Set internal clock
            ret = dio.DmSetInternalClock(Form1.Id, Direction, InternalClock, (short)CdioConst.DIODM_TIM_UNIT_NS);
            if (ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                dio.GetErrorString(ret, out error_string);
                strMsg = string.Format("DioDmSetInternalClock ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
                return;
            }
            // Set stop number
            ret = dio.DmSetStopNumber(Form1.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();
        }

        //===================================================
        // Cancel button handle
        //===================================================
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}
