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

namespace Counter
{
    public partial class Event : Form
    {
        // Signal input event
        static Mode.StrNum[] InputEvent =
        {
            new Mode.StrNum("General Input",         (short)CcntConst.CNT_IEVENT_GENINP),
            new Mode.StrNum("Counter Start/Stop", (short)CcntConst.CNT_IEVENT_STARTSTOP),
            new Mode.StrNum("Counter Preset",    (short)CcntConst.CNT_IEVENT_PRESET),
            new Mode.StrNum("Counter Clear",         (short)CcntConst.CNT_IEVENT_CLEAR),
            new Mode.StrNum("End",               0)
        };

        // Counter event
        static Mode.StrNum[] CountEvent =
        {
            new Mode.StrNum("None",             (short)CcntConst.CNT_MATCH_NONE),
            new Mode.StrNum("Preset",           (short)CcntConst.CNT_MATCH_PRESET),
            new Mode.StrNum("Clear",                (short)CcntConst.CNT_MATCH_CLEAR),
            new Mode.StrNum("End",              0)
        };

        // Rise/fall
        static Mode.StrNum[] RF =
        {
            new Mode.StrNum("Rise",         (short)CcntConst.CNT_INP_RIZE),
            new Mode.StrNum("Fall",         (short)CcntConst.CNT_INP_FALL),
            new Mode.StrNum("None",         (short)CcntConst.CNT_INP_NONE),
            new Mode.StrNum("End",          0)
        };

        // Pulse width
        static Mode.StrNum[] PulseLength =
        {
            new Mode.StrNum("0",                    0x00),
            new Mode.StrNum("1",                    0x01),
            new Mode.StrNum("2",                    0x02),
            new Mode.StrNum("3",                    0x03),
            new Mode.StrNum("4",                    0x04),
            new Mode.StrNum("5",                    0x05),
            new Mode.StrNum("6",                    0x06),
            new Mode.StrNum("7",                    0x07),
            new Mode.StrNum("8",                    0x08),
            new Mode.StrNum("9",                    0x09),
            new Mode.StrNum("A",                    0x0A),
            new Mode.StrNum("B",                    0x0B),
            new Mode.StrNum("C",                    0x0C),
            new Mode.StrNum("D",                    0x0D),
            new Mode.StrNum("E",                    0x0E),
            new Mode.StrNum("F",                    0x0F),
            new Mode.StrNum("End",                  0)
        };

        static string szInputEvent = "General Input";
        static int PresetReg = 0x1000;
        static short RF0 = (short)CcntConst.CNT_INP_NONE;
        static short RF1 = (short)CcntConst.CNT_INP_NONE;
        static int OutputEvent = (int)CcntConst.CNT_OEVENT_GENOUT;
        static string szPulseLength = "0";
        static string szCount = "None";

        Ccnt cnt = new Ccnt();

        public Event()
        {
            InitializeComponent();
        }

        //===================================================
        // Dialog initialization
        //===================================================
        private void frmEvent_Load(object sender, System.EventArgs e)
        {
            //===================================================
            // Initialize combo boxes
            //===================================================
            short i;
            for (i = 0; string.Compare(InputEvent[i].Str, "End") != 0; i++)
            {
                cboInputEvent.Items.Add(InputEvent[i].Str);
            }
            for (i = 0; string.Compare(RF[i].Str, "End") != 0; i++)
            {
                cboRF0.Items.Add(RF[i].Str);
            }
            for (i = 0; string.Compare(RF[i].Str, "End") != 0; i++)
            {
                cboRF1.Items.Add(RF[i].Str);
            }
            for (i = 0; string.Compare(PulseLength[i].Str, "End") != 0; i++)
            {
                cboPulseLength.Items.Add(PulseLength[i].Str);
            }
            for (i = 0; string.Compare(CountEvent[i].Str, "End") != 0; i++)
            {
                cboCount.Items.Add(CountEvent[i].Str);
            }
            //===================================================
            // Initialize data
            //===================================================
            // Input event signal
            string szBuf = new string(' ', 100);
            cboInputEvent.Text = szInputEvent;
            szBuf = string.Format("{0:X}", PresetReg);
            txtPresetReg.Text = szBuf;
            cboRF0.Text = Mode.num_to_str(RF, RF0);
            cboRF1.Text = Mode.num_to_str(RF, RF1);
            cboCount.Text = szCount;
            cboInputEvent_SelectedIndexChanged(cboInputEvent, new System.EventArgs());
            // Output event signal
            if (OutputEvent == (int)CcntConst.CNT_OEVENT_GENOUT)
            {
                rdoGenOutput.Checked = true;
                cboPulseLength.Enabled = false;
                txtChNo.Enabled = true;
                txtData.Enabled = true;
                btnOutPulse.Enabled = true;
            }
            if ((OutputEvent & (int)CcntConst.CNT_OEVENT_MATCH0) != 0)
            {
                rdoCountMatch0.Checked = true;
                cboPulseLength.Enabled = true;
                txtChNo.Enabled = false;
                txtData.Enabled = false;
                btnOutPulse.Enabled = false;
            }
            if ((OutputEvent & (int)CcntConst.CNT_OEVENT_MATCH1) != 0)
            {
                rdoCountMatch1.Checked = true;
                cboPulseLength.Enabled = true;
                txtChNo.Enabled = false;
                txtData.Enabled = false;
                btnOutPulse.Enabled = false;
            }
            if ((OutputEvent & (int)CcntConst.CNT_OEVENT_AIERR) != 0)
            {
                rdoAiError.Checked = true;
                cboPulseLength.Enabled = true;
                txtChNo.Enabled = false;
                txtData.Enabled = false;
                btnOutPulse.Enabled = false;
            }
            if ((OutputEvent & (int)CcntConst.CNT_OEVENT_FLTERR) != 0)
            {
                rdoFilterError.Checked = true;
                cboPulseLength.Enabled = true;
                txtChNo.Enabled = false;
                txtData.Enabled = false;
                btnOutPulse.Enabled = false;
            }
            if ((OutputEvent & (int)CcntConst.CNT_OEVENT_ALMERR) != 0)
            {
                rdoAlarmError.Checked = true;
                cboPulseLength.Enabled = true;
                txtChNo.Enabled = false;
                txtData.Enabled = false;
                btnOutPulse.Enabled = false;
            }
            cboPulseLength.Text = szPulseLength;
            txtChNo.Text = "0";
            txtData.Text = "1";
        }

        //---------------------------------------------
        // Setting completion processing
        //---------------------------------------------
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            uint dwInputEvent;
            short wPulseLength;
            uint dwCountEvent;
            short ChNo;
            int ret;
            string str;
            string buf = new string(' ', 100);
            string error_string = new string(' ', 256);

            //-------------------------------
            // Get the set information
            //-------------------------------
            // Input event signal
            szInputEvent = cboInputEvent.Text;
            dwInputEvent = (uint)Mode.str_to_num(InputEvent, szInputEvent);

            buf = txtPresetReg.Text;
            PresetReg = int.Parse(buf, NumberStyles.HexNumber);

            buf = cboRF0.Text;
            RF0 = Mode.str_to_num(RF, buf);

            buf = cboRF1.Text;
            RF1 = Mode.str_to_num(RF, buf);
            // Output event signal
            OutputEvent = (int)CcntConst.CNT_OEVENT_GENOUT;

            if (rdoCountMatch0.Checked == true)
            {
                OutputEvent |= (int)CcntConst.CNT_OEVENT_MATCH0;
            }
            else if (rdoCountMatch1.Checked == true)
            {
                OutputEvent |= (int)CcntConst.CNT_OEVENT_MATCH1;
            }
            else if (rdoAiError.Checked == true)
            {
                OutputEvent |= (int)CcntConst.CNT_OEVENT_AIERR;
            }
            else if (rdoFilterError.Checked == true)
            {
                OutputEvent |= (int)CcntConst.CNT_OEVENT_FLTERR;
            }
            else if (rdoAlarmError.Checked == true)
            {
                OutputEvent |= (int)CcntConst.CNT_OEVENT_ALMERR;
            }
            szPulseLength = cboPulseLength.Text;
            wPulseLength = Mode.str_to_num(PulseLength, szPulseLength);
            // Count match event
            szCount = cboCount.Text;
            dwCountEvent = (uint)Mode.str_to_num(CountEvent, szCount);

            //-------------------------------
            // Set the set information to device
            //-------------------------------
            for (ChNo = 0; ChNo < Form1.ChannelNum; ChNo++)
            {
                //---------------------------------------------
                // Set hardware event of control input signal
                //---------------------------------------------
                ret = cnt.SetInputHardwareEvent(Form1.Id, ChNo, dwInputEvent, RF0, RF1, 0);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntSetInputHardwareEvent : ret = {0:X} h\n {1}", ret, error_string);
                    MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                    goto ERR_END;
                }
                //---------------------------------------------
                // Set preset register
                //---------------------------------------------
                if (dwInputEvent == (uint)CcntConst.CNT_IEVENT_PRESET || dwCountEvent == (uint)CcntConst.CNT_MATCH_PRESET)
                {
                    ret = cnt.SetPresetRegister(Form1.Id, ChNo, (uint)PresetReg, 0);
                    if (ret != 0)
                    {
                        cnt.GetErrorString(ret, out error_string);
                        str = string.Format("CntSetPresetRegister : ret = {0:X} h\n {1}", ret, error_string);
                        MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                        goto ERR_END;
                    }
                }
                //---------------------------------------------
                // Set hardware event of control output signal
                //---------------------------------------------
                ret = cnt.SetOutputHardwareEvent(Form1.Id, ChNo, 0, (uint)OutputEvent, wPulseLength);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntSetOutputHardwareEvent : ret = {0:X} h\n {1}", ret, error_string);
                    MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                    goto ERR_END;
                }
                //---------------------------------------------
                // Set hardware event of count match
                //---------------------------------------------
                ret = cnt.SetCountMatchHardwareEvent(Form1.Id, ChNo, 0, dwCountEvent, 0);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    str = string.Format("CntSetCountMatchHardwareEvent : ret = {0:X} h\n {1}", ret, error_string);
                    MessageBox.Show(str, "Error", MessageBoxButtons.OK);
                    goto ERR_END;
                }
            }
        ERR_END:
            this.Close();
        }

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

        private void cboInputEvent_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szInput = new string(' ', 100);
            string szCount = new string(' ', 100);

            szInput = cboInputEvent.Text;
            szCount = cboCount.Text;
            if (string.Compare(szInput, "General Input") == 0)
            {
                cboRF0.Text = "None";
                cboRF1.Text = "None";
                cboRF0.Enabled = false;
                cboRF1.Enabled = false;
                txtPresetReg.Enabled = false;
            }
            if (string.Compare(szInput, "Counter Start/Stop") == 0)
            {
                cboRF0.Enabled = true;
                cboRF1.Enabled = true;
                txtPresetReg.Enabled = false;
            }
            if (string.Compare(szInput, "Counter Preset") == 0)
            {
                cboRF0.Enabled = true;
                cboRF1.Text = "None";
                cboRF1.Enabled = false;
                txtPresetReg.Enabled = true;
            }
            if (string.Compare(szInput, "Counter Clear") == 0)
            {
                cboRF0.Enabled = true;
                cboRF1.Text = "None";
                cboRF1.Enabled = false;
                txtPresetReg.Enabled = false;
            }
            if (string.Compare(szCount, "Preset") == 0)
            {
                txtPresetReg.Enabled = true;
            }
        }

        private void cboCount_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            cboInputEvent_SelectedIndexChanged(cboInputEvent, new System.EventArgs());
        }

        //---------------------------------------------
        // Set event signal output
        //---------------------------------------------
        private void rdoGenOutput_CheckedChanged(object sender, System.EventArgs e)
        {
            cboPulseLength.Enabled = false;
            txtChNo.Enabled = true;
            txtData.Enabled = true;
            btnOutPulse.Enabled = true;
        }

        private void rdoCountMatch0_CheckedChanged(object sender, System.EventArgs e)
        {
            cboPulseLength.Enabled = true;
            txtChNo.Enabled = false;
            txtData.Enabled = false;
            btnOutPulse.Enabled = false;
        }

        private void rdoCountMatch1_CheckedChanged(object sender, System.EventArgs e)
        {
            cboPulseLength.Enabled = true;
            txtChNo.Enabled = false;
            txtData.Enabled = false;
            btnOutPulse.Enabled = false;
        }

        private void rdoAiError_CheckedChanged(object sender, System.EventArgs e)
        {
            cboPulseLength.Enabled = true;
            txtChNo.Enabled = false;
            txtData.Enabled = false;
            btnOutPulse.Enabled = false;
        }

        private void rdoFilterError_CheckedChanged(object sender, System.EventArgs e)
        {
            cboPulseLength.Enabled = true;
            txtChNo.Enabled = false;
            txtData.Enabled = false;
            btnOutPulse.Enabled = false;
        }

        private void rdoAlarmError_CheckedChanged(object sender, System.EventArgs e)
        {
            cboPulseLength.Enabled = true;
            txtChNo.Enabled = false;
            txtData.Enabled = false;
            btnOutPulse.Enabled = false;
        }

        //---------------------------------------------
        // Signal output processing
        //---------------------------------------------
        private void btnOutPulse_Click(object sender, System.EventArgs e)
        {
            string buf = new string(' ', 100);
            short ChNo;
            byte Data;
            int ret;
            string str;
            string error_string = new string(' ', 256);

            buf = txtChNo.Text;
            try
            {
                ChNo = short.Parse(buf);
            }
            catch (System.FormatException)
            {
                ChNo = 0;
            }
            buf = txtData.Text;
            try
            {
                Data = byte.Parse(buf);
            }
            catch (System.FormatException)
            {
                Data = 0;
            }

            //---------------------------------------------
            // General output processing
            //---------------------------------------------
            ret = cnt.OutputDOBit(Form1.Id, ChNo, 0, Data);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                str = string.Format("CntOutPulse : ret = {0:X} h\n {1}", ret, error_string);
                MessageBox.Show(str, "Error", MessageBoxButtons.OK);
            }
        }
    }
}
