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 CanAerospaceCore;
using FTD2XX_NET;
using System.Threading;

namespace WindowsFormsApplication2.MG3
{
    public partial class MG3Form : Form
    {
        public MG3Form()
        {
            InitializeComponent();
        }

        private void MG3Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.MG3_FormLocation = Location;
            Properties.Settings.Default.Save();
        }

        private void MG3Form_Load(object sender, EventArgs e)
        {
            StartPosition = FormStartPosition.Manual;
            Location = Properties.Settings.Default.MG3_FormLocation;
        }
       
        private void MG3_refresh_Click(object sender, EventArgs e)
        {
            Form1 mainform = Application.OpenForms.OfType<Form1>().First();
            mainform.ActiveForm2 = null;
            this.Close();
        }

        private void MG3_button_click(object sender, EventArgs e)
        {
            if (Application.OpenForms.OfType<Form1>().Any())
            {
                Form1 mainform = Application.OpenForms.OfType<Form1>().First();
                if (mainform.ActiveForm2 != null)
                {
                    Button button = sender as Button;
                    uint weaponChannel = 100;
                    byte weaponNodeId = 136;
                    CanData dataToWrite = new CanData();
                    //Thread.Sleep(1000);
                    byte register = 10;
                    switch (button.Name)
                    {
                        case ("MG3_shot"):
                            dataToWrite.SetAsUchar((byte)(16));
                            break;
                        case ("MG3_release"):
                            dataToWrite.SetAsUchar((byte)(17));
                            break;
                        default:
                            return;
                        //break;
                    }
                    RegisterKey regKey = new RegisterKey(weaponNodeId, register);
                    mainform.canConnection.SendRegisterWriteRequest(weaponChannel, regKey, dataToWrite);
                }

            }

        }

        public void MG3_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown editbox = sender as NumericUpDown;
            uint weaponChannel = 100;
            byte weaponNodeId = 136;
            CanData dataToWrite = new CanData();
            //Thread.Sleep(1000);

            byte register = 0;
            switch (editbox.Name)
            {
                case ("MG3_treshlow"):
                    register = 29;
                    dataToWrite.SetAsUchar((byte)(editbox.Value));
                    break;
                case ("MG3_treshhigh"):
                    register = 30;
                    dataToWrite.SetAsUchar((byte)(editbox.Value));
                    break;
                case ("MG3_hs3release"):
                    register = 33;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("MG3_hs3hold"):
                    register = 34;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("MG3_tempdown"):
                    register = 60;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("MG3_tempup"):
                    register = 61;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("MG3_maxledpwm"):
                    register = 62;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("MG3_downstep1"):
                    register = 63;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("MG3_downstep2"):
                    register = 64;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("MG3_downstep3"):
                    register = 65;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("MG3_upstep1"):
                    register = 66;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("MG3_upstep2"):
                    register = 67;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("MG3_upstep3"):
                    register = 68;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                default:
                    return;
                //break;
            }
            if (register == 0) return;
            if (Application.OpenForms.OfType<Form1>().Any())
            {
                Form1 mainform = Application.OpenForms.OfType<Form1>().First();
                if (mainform.ActiveForm2 != null)
                {
                    RegisterKey regKey = new RegisterKey(weaponNodeId, register);
                    if (mainform.canConnection.SendRegisterWriteRequest(weaponChannel, regKey, dataToWrite)) MessageBox.Show("Value changed");
                }

            }

        }


    }
}
