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

namespace Counter
{
    public partial class Mode : Form
    {
        Ccnt cnt = new Ccnt();

        // static characters
        const int ZMODE_NUM = 3;
        const int ZLOGIC_NUM = 2;
        const int CHSIG_NUM = 3;
        const int CNTDIR_NUM = 2;
        const int FILTER_NUM = 15;
        const int PHASE_NUM = 3;
        const int MUL_NUM = 3;
        const int SYNCCLR_NUM = 2;

        static string[] szCombo_ZMode = new string[ZMODE_NUM]
        {
            "Not Use",
            "Next Time",
            "Every Time"
        };
        static string[] szCombo_ZLogic = new string[ZLOGIC_NUM]
        {
            "Positive Logic",
            "Negative Logic"
        };
        static string[] szCombo_ChannelSignal = new string[CHSIG_NUM]
        {
            "Photo-coupler Isolation",
            "TTL",
            "Differential"
        };
        static string[] szCombo_CntDir = new string[CNTDIR_NUM]
        {
            "Down-count",
            "Up-count"
        };
        static string[] szCombo_Phase = new string[PHASE_NUM]
        {
            "1 Phase",
            "2 Phase",
            "Gate Control"
        };
        static string[] szCombo_Mul = new string[MUL_NUM]
        {
            "x1",
            "x2",
            "x4"
        };
        static string[] szCombo_SyncClr = new string[SYNCCLR_NUM]
        {
            "Asynchronous",
            "Synchronous"
        };

        public Mode()
        {
            InitializeComponent();

            string csTmp;
            uint i;

            int Ret;
            short ChNo = 0;
            short Mode = 0;
            short ZLogic = 0;
            short SigType = 0;
            short Dir = 0;
            short Phase = 0;
            short Mul = 0;
            short SyncClr = 0;
            short FilterValue = 0;

            Ret = cnt.GetZMode(Form1.m_Id, ChNo, out Mode);
            if (Ret != 0)
            {
                csTmp = string.Format("CntGetZMode Return Code = {0:d5}", Ret);
                MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
                return;
            }
            Ret = cnt.GetZLogic(Form1.m_Id, ChNo, out ZLogic);
            Ret = cnt.GetChannelSignal(Form1.m_Id, ChNo, out SigType);
            Ret = cnt.GetCountDirection(Form1.m_Id, ChNo, out Dir);
            Ret = cnt.GetOperationMode(Form1.m_Id, ChNo, out Phase, out Mul, out SyncClr);
            Ret = cnt.GetDigitalFilter(Form1.m_Id, ChNo, out FilterValue);

            for (i = 0; i < ZMODE_NUM; i++)
            {
                comboBox_ZPhaseMode.Items.Add(szCombo_ZMode[i]);
            }
            comboBox_ZPhaseMode.SelectedIndex = Mode - 1;

            for (i = 0; i < ZLOGIC_NUM; i++)
            {
                comboBox_ZPhaseLogic.Items.Add(szCombo_ZLogic[i]);
            }
            comboBox_ZPhaseLogic.SelectedIndex = ZLogic;

            for (i = 0; i < CHSIG_NUM; i++)
            {
                comboBox_SignalSource.Items.Add(szCombo_ChannelSignal[i]);
            }
            comboBox_SignalSource.SelectedIndex = SigType;

            for (i = 0; i < CNTDIR_NUM; i++)
            {
                comboBox_Direction.Items.Add(szCombo_CntDir[i]);
            }
            comboBox_Direction.SelectedIndex = Dir;

            for (i = 0; i <= FILTER_NUM; i++)
            {
                csTmp = string.Format("{0:X}", i);
                comboBox_DigitalFilter.Items.Add(csTmp);
            }
            comboBox_DigitalFilter.SelectedIndex = FilterValue;

            for (i = 0; i < PHASE_NUM; i++)
            {
                comboBox_Phasenumber.Items.Add(szCombo_Phase[i]);
            }
            comboBox_Phasenumber.SelectedIndex = Phase;

            for (i = 0; i < MUL_NUM; i++)
            {
                comboBox_Multiple.Items.Add(szCombo_Mul[i]);
            }
            comboBox_Multiple.SelectedIndex = Mul;

            for (i = 0; i < SYNCCLR_NUM; i++)
            {
                comboBox_Synclear.Items.Add(szCombo_SyncClr[i]);
            }
            comboBox_Synclear.SelectedIndex = SyncClr;
        }

        private void button_OK_Click(object sender, System.EventArgs e)
        {
            string csTmp;
            int Index;

            int Ret;
            short ChNo = 0;
            short Mode = 0;
            short ZLogic = 0;
            short SigType = 0;
            short Dir = 0;
            short Phase = 0;
            short Mul = 0;
            short SyncClr = 0;
            short FilterValue = 0;

            //---------------------------------------------
            // Set Z Phase Mode
            //---------------------------------------------
            Index = comboBox_ZPhaseMode.SelectedIndex;
            switch (Index)
            {
                case 0:
                    Mode = (short)CcntConst.CNT_ZPHASE_NOT_USE;
                    break;
                case 1:
                    Mode = (short)CcntConst.CNT_ZPHASE_NEXT_ONE;
                    break;
                case 2:
                    Mode = (short)CcntConst.CNT_ZPHASE_EVERY_TIME;
                    break;
            }
            for (ChNo = 0; ChNo < Form1.m_ChannelNum; ChNo++)
            {
                Ret = cnt.SetZMode(Form1.m_Id, ChNo, (short)(Mode));
                if (Ret != 0)
                {
                    csTmp = string.Format("CntSetZMode Return Code = {0:d5}", Ret);
                    MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
                    return;
                }
            }
            //-----------------------------------
            // Set Z Phase Logic
            //-----------------------------------
            Index = comboBox_ZPhaseLogic.SelectedIndex;
            switch (Index)
            {
                case 0:
                    ZLogic = (short)CcntConst.CNT_ZLOGIC_POSITIVE;
                    break;
                case 1:
                    ZLogic = (short)CcntConst.CNT_ZLOGIC_NEGATIVE;
                    break;
            }
            for (ChNo = 0; ChNo < Form1.m_ChannelNum; ChNo++)
            {
                Ret = cnt.SetZLogic(Form1.m_Id, ChNo, ZLogic);
                if (Ret != 0)
                {
                    csTmp = string.Format("CntSetZLogic Return Code = {0:d5}", Ret);
                    MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
                    return;
                }
            }
            //-----------------------------------
            // Set Channel Signal
            //-----------------------------------
            Index = comboBox_SignalSource.SelectedIndex;
            switch (Index)
            {
                case 0:
                    SigType = (short)CcntConst.CNT_SIGTYPE_ISOLATE;
                    break;
                case 1:
                    SigType = (short)CcntConst.CNT_SIGTYPE_TTL;
                    break;
                case 2:
                    SigType = (short)CcntConst.CNT_SIGTYPE_LINERECEIVER;
                    break;
            }
            for (ChNo = 0; ChNo < Form1.m_ChannelNum; ChNo++)
            {
                Ret = cnt.SelectChannelSignal(Form1.m_Id, ChNo, SigType);
                if (Ret != 0)
                {
                    csTmp = string.Format("CntSelectChannelSignal Return Code = {0:d5}", Ret);
                    MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
                    return;
                }
            }
            //-----------------------------------
            // Set Count Direction
            //-----------------------------------
            Index = comboBox_Direction.SelectedIndex;
            switch (Index)
            {
                case 0:
                    Dir = (short)CcntConst.CNT_DIR_DOWN;
                    break;
                case 1:
                    Dir = (short)CcntConst.CNT_DIR_UP;
                    break;
            }
            for (ChNo = 0; ChNo < Form1.m_ChannelNum; ChNo++)
            {
                Ret = cnt.SetCountDirection(Form1.m_Id, ChNo, Dir);
                if (Ret != 0)
                {
                    csTmp = string.Format("CntSetCountDirection Return Code = {0:d5}", Ret);
                    MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
                    return;
                }
            }
            //-----------------------------------
            // Set Digital Filter
            //-----------------------------------
            FilterValue = (short)comboBox_DigitalFilter.SelectedIndex;
            for (ChNo = 0; ChNo < Form1.m_ChannelNum; ChNo++)
            {
                Ret = cnt.SetDigitalFilter(Form1.m_Id, ChNo, FilterValue);
                if (Ret != 0)
                {
                    csTmp = string.Format("CntSetDigitalFilter Return Code = {0:d5}", Ret);
                    MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
                    return;
                }
            }
            //-----------------------------------
            // Set Operation Mode
            //-----------------------------------
            //Set Phase
            Index = comboBox_Phasenumber.SelectedIndex;
            switch (Index)
            {
                case 0:
                    Phase = (short)CcntConst.CNT_MODE_1PHASE;
                    break;
                case 1:
                    Phase = (short)CcntConst.CNT_MODE_2PHASE;
                    break;
                case 2:
                    Phase = (short)CcntConst.CNT_MODE_GATECONTROL;
                    break;
            }
            //Set Multiple
            Index = comboBox_Multiple.SelectedIndex;
            switch (Index)
            {
                case 0:
                    Mul = (short)CcntConst.CNT_MUL_X1;
                    break;
                case 1:
                    Mul = (short)CcntConst.CNT_MUL_X2;
                    break;
                case 2:
                    Mul = (short)CcntConst.CNT_MUL_X4;
                    break;
            }
            //Set Sync Clear
            Index = comboBox_Synclear.SelectedIndex;
            switch (Index)
            {
                case 0:
                    SyncClr = (short)CcntConst.CNT_CLR_ASYNC;
                    break;
                case 1:
                    SyncClr = (short)CcntConst.CNT_CLR_SYNC;
                    break;
            }
            for (ChNo = 0; ChNo < Form1.m_ChannelNum; ChNo++)
            {
                Ret = cnt.SetOperationMode(Form1.m_Id, ChNo, Phase, Mul, SyncClr);
                if (Ret != 0)
                {
                    csTmp = string.Format("CntSetOperationMode Return Code = {0:d5}", Ret);
                    MessageBox.Show(csTmp, "ERROR", MessageBoxButtons.OK);
                    return;
                }
            }
            this.Close();
        }

        private void button_Cancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}
