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;

namespace arinc_1553_CSharpGUI
{
    public partial class ArincTestForm : Form
    {
        DDCARINC429 pDDCARINC429;

        public ArincTestForm(byte numTxChans, DDCARINC429 paraDDCARINC429)
        {
            InitializeComponent();

            comboBox_TxChannel.Items.Clear();

            if(numTxChans > 0)
            {
                /* Populate the TxChannel comboBox based on number of tx channels */
                for (byte u8Chan = 1; u8Chan <= numTxChans; u8Chan++)
                {
                    comboBox_TxChannel.Items.Add(u8Chan);
                }
                comboBox_TxChannel.SelectedIndex = 0;
            }

            pDDCARINC429 = paraDDCARINC429;
        }

        private void button_Tx_Click(object sender, EventArgs e)
        {
            uint num;
            try
            {
                num = uint.Parse(textBoxHexData.Text, System.Globalization.NumberStyles.AllowHexSpecifier);

                //MessageBox.Show("Valid data value entered.", "ARINC 429 Transmit Message", MessageBoxButtons.OK);

                if (pDDCARINC429.TxData(Byte.Parse(comboBox_TxChannel.SelectedItem.ToString()), num) != 1)
                {
                    MessageBox.Show("Error in Tx Data.",
                                            "Tx Arinc error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }

                MessageBox.Show("Tx Data successful!",
                                            "Tx Data Arinc", MessageBoxButtons.OK);
            }
            catch
            {
                MessageBox.Show("Invalid Tx Channel or Invalid data value entered. Please select a Tx Channel or enter a valid hex value.", "ARINC 429 Transmit Message", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                textBoxHexData.Focus();
                textBoxHexData.SelectAll();
            }
        }

        private void button_ScheduleTx_Click(object sender, EventArgs e)
        {
            uint num;
            try
            {
                num = uint.Parse(textBoxHexData.Text, System.Globalization.NumberStyles.AllowHexSpecifier);

                if (pDDCARINC429.ScheduleTx(Byte.Parse(comboBox_TxChannel.SelectedItem.ToString()), num) != 0)
                {
                    MessageBox.Show("Error in Schedule Tx.",
                                            "Schedule Tx Arinc error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }

                MessageBox.Show("Schedule Tx successful!",
                                            "Schedule Tx Arinc", MessageBoxButtons.OK);

                button_ScheduleTx.Enabled = false;
            }
            catch
            {
                MessageBox.Show("Invalid Tx Channel or Invalid data value entered. Please select a Tx Channel or enter a valid hex value.", "ARINC 429 Schedule Tx", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                textBoxHexData.Focus();
                textBoxHexData.SelectAll();
            }
        }

        private void button_StopTx_Click(object sender, EventArgs e)
        {
            uint num;
            try
            {
                num = uint.Parse(textBoxHexData.Text, System.Globalization.NumberStyles.AllowHexSpecifier);

                if (pDDCARINC429.StopTx(Byte.Parse(comboBox_TxChannel.SelectedItem.ToString()), num) != 0)
                {
                    MessageBox.Show("Error in Stop Tx.",
                                            "Stop Tx Arinc error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }

                MessageBox.Show("Stop Tx successful!",
                                            "Stop Tx Arinc", MessageBoxButtons.OK);

                button_ScheduleTx.Enabled = true;
            }
            catch
            {
                MessageBox.Show("Invalid Tx Channel or Invalid data value entered. Please select a Tx Channel or enter a valid hex value.", "ARINC 429 Stop Tx", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                textBoxHexData.Focus();
                textBoxHexData.SelectAll();
            }
        }
    }
}
