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

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

        private void GP34Form_Load(object sender, EventArgs e)
        {
            StartPosition = FormStartPosition.Manual;
            Location = Properties.Settings.Default.GP34_FormLocation;
        }
        
        /*private void GP34_shot_Click(object sender, EventArgs e)
        {
            GP34_triggerposition.Value = 45;

            GP34_serial.Text = "" + 9999;
            GP34_weapon.Text = "99";
            GP34_year.Text = "99";
            GP34_address.Text = "99";
            GP34_hw.Text = "99";
            GP34_sw.Text = "99";
            GP34_shots.Text = "99";
            GP34_powerons.Text = "99";

            GP34_trigger1low.Value = 9999;
            GP34_trigger1high.Value = 9999;
            GP34_trigger2low.Value = 9999;
            GP34_trigger2high.Value = 9999;

            GP34_rs0m.Value = 9999;
            GP34_rs100m.Value = 9999;
            GP34_rs150m.Value = 9999;
            GP34_rs200m.Value = 9999;
            GP34_rs250m.Value = 9999;
            GP34_rs300m.Value = 9999;
            GP34_rs350m.Value = 9999;
            GP34_rs400m.Value = 9999;
            GP34_rsx350m.Value = 9999;
            GP34_rsx300m.Value = 9999;
            GP34_rsx250m.Value = 9999;
            GP34_rsx200m.Value = 9999;

            GP34_safe.ForeColor = Color.FromKnownColor(KnownColor.Silver);
            GP34_safe.ForeColor = Color.FromKnownColor(KnownColor.Lime);
            GP34_unsafe.ForeColor = Color.FromKnownColor(KnownColor.Silver);
            GP34_unsafe.ForeColor = Color.FromKnownColor(KnownColor.Lime);
            GP34_smk.ForeColor = Color.FromKnownColor(KnownColor.Silver);
            GP34_smk.ForeColor = Color.FromKnownColor(KnownColor.Lime);
            GP34_heap.ForeColor = Color.FromKnownColor(KnownColor.Silver);
            GP34_heap.ForeColor = Color.FromKnownColor(KnownColor.Lime);
            GP34_he.ForeColor = Color.FromKnownColor(KnownColor.Silver);
            GP34_he.ForeColor = Color.FromKnownColor(KnownColor.Lime);
            GP34_loaded.ForeColor = Color.FromKnownColor(KnownColor.Silver);
            GP34_loaded.ForeColor = Color.FromKnownColor(KnownColor.Lime);
            GP34_ready.ForeColor = Color.FromKnownColor(KnownColor.Silver);
            GP34_ready.ForeColor = Color.FromKnownColor(KnownColor.Lime);
            GP34_trigger13.ForeColor = Color.FromKnownColor(KnownColor.Silver);
            GP34_trigger13.ForeColor = Color.FromKnownColor(KnownColor.Lime);
            GP34_trigger23.ForeColor = Color.FromKnownColor(KnownColor.Silver);
            GP34_trigger23.ForeColor = Color.FromKnownColor(KnownColor.Lime);
            GP34_trigger33.ForeColor = Color.FromKnownColor(KnownColor.Silver);
            GP34_trigger33.ForeColor = Color.FromKnownColor(KnownColor.Lime);
        }*/

        private void GP34_refresh_Click(object sender, EventArgs e)
        {
            Form1 mainform = Application.OpenForms.OfType<Form1>().First();
            mainform.ActiveForm2 = null;
            this.Close();
        }

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

            }

        }

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

            byte register = 0;
            switch (editbox.Name)
            {
                case ("GP34_trigger1low"):
                    register = 29;
                    dataToWrite.SetAsUchar((byte)(editbox.Value));
                    break;
                case ("GP34_trigger1high"):
                    register = 30;
                    dataToWrite.SetAsUchar((byte)(editbox.Value));
                    break;
                case ("GP34_trigger2low"):
                    register = 33;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_trigger2high"):
                    register = 34;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_rs0m"):
                    register = 100;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_rs100m"):
                    register = 101;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_rs150m"):
                    register = 102;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_rs200m"):
                    register = 103;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_rs250m"):
                    register = 104;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_rs300m"):
                    register = 105;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_rs350m"):
                    register = 106;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_rs400m"):
                    register = 107;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_rsx350m"):
                    register = 108;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_rsx300m"):
                    register = 109;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_rsx250m"):
                    register = 110;
                    dataToWrite.SetAsShort((short)(editbox.Value));
                    break;
                case ("GP34_rsx200m"):
                    register = 111;
                    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");
                }

            }

        }
        
    }
}
