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.TIMBER
{
    public partial class TIMBERForm : Form
    {
        public TIMBERForm()
        {
            InitializeComponent();
        }

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

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

        private void TIMBER_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 = 143;
                    CanData dataToWrite = new CanData();
                    //Thread.Sleep(1000);
                    byte register = 10;
                    switch (button.Name)
                    {
                        case ("TIMBER_shot"):
                            dataToWrite.SetAsUchar((byte)(16));
                            break;
                        default:
                            return;
                        //break;
                    }
                    RegisterKey regKey = new RegisterKey(weaponNodeId, register);
                    mainform.canConnection.SendRegisterWriteRequest(weaponChannel, regKey, dataToWrite);
                }

            }

        }

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

            byte register = 0;
            switch (editbox.Name)
            {
                case ("TIMBER_treshlow"):
                    register = 29;
                    dataToWrite.SetAsUchar((byte)(editbox.Value));
                    break;
                case ("TIMBER_treshhigh"):
                    register = 30;
                    dataToWrite.SetAsUchar((byte)(editbox.Value));
                    break;
                case ("TIMBER_triggerrelease"):
                    register = 31;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("TIMBER_triggerhold"):
                    register = 32;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("TIMBER_triggerdaoffset"):
                    register = 33;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("TIMBER_boltlocktresh"):
                    register = 100;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("TIMBER_boltunlocktresh"):
                    register = 101;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("TIMBER_firingpintresh"):
                    register = 102;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("TIMBER_firingpindaoffset"):
                    register = 103;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("TIMBER_safetresh"):
                    register = 104;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("TIMBER_safedaoffset"):
                    register = 105;
                    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");
                }

            }

        }


    }
}
