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 DisableTrims : Form
    {
        #region Variables
        private WM16MEV wm16ev = null;
        private bool loaded = false;
        #endregion

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

            wm16ev = wm16evpassed;

            BitArray disabletrims = wm16ev.TDWDisableTrims;

            cbFuelAirTempEnabled.Checked = disabletrims[WM16MEV.ctdwdisabletrims_fuel_airtemp_enabled];
            cbFuelWaterTempEnabled.Checked = disabletrims[WM16MEV.ctdwdisabletrims_fuel_watertemp_enabled];
            cbFuelAltitudeEnabled.Checked = disabletrims[WM16MEV.ctdwdisabletrims_fuel_altitude_enabled];

            gbFuel.Enabled = disabletrims[WM16MEV.ctdwdisabletrims_enabled];
            if (!gbFuel.Enabled)
            {
                MessageBox.Show("Disable trims has not been enabled. To enable turn ignition switch to off, turn key to off, wait till ECU relay drops out, turn key to on, open throttle to wide open, turn ignition switch to on, wait 5 seconds, release throttle", "Disable trims", MessageBoxButtons.OK);
            }

            loaded = true;
        }
        #endregion

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

            loaded = false;
            wm16ev.DisplayDialog("Updating disable trims in ECU...", true);
            try
            {
                bool result = wm16ev.EnableDisableTDWDisableFuelAirTempTrim(cbFuelAirTempEnabled.Checked);
                cbFuelAirTempEnabled.Checked = wm16ev.TDWDisableTrims[WM16MEV.ctdwdisabletrims_fuel_airtemp_enabled];
                if (result)
                {
                    if (!wm16ev.ReceivedByteResult || !wm16ev.TransmitByteResult)
                    {
                        cbFuelAirTempEnabled.Enabled = false;
                    }
                }
                else
                {
                    cbFuelAirTempEnabled.Enabled = false;
                }
            }
            finally
            {
                wm16ev.DisplayDialog("", false);
                loaded = true;
            }
        }
        #endregion

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

            loaded = false;
            wm16ev.DisplayDialog("Updating disable trims in ECU...", true);
            try
            {
                bool result = wm16ev.EnableDisableTDWDisableFuelWaterTempTrim(cbFuelWaterTempEnabled.Checked);
                cbFuelWaterTempEnabled.Checked = wm16ev.TDWDisableTrims[WM16MEV.ctdwdisabletrims_fuel_watertemp_enabled];
                if (result)
                {
                    if (!wm16ev.ReceivedByteResult || !wm16ev.TransmitByteResult)
                    {
                        cbFuelWaterTempEnabled.Enabled = false;
                    }
                }
                else
                {
                    cbFuelWaterTempEnabled.Enabled = false;
                }
            }
            finally
            {
                wm16ev.DisplayDialog("", false);
                loaded = true;
            }
        }
        #endregion

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

            loaded = false;
            wm16ev.DisplayDialog("Updating disable trims in ECU...", true);
            try
            {
                bool result = wm16ev.EnableDisableTDWDisableFuelAltitudeTrim(cbFuelAltitudeEnabled.Checked);
                cbFuelAltitudeEnabled.Checked = wm16ev.TDWDisableTrims[WM16MEV.ctdwdisabletrims_fuel_altitude_enabled];
                if (result)
                {
                    if (!wm16ev.ReceivedByteResult || !wm16ev.TransmitByteResult)
                    {
                        cbFuelAltitudeEnabled.Enabled = false;
                    }
                }
                else
                {
                    cbFuelAltitudeEnabled.Enabled = false;
                }
            }
            finally
            {
                wm16ev.DisplayDialog("", false);
                loaded = true;
            }
        }
        #endregion

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

        #region btnFuelDisableAll_Click
        private void btnFuelDisableAll_Click(object sender, EventArgs e)
        {
            cbFuelAirTempEnabled.Checked = false;
            cbFuelAltitudeEnabled.Checked = false;
            cbFuelWaterTempEnabled.Checked = false;
        }
        #endregion

        #region btnFuelEnableAll_Click
        private void btnFuelEnableAll_Click(object sender, EventArgs e)
        {
            cbFuelAirTempEnabled.Checked = true;
            cbFuelAltitudeEnabled.Checked = true;
            cbFuelWaterTempEnabled.Checked = true;
        }
        #endregion
    }
}
