using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TheDarkWraith.WM16MEV
{
    public partial class GlobalTrims : Form
    {
        #region Variables
        private WM16MEV wm16ev = null;
        private int currentfueltrim = 0;
        private int currentvertcyltrim = 0;
        private int currentsparktrim = 0;
        private bool loaded = false;
        #endregion

        #region Constructor
        public GlobalTrims(WM16MEV wm16evpassed)
        {
            InitializeComponent();

            wm16ev = wm16evpassed;

            BitArray globaltrims = wm16ev.TDWGlobalTrims;
            byte newvalue = 0;

            cbFuelEnabled.Checked = globaltrims[WM16MEV.ctdwglobaltrims_fuel_enabled];
            //nudFuelTrim.Value = (wm16ev.TDWGlobalFuelTrimValue - 0x80);
            newvalue = wm16ev.TDWGlobalFuelTrimValue;
            if (newvalue > 0x80)
            {
                nudFuelTrim.Value = -(0x100 - newvalue);
            }
            else
            {
                nudFuelTrim.Value = newvalue;
            }
            currentfueltrim = (int)nudFuelTrim.Value;
            //newvalue = (byte)(nudFuelTrim.Value + 0x80);
            //toolTip1.SetToolTip(nudFuelTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((newvalue - 0x80) * WM16MEV.cfueltableconversion).ToString("F2") + "mS");
            toolTip1.SetToolTip(nudFuelTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((float)nudFuelTrim.Value * WM16MEV.cfueltableconversion).ToString("F2") + "mS");

            cbVerticalCylinderEnabled.Checked = globaltrims[WM16MEV.ctdwglobaltrims_vertcyl_enabled];
            //nudVerticalCylinderTrim.Value = (wm16ev.TDWGlobalVertCylTrimValue - 0x80);
            newvalue = wm16ev.TDWGlobalVertCylTrimValue;
            if (newvalue > 0x80)
            {
                nudVerticalCylinderTrim.Value = -(0x100 - newvalue);
            }
            else
            {
                nudVerticalCylinderTrim.Value = newvalue;
            }
            currentvertcyltrim = (int)nudVerticalCylinderTrim.Value;
            //newvalue = (byte)(nudVerticalCylinderTrim.Value + 0x80);
            //toolTip1.SetToolTip(nudVerticalCylinderTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((newvalue - 128) / 32.0).ToString("F2") + "mS");
            toolTip1.SetToolTip(nudVerticalCylinderTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((float)nudVerticalCylinderTrim.Value / 32).ToString("F2") + "mS");

            cbSparkEnabled.Checked = globaltrims[WM16MEV.ctdwglobaltrims_spark_enabled];
            //nudSparkTrim.Value = (wm16ev.TDWGlobalSparkTrimValue - 0x80);
            newvalue = wm16ev.TDWGlobalSparkTrimValue;
            if (newvalue > 0x80)
            {
                nudSparkTrim.Value = -(0x100 - newvalue);
            }
            else
            {
                nudSparkTrim.Value = newvalue;
            }
            currentsparktrim = (int)nudSparkTrim.Value;
            //newvalue = (byte)(nudSparkTrim.Value + 0x80);
            //toolTip1.SetToolTip(nudSparkTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((newvalue - 0x80) * WM16MEV.csparktableconversion).ToString("F2") + " degrees BTDC");
            toolTip1.SetToolTip(nudSparkTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((float)nudSparkTrim.Value * WM16MEV.csparktableconversion).ToString("F2") + " degrees BTDC");

            loaded = true;
        }
        #endregion

        #region Destroy
        internal void Destroy()
        {
            wm16ev = null;
        }
        #endregion

        #region nudFuelTrim_ValueChanged
        private void nudFuelTrim_ValueChanged(object sender, EventArgs e)
        {
            byte newvalue = 0;

            if (!loaded)
            {
                //newvalue = (byte)(nudFuelTrim.Value + 0x80);
                if (nudFuelTrim.Value < 0)
                {
                    newvalue = (byte)(nudFuelTrim.Value + 0x100);
                }
                else
                {
                    newvalue = (byte)nudFuelTrim.Value;
                }
                //toolTip1.SetToolTip(nudFuelTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((newvalue - 0x80) * WM16MEV.cfueltableconversion).ToString("F2") + "mS");
                toolTip1.SetToolTip(nudFuelTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((float)nudFuelTrim.Value * WM16MEV.cfueltableconversion).ToString("F2") + "mS");
                return;
            }

            loaded = false;
            wm16ev.DisplayDialog("Updating Global trims in ECU...", true);
            try
            {
                bool result = wm16ev.IncreaseDecreaseTDWGlobalFuelTrim(((int)nudFuelTrim.Value - currentfueltrim > 0));
                newvalue = wm16ev.TDWGlobalFuelTrimValue;
                if (newvalue > 0x80)
                {
                    nudFuelTrim.Value = -(0x100-newvalue);
                }
                else
                {
                    nudFuelTrim.Value = newvalue;
                }
                //nudFuelTrim.Value = (wm16ev.TDWGlobalFuelTrimValue - (byte)0x80);
                if (result)
                {
                    if (!wm16ev.ReceivedByteResult || !wm16ev.TransmitByteResult)
                    {
                        nudFuelTrim.Enabled = false;
                    }
                }
                else
                {
                    nudFuelTrim.Enabled = false;
                }
            }
            finally
            {
                wm16ev.DisplayDialog("", false);
                loaded = true;
            }

            //newvalue = (byte)(nudFuelTrim.Value + 0x80);
            //toolTip1.SetToolTip(nudFuelTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((newvalue - 0x80) * WM16MEV.cfueltableconversion).ToString("F2") + "mS");
            toolTip1.SetToolTip(nudFuelTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((int)nudFuelTrim.Value * WM16MEV.cfueltableconversion).ToString("F2") + "mS");

            currentfueltrim = (int)nudFuelTrim.Value;
        }
        #endregion

        #region nudVerticalCylinderTrim_ValueChanged
        private void nudVerticalCylinderTrim_ValueChanged(object sender, EventArgs e)
        {
            byte newvalue = 0;

            if (!loaded)
            {
                //newvalue = (byte)(nudVerticalCylinderTrim.Value + 0x80);
                if (nudVerticalCylinderTrim.Value < 0)
                {
                    newvalue = (byte)(nudVerticalCylinderTrim.Value + 0x100);
                }
                else
                {
                    newvalue = (byte)nudVerticalCylinderTrim.Value;
                }
                //toolTip1.SetToolTip(nudVerticalCylinderTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((newvalue-128)/32.0).ToString("F2") + "mS");
                toolTip1.SetToolTip(nudVerticalCylinderTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((int)nudVerticalCylinderTrim.Value * WM16MEV.cfueltableconversion).ToString("F2") + "mS");
                return;
            }

            loaded = false;
            wm16ev.DisplayDialog("Updating Global trims in ECU...", true);
            try
            {
                bool result = wm16ev.IncreaseDecreaseTDWGlobalVertCylTrim(((int)nudVerticalCylinderTrim.Value - currentvertcyltrim > 0));
                newvalue = wm16ev.TDWGlobalVertCylTrimValue;
                if (newvalue > 0x80)
                {
                    nudVerticalCylinderTrim.Value = -(0x100 - newvalue);
                }
                else
                {
                    nudVerticalCylinderTrim.Value = newvalue;
                }
                //nudVerticalCylinderTrim.Value = (wm16ev.TDWGlobalVertCylTrimValue - (byte)0x80);
                if (result)
                {
                    if (!wm16ev.ReceivedByteResult || !wm16ev.TransmitByteResult)
                    {
                        nudVerticalCylinderTrim.Enabled = false;
                    }
                }
                else
                {
                    nudVerticalCylinderTrim.Enabled = false;
                }
            }
            finally
            {
                wm16ev.DisplayDialog("", false);
                loaded = true;
            }

            //newvalue = (byte)(nudVerticalCylinderTrim.Value + 0x80);
            //toolTip1.SetToolTip(nudVerticalCylinderTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((newvalue - 128) / 32.0).ToString("F2") + "mS");
            toolTip1.SetToolTip(nudVerticalCylinderTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((int)nudVerticalCylinderTrim.Value * WM16MEV.cfueltableconversion).ToString("F2") + "mS");

            currentvertcyltrim = (int)nudVerticalCylinderTrim.Value;
        }
        #endregion

        #region nudSparkTrim_ValueChanged
        private void nudSparkTrim_ValueChanged(object sender, EventArgs e)
        {
            byte newvalue = 0;

            if (!loaded)
            {
                //newvalue = (byte)(nudSparkTrim.Value + 0x80);
                if (nudSparkTrim.Value < 0)
                {
                    newvalue = (byte)(nudSparkTrim.Value + 0x100);
                }
                else
                {
                    newvalue = (byte)nudSparkTrim.Value;
                }
                //toolTip1.SetToolTip(nudSparkTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((newvalue - 0x80) * WM16MEV.csparktableconversion).ToString("F2") + " degrees BTDC");
                toolTip1.SetToolTip(nudSparkTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((int)nudSparkTrim.Value * WM16MEV.csparktableconversion).ToString("F2") + " degrees BTDC");
                return;
            }

            loaded = false;
            wm16ev.DisplayDialog("Updating Global trims in ECU...", true);
            try
            {
                bool result = wm16ev.IncreaseDecreaseTDWGlobalSparkTrim(((int)nudSparkTrim.Value - currentsparktrim > 0));
                newvalue = wm16ev.TDWGlobalSparkTrimValue;
                if (newvalue > 0x80)
                {
                    nudSparkTrim.Value = -(0x100 - newvalue);
                }
                else
                {
                    nudSparkTrim.Value = newvalue;
                }
                //nudSparkTrim.Value = (wm16ev.TDWGlobalSparkTrimValue - (byte)0x80);
                if (result)
                {
                    if (!wm16ev.ReceivedByteResult || !wm16ev.TransmitByteResult)
                    {
                        nudSparkTrim.Enabled = false;
                    }
                }
                else
                {
                    nudSparkTrim.Enabled = false;
                }
            }
            finally
            {
                wm16ev.DisplayDialog("", false);
                loaded = true;
            }

            //newvalue = (byte)(nudSparkTrim.Value + 0x80);
            //toolTip1.SetToolTip(nudSparkTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((newvalue - 0x80) * WM16MEV.csparktableconversion).ToString("F2") + " degrees BTDC");
            toolTip1.SetToolTip(nudSparkTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((int)nudSparkTrim.Value * WM16MEV.csparktableconversion).ToString("F2") + " degrees BTDC");

            currentsparktrim = (int)nudSparkTrim.Value;
        }
        #endregion

        #region cbFuelEnabled_CheckedChanged
        private void cbFuelEnabled_CheckedChanged(object sender, EventArgs e)
        {
            if (!loaded)
            {
                return;
            }

            loaded = false;
            wm16ev.DisplayDialog("Updating Global trims in ECU...", true);
            try
            {
                bool result = wm16ev.EnableDisableTDWGlobalFuelTrim(cbFuelEnabled.Checked);
                cbFuelEnabled.Checked = wm16ev.TDWGlobalTrims[WM16MEV.ctdwglobaltrims_fuel_enabled];
                if (result)
                {
                    if (!wm16ev.ReceivedByteResult || !wm16ev.TransmitByteResult)
                    {
                        cbFuelEnabled.Enabled = false;
                    }
                }
                else
                {
                    cbFuelEnabled.Enabled = false;
                }
            }
            finally
            {
                wm16ev.DisplayDialog("", false);
                loaded = true;
            }
        }
        #endregion

        #region cbVerticalCylinderEnabled_CheckedChanged
        private void cbVerticalCylinderEnabled_CheckedChanged(object sender, EventArgs e)
        {
            if (!loaded)
            {
                return;
            }

            loaded = false;
            wm16ev.DisplayDialog("Updating Global trims in ECU...", true);
            try
            {
                bool result = wm16ev.EnableDisableTDWGlobalVertCylTrim(cbVerticalCylinderEnabled.Checked);
                cbVerticalCylinderEnabled.Checked = wm16ev.TDWGlobalTrims[WM16MEV.ctdwglobaltrims_vertcyl_enabled];
                if (result)
                {
                    if (!wm16ev.ReceivedByteResult || !wm16ev.TransmitByteResult)
                    {
                        cbVerticalCylinderEnabled.Enabled = false;
                    }
                }
                else
                {
                    cbVerticalCylinderEnabled.Enabled = false;
                }
            }
            finally
            {
                wm16ev.DisplayDialog("", false);
                loaded = true;
            }
        }
        #endregion

        #region cbSparkEnabled_CheckedChanged
        private void cbSparkEnabled_CheckedChanged(object sender, EventArgs e)
        {
            if (!loaded)
            {
                return;
            }

            loaded = false;
            wm16ev.DisplayDialog("Updating Global trims in ECU...", true);
            try
            {
                bool result = wm16ev.EnableDisableTDWGlobalSparkTrim(cbSparkEnabled.Checked);
                cbSparkEnabled.Checked = wm16ev.TDWGlobalTrims[WM16MEV.ctdwglobaltrims_spark_enabled];
                if (result)
                {
                    if (!wm16ev.ReceivedByteResult || !wm16ev.TransmitByteResult)
                    {
                        cbSparkEnabled.Enabled = false;
                    }
                }
                else
                {
                    cbSparkEnabled.Enabled = false;
                }
            }
            finally
            {
                wm16ev.DisplayDialog("", false);
                loaded = true;
            }
        }
        #endregion

        #region btnFuelVertCylSparkDisableAll_Click
        private void btnFuelVertCylSparkDisableAll_Click(object sender, EventArgs e)
        {
            cbFuelEnabled.Checked = false;
            cbVerticalCylinderEnabled.Checked = false;
            cbSparkEnabled.Checked = false;
        }
        #endregion

        #region btnFuelVertCylSparkEnableAll_Click
        private void btnFuelVertCylSparkEnableAll_Click(object sender, EventArgs e)
        {
            cbFuelEnabled.Checked = true;
            cbVerticalCylinderEnabled.Checked = true;
            cbSparkEnabled.Checked = true;
        }
        #endregion

        #region btnFuelReset_Click
        private void btnFuelReset_Click(object sender, EventArgs e)
        {
            if (!loaded)
            {
                return;
            }

            byte newvalue = 0;

            loaded = false;
            wm16ev.DisplayDialog("Updating Global trims in ECU...", true);
            try
            {
                bool result = wm16ev.ResetTDWGlobalFuelTrim();
                newvalue = wm16ev.TDWGlobalFuelTrimValue;
                if (newvalue > 0x80)
                {
                    nudFuelTrim.Value = -(0x100 - newvalue);
                }
                else
                {
                    nudFuelTrim.Value = newvalue;
                }
                if (result)
                {
                    if (!wm16ev.ReceivedByteResult || !wm16ev.TransmitByteResult)
                    {
                        nudFuelTrim.Enabled = false;
                    }
                }
                else
                {
                    nudFuelTrim.Enabled = false;
                }
            }
            finally
            {
                wm16ev.DisplayDialog("", false);
                loaded = true;
            }

            //toolTip1.SetToolTip(nudFuelTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((newvalue - 0x80) * WM16MEV.cfueltableconversion).ToString("F2") + "mS");
            toolTip1.SetToolTip(nudFuelTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((int)nudFuelTrim.Value * WM16MEV.cfueltableconversion).ToString("F2") + "mS");

            currentfueltrim = (int)nudFuelTrim.Value;
        }
        #endregion

        #region btnVertCylinderReset_Click
        private void btnVertCylinderReset_Click(object sender, EventArgs e)
        {
            if (!loaded)
            {
                return;
            }

            byte newvalue = 0;

            loaded = false;
            wm16ev.DisplayDialog("Updating Global trims in ECU...", true);
            try
            {
                bool result = wm16ev.ResetTDWGlobalVertCylTrim();
                newvalue = wm16ev.TDWGlobalVertCylTrimValue;
                if (newvalue > 0x80)
                {
                    nudVerticalCylinderTrim.Value = -(0x100 - newvalue);
                }
                else
                {
                    nudVerticalCylinderTrim.Value = newvalue;
                }
                if (result)
                {
                    if (!wm16ev.ReceivedByteResult || !wm16ev.TransmitByteResult)
                    {
                        nudVerticalCylinderTrim.Enabled = false;
                    }
                }
                else
                {
                    nudVerticalCylinderTrim.Enabled = false;
                }
            }
            finally
            {
                wm16ev.DisplayDialog("", false);
                loaded = true;
            }

            //toolTip1.SetToolTip(nudFuelTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((newvalue - 0x80) * WM16MEV.cfueltableconversion).ToString("F2") + "mS");
            toolTip1.SetToolTip(nudVerticalCylinderTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((int)nudVerticalCylinderTrim.Value * WM16MEV.cfueltableconversion).ToString("F2") + "mS");

            currentvertcyltrim = (int)nudVerticalCylinderTrim.Value;
        }
        #endregion

        #region btnSparkReset_Click
        private void btnSparkReset_Click(object sender, EventArgs e)
        {
            if (!loaded)
            {
                return;
            }

            byte newvalue = 0;

            loaded = false;
            wm16ev.DisplayDialog("Updating Global trims in ECU...", true);
            try
            {
                bool result = wm16ev.ResetTDWGlobalSparkTrim();
                newvalue = wm16ev.TDWGlobalSparkTrimValue;
                if (newvalue > 0x80)
                {
                    nudSparkTrim.Value = -(0x100 - newvalue);
                }
                else
                {
                    nudSparkTrim.Value = newvalue;
                }
                if (result)
                {
                    if (!wm16ev.ReceivedByteResult || !wm16ev.TransmitByteResult)
                    {
                        nudSparkTrim.Enabled = false;
                    }
                }
                else
                {
                    nudSparkTrim.Enabled = false;
                }
            }
            finally
            {
                wm16ev.DisplayDialog("", false);
                loaded = true;
            }

            //toolTip1.SetToolTip(nudFuelTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((newvalue - 0x80) * WM16MEV.cfueltableconversion).ToString("F2") + "mS");
            toolTip1.SetToolTip(nudSparkTrim, "0x" + newvalue.ToString("X2") + "\r\n" + ((int)nudSparkTrim.Value * WM16MEV.csparktableconversion).ToString("F2") + "mS");

            currentsparktrim = (int)nudSparkTrim.Value;
        }
        #endregion

        #region btnFuelVertCylSparkResetAll_Click
        private void btnFuelVertCylSparkResetAll_Click(object sender, EventArgs e)
        {
            btnFuelReset_Click(null, null);
            btnVertCylinderReset_Click(null, null);
            btnSparkReset_Click(null, null);
        }
        #endregion
    }
}
