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

namespace Sampling
{
    public partial class ConditionDlg : Form
    {
        public 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
        StrToNum[] StartTrgTable =
        {
            new StrToNum("Software start",                          (short)CcntConst.CNTS_START_SOFT                ),
            new StrToNum("External signal rising",                  (short)CcntConst.CNTS_START_EXT_RISE            ),
            new StrToNum("External signal falling",                 (short)CcntConst.CNTS_START_EXT_FALL            ),
            new StrToNum("Count-match CH0 Compare-register 0",      (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG0   ),
            new StrToNum("Count-match CH1 Compare-register 0",      (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG0   ),
            new StrToNum("Count-match CH2 Compare-register 0",      (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG0   ),
            new StrToNum("Count-match CH3 Compare-register 0",      (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG0   ),
            new StrToNum("Count-match CH0 Compare-register 1",      (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG1   ),
            new StrToNum("Count-match CH1 Compare-register 1",      (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG1   ),
            new StrToNum("Count-match CH2 Compare-register 1",      (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG1   ),
            new StrToNum("Count-match CH3 Compare-register 1",      (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG1   ),
            new StrToNum("End",                                     0                                               )
        };
        // Clock Condition
        StrToNum[] ClockTrgTable =
        {
            new StrToNum("Internal clock",                      (short)CcntConst.CNTS_CLOCK_CLOCK               ),
            new StrToNum("External clock",                      (short)CcntConst.CNTS_CLOCK_EXT_TRG             ),
            new StrToNum("End",                                 0                                               )
        };
        // Stop Condition
        StrToNum[] StopTrgTable =
        {
            new StrToNum("Software stop",                       (short)CcntConst.CNTS_STOP_SOFT                 ),
            new StrToNum("External signal rising",              (short)CcntConst.CNTS_STOP_EXT_RISE             ),
            new StrToNum("External signal falling",             (short)CcntConst.CNTS_STOP_EXT_FALL             ),
            new StrToNum("Stop transfer by specified times",    (short)CcntConst.CNTS_STOP_NUM                  ),
            new StrToNum("Count-match CH0 Compare-register 0",  (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG0    ),
            new StrToNum("Count-match CH1 Compare-register 0",  (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG0    ),
            new StrToNum("Count-match CH2 Compare-register 0",  (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG0    ),
            new StrToNum("Count-match CH3 Compare-register 0",  (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG0    ),
            new StrToNum("Count-match CH0 Compare-register 1",  (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG1    ),
            new StrToNum("Count-match CH1 Compare-register 1",  (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG1    ),
            new StrToNum("Count-match CH2 Compare-register 1",  (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG1    ),
            new StrToNum("Count-match CH3 Compare-register 1",  (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG1    ),
            new StrToNum("End",                                 0                                               )
        };

        static uint StartComp = 0x100;
        static uint StopComp = 0x200;

        static string szStart = "Software start";
        static string szClock = "Internal clock";
        static string szStop = "Software stop";
        static uint InternalClock = 100000;
        static uint StopNumber = 1000;
        public static uint NumberOfData = 500;
        static short StartCountMode = (short)CcntConst.CNTS_START_CNT_WITH_SAMP;
        static short StopCountMode = (short)CcntConst.CNTS_STOP_CNT_WITH_SAMP;

        Ccnt cnt = new Ccnt();

        public ConditionDlg()
        {
            InitializeComponent();
        }

        //===================================================
        // Dialog initialization
        //===================================================
        private void ConditionDlg_Load(object sender, System.EventArgs e)
        {
            //--------------------------------
            // Initialize display
            //--------------------------------
            uint i;
            string szBuf = new string(' ', 256);

            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;

            if (StartCountMode == (short)CcntConst.CNTS_START_CNT_NOT_WITH_SAMP)
            {
                rdoStartCntNotWithSamp.Checked = true;
                rdoStartCntWithSamp.Checked = false;
            }
            else
            {
                rdoStartCntNotWithSamp.Checked = false;
                rdoStartCntWithSamp.Checked = true;
            }
            if (StopCountMode == (short)CcntConst.CNTS_STOP_CNT_NOT_WITH_SAMP)
            {
                rdoStopCntNotWithSamp.Checked = true;
                rdoStopCntWithSamp.Checked = false;
            }
            else
            {
                rdoStopCntNotWithSamp.Checked = false;
                rdoStopCntWithSamp.Checked = true;
            }

            szBuf = string.Format("{0:X}", StartComp);
            txtCompStart.Text = szBuf;
            txtInternalClock.Text = InternalClock.ToString();
            txtStopNum.Text = StopNumber.ToString();
            txtNumEvent.Text = NumberOfData.ToString();
            szBuf = string.Format("{0:X}", StopComp);
            txtCompStop.Text = szBuf;

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

        //===================================================
        // Start condition setting is changed
        //===================================================
        private void cboStartTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);

            szBuf = cboStartTrg.Text;
            if (szBuf.StartsWith("Count-match"))
            {
                txtCompStart.Enabled = true;
            }
            else
            {
                txtCompStart.Enabled = false;
            }
        }

        //===================================================
        // Clock condition setting is changed
        //===================================================
        private void cboClockTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);

            //--------------------------------
            // Enable/Disable internal clock value
            //--------------------------------
            szBuf = cboClockTrg.Text;

            if (string.Compare(szBuf, "Internal clock") == 0)
            {
                txtInternalClock.Enabled = true;
            }
            else
            {
                txtInternalClock.Enabled = false;
            }
        }

        //===================================================
        // Stop condition setting is changed
        //===================================================
        private void cboStopTrg_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);

            szBuf = cboStopTrg.Text;
            if (szBuf.StartsWith("Count-match"))
            {
                txtCompStop.Enabled = true;
            }
            else
            {
                txtCompStop.Enabled = false;
            }
            if (string.Compare(szBuf, "Stop transfer by specified times") == 0)
            {
                txtStopNum.Enabled = true;
            }
            else
            {
                txtStopNum.Enabled = false;
            }
        }

        //===================================================
        // OK button processing
        //===================================================
        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;
            int ret;
            string strMsg;
            short CompChNo;
            short CompRegNo;

            //--------------------------------
            // Get displayed data
            //--------------------------------
            szStart = cboStartTrg.Text;
            szClock = cboClockTrg.Text;
            szStop = cboStopTrg.Text;

            if (rdoStartCntNotWithSamp.Checked == true)
            {
                StartCountMode = (short)CcntConst.CNTS_START_CNT_NOT_WITH_SAMP;
            }
            else
            {
                StartCountMode = (short)CcntConst.CNTS_START_CNT_WITH_SAMP;
            }
            if (rdoStopCntNotWithSamp.Checked == true)
            {
                StopCountMode = (short)CcntConst.CNTS_STOP_CNT_NOT_WITH_SAMP;
            }
            else
            {
                StopCountMode = (short)CcntConst.CNTS_STOP_CNT_WITH_SAMP;
            }

            szTmp = txtCompStart.Text;
            try
            {
                StartComp = uint.Parse(szTmp, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                StartComp = 0x100;
            }
            szTmp = txtCompStop.Text;
            try
            {
                StopComp = uint.Parse(szTmp, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                StopComp = 0x200;
            }
            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 setting value to a 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
            //--------------------------------
            // Stand-alone
            ret = cnt.SetStandAlone(Form1.Id);
            if (ret != (int)CcntConst.CNT_ERR_SUCCESS)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetStandAlone ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                return;
            }
            // Set start condition
            ret = cnt.SetSamplingStartTrigger(Form1.Id, StartTrg, StartCountMode, Form1.CHANNEL_NUM);
            if (ret != (int)CcntConst.CNT_ERR_SUCCESS)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingStartTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                return;
            }
            switch (StartTrg)
            {
                case (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG0: CompChNo = 0; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG0: CompChNo = 1; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG0: CompChNo = 2; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG0: CompChNo = 3; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH4_REG0: CompChNo = 4; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH5_REG0: CompChNo = 5; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH6_REG0: CompChNo = 6; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH7_REG0: CompChNo = 7; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG1: CompChNo = 0; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG1: CompChNo = 1; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG1: CompChNo = 2; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG1: CompChNo = 3; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH4_REG1: CompChNo = 4; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH5_REG1: CompChNo = 5; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH6_REG1: CompChNo = 6; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH7_REG1: CompChNo = 7; CompRegNo = 1; break;
                default: CompChNo = 0xfff; CompRegNo = 0xfff; break;
            }
            if (CompChNo != 0xfff && CompRegNo != 0xfff)
            {
                ret = cnt.NotifyCountUp(Form1.Id, CompChNo, CompRegNo, StartComp, (IntPtr)0);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    strMsg = string.Format("CntNotifyCountUp ret = {0:d}: {1}", ret, error_string);
                    MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                }
            }
            // Set clock condition
            ret = cnt.SetSamplingClockTrigger(Form1.Id, ClockTrg);
            if (ret != (int)CcntConst.CNT_ERR_SUCCESS)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingClockTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                return;
            }
            // Set internal clock value
            if (ClockTrg == (short)CcntConst.CNTS_CLOCK_CLOCK)
            {
                ret = cnt.SetSamplingInternalClock(Form1.Id, InternalClock, (short)CcntConst.CNTS_TIM_UNIT_NS);
                if (ret != (int)CcntConst.CNT_ERR_SUCCESS)
                {
                    cnt.GetErrorString(ret, out error_string);
                    strMsg = string.Format("CntSetSamplingInternalClock ret = {0:d}: {1}", ret, error_string);
                    MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                    return;
                }
            }

            // Set stop condition
            ret = cnt.SetSamplingStopTrigger(Form1.Id, StopTrg, StopCountMode, Form1.CHANNEL_NUM);
            if (ret != (int)CcntConst.CNT_ERR_SUCCESS)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingStopTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                return;
            }
            switch (StopTrg)
            {
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG0: CompChNo = 0; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG0: CompChNo = 1; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG0: CompChNo = 2; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG0: CompChNo = 3; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH4_REG0: CompChNo = 4; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH5_REG0: CompChNo = 5; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH6_REG0: CompChNo = 6; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH7_REG0: CompChNo = 7; CompRegNo = 0; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG1: CompChNo = 0; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG1: CompChNo = 1; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG1: CompChNo = 2; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG1: CompChNo = 3; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH4_REG1: CompChNo = 4; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH5_REG1: CompChNo = 5; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH6_REG1: CompChNo = 6; CompRegNo = 1; break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH7_REG1: CompChNo = 7; CompRegNo = 1; break;
                default: CompChNo = 0xfff; CompRegNo = 0xfff; break;
            }
            if (CompChNo != 0xfff && CompRegNo != 0xfff)
            {
                ret = cnt.NotifyCountUp(Form1.Id, CompChNo, CompRegNo, StopComp, (IntPtr)0);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    strMsg = string.Format("CntNotifyCountUp ret = {0:d}: {1}", ret, error_string);
                    MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                }
            }
            // Set stop number
            if (StopTrg == (short)CcntConst.CNTS_STOP_NUM)
            {
                ret = cnt.SetSamplingStopNumber(Form1.Id, StopNumber);
                if (ret != (int)CcntConst.CNT_ERR_SUCCESS)
                {
                    cnt.GetErrorString(ret, out error_string);
                    strMsg = string.Format("CntSetSamplingInternalClock ret = {0:d}: {1}", ret, error_string);
                    MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                    return;
                }
            }
            this.Close();
        }

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