﻿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;
// Counter header
using CcntCs;

namespace Sync
{
    public partial class SSampMode : Form
    {
        public struct StrNum
        {
            public string Str;
            public short Num;
            public StrNum(string Str, short Num)
            {
                this.Str = Str;
                this.Num = Num;
            }
        };

        // Sampling Start
        static StrNum[] Start =
        {
            new StrNum("ExtSig1",                               (short)CcntConst.CNTS_START_EXTSIG_1            ),
            new StrNum("ExtSig2",                               (short)CcntConst.CNTS_START_EXTSIG_2            ),
            new StrNum("ExtSig3",                               (short)CcntConst.CNTS_START_EXTSIG_3            ),
            new StrNum("End",                                   0                                               )
        };
        // Counter Start
        static StrNum[] StartWith =
        {
            new StrNum("Start counter regardless of sampling",  (short)CcntConst.CNTS_START_CNT_NOT_WITH_SAMP   ),
            new StrNum("Start counter when sampling starts",    (short)CcntConst.CNTS_START_CNT_WITH_SAMP       ),
            new StrNum("End",                                   0                                               )
        };
        // Sampling Clock
        static StrNum[] Clock =
        {
            new StrNum("ExtSig1",                               (short)CcntConst.CNTS_CLOCK_EXTSIG_1            ),
            new StrNum("ExtSig2",                               (short)CcntConst.CNTS_CLOCK_EXTSIG_2            ),
            new StrNum("ExtSig3",                               (short)CcntConst.CNTS_CLOCK_EXTSIG_3            ),
            new StrNum("End",                                   0                                               )
        };
        // Sampling Stop
        static StrNum[] Stop =
        {
            new StrNum("ExtSig1",                               (short)CcntConst.CNTS_STOP_EXTSIG_1             ),
            new StrNum("ExtSig2",                               (short)CcntConst.CNTS_STOP_EXTSIG_2             ),
            new StrNum("ExtSig3",                               (short)CcntConst.CNTS_STOP_EXTSIG_3             ),
            new StrNum("Stop transfer by specified times",      (short)CcntConst.CNTS_STOP_NUM                  ),
            new StrNum("End",                                   0                                               )
        };

        static StrNum[] StopWith =
        {
            new StrNum("Stop counter regardless of sampling",   (short)CcntConst.CNTS_STOP_CNT_NOT_WITH_SAMP    ),
            new StrNum("Stop counter when sampling stops",      (short)CcntConst.CNTS_STOP_CNT_WITH_SAMP        ),
            new StrNum("End",                                   0                                               )
        };

        static string szStart = "ExtSig2";
        static string szStartWith = "Start counter regardless of sampling";
        static string szClock = "ExtSig1";
        static string szStop = "ExtSig3";
        static string szStopWith = "Stop counter regardless of sampling";
        static uint StopNum = 1000;

        public static short S_IsRing = (short)CcntConst.CNTS_WRITE_ONCE;
        public static uint S_NotifyCount = 500;

        Ccnt cnt = new Ccnt();

        static short str_to_num(StrNum[] strnum, string str)
        {
            ushort i;
            for (i = 0; string.Compare(strnum[i].Str, "End") != 0; i++)
            {
                if (string.Compare(strnum[i].Str, str) == 0)
                {
                    return strnum[i].Num;
                }
            }
            return 0;
        }

        static string num_to_str(StrNum[] strnum, short num)
        {
            ushort i;
            for (i = 0; string.Compare(strnum[i].Str, "End") != 0; i++)
            {
                if (strnum[i].Num == num)
                {
                    return strnum[i].Str;
                }
            }
            return strnum[0].Str;
        }

        public SSampMode()
        {
            InitializeComponent();
        }

        //===================================================
        // Dialog initialization
        //===================================================
        private void SSampMode_Load(object sender, System.EventArgs e)
        {
            ushort i;
            string szBuf = new string(' ', 100);

            //--------------------------------
            // Initialize display
            //--------------------------------
            // Start
            for (i = 0; string.Compare(Start[i].Str, "End") != 0; i++)
            {
                cboStart.Items.Add(Start[i].Str);
            }
            // Start with
            for (i = 0; string.Compare(StartWith[i].Str, "End") != 0; i++)
            {
                cboStartWith.Items.Add(StartWith[i].Str);
            }
            // Clock
            for (i = 0; string.Compare(Clock[i].Str, "End") != 0; i++)
            {
                cboClock.Items.Add(Clock[i].Str);
            }
            // Stop
            for (i = 0; string.Compare(Stop[i].Str, "End") != 0; i++)
            {
                cboStop.Items.Add(Stop[i].Str);
            }
            // Stop with
            for (i = 0; string.Compare(StopWith[i].Str, "End") != 0; i++)
            {
                cboStopWith.Items.Add(StopWith[i].Str);
            }

            cboStart.Text = szStart;
            cboStartWith.Text = szStartWith;
            cboClock.Text = szClock;
            cboStop.Text = szStop;
            cboStopWith.Text = szStopWith;

            txtStopNumber.Text = StopNum.ToString();

            txtNumberData.Text = S_NotifyCount.ToString();

            if (S_IsRing == (short)CcntConst.CNTS_WRITE_ONCE)
            {
                rdoOnce.Checked = true;
                rdoRing.Checked = false;
            }
            else
            {
                rdoOnce.Checked = false;
                rdoRing.Checked = true;
            }
            cboStop_SelectedIndexChanged(cboStop, new System.EventArgs()); ;
        }

        //===================================================
        // OK button processing
        //===================================================
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);
            bool Checked;
            short dwStart;
            short ExtSig1, ExtSig2, ExtSig3;
            ushort wStartWith;
            short dwClock;
            short dwStop;
            ushort wStopWith;
            int ret;
            string strMsg;
            string error_string = new string(' ', 256);

            //--------------------------------
            // Get displayed data
            //--------------------------------
            szStart = cboStart.Text;
            szStartWith = cboStartWith.Text;
            szClock = cboClock.Text;
            szStop = cboStop.Text;
            szStopWith = cboStopWith.Text;

            szBuf = txtStopNumber.Text;
            try
            {
                StopNum = uint.Parse(szBuf);
            }
            catch (System.FormatException)
            {
                StopNum = 1000;
            }

            szBuf = txtNumberData.Text;
            try
            {
                S_NotifyCount = uint.Parse(szBuf);
            }
            catch (System.FormatException)
            {
                S_NotifyCount = 500;
            }

            Checked = rdoOnce.Checked;

            //--------------------------------
            // Convert setting value to a number
            //--------------------------------
            dwStart = str_to_num(Start, szStart);
            wStartWith = (ushort)str_to_num(StartWith, szStartWith);

            dwClock = str_to_num(Clock, szClock);

            dwStop = str_to_num(Stop, szStop);
            wStopWith = (ushort)str_to_num(StopWith, szStopWith);

            if (Checked == true)
            {
                S_IsRing = (short)CcntConst.CNTS_WRITE_ONCE;
            }
            else
            {
                S_IsRing = (short)CcntConst.CNTS_WRITE_RING;
            }

            //--------------------------------
            // Set sampling condition
            //--------------------------------
            ExtSig1 = 1;
            ExtSig2 = 1;
            ExtSig3 = 1;
            ret = cnt.SetSlave(Form1.S_Id, ExtSig1, ExtSig2, ExtSig3, 1, 1);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSlave ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, "Error", MessageBoxButtons.OK);
                return;
            }

            ret = cnt.SetSamplingStartTrigger(Form1.S_Id, dwStart, (short)wStartWith, Form1.CHANNEL_NUM);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingStartTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, "Error", MessageBoxButtons.OK);
                return;
            }
            ret = cnt.SetSamplingClockTrigger(Form1.S_Id, dwClock);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingClockTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, "Error", MessageBoxButtons.OK);
                return;
            }
            ret = cnt.SetSamplingStopTrigger(Form1.S_Id, dwStop, (short)wStopWith, Form1.CHANNEL_NUM);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingStopTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, "Error", MessageBoxButtons.OK);
                return;
            }
            ret = cnt.SetSamplingStopNumber(Form1.S_Id, StopNum);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingStopNumber ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, "Error", MessageBoxButtons.OK);
            }

            this.Close();
        }

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

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

            szBuf = cboStop.Text;
            if (string.Compare(szBuf, "Stop transfer by specified times") == 0)
            {
                txtStopNumber.Enabled = true;
            }
            else
            {
                txtStopNumber.Enabled = false;
            }
        }
    }
}
