using System.Data;
using System.Linq;

namespace QC.SmartMatchApp;

public class KitValuesConverter
{
	private const string CAP_TABLE_NAME = "CapacitorTable";

	private const string IND_TABLE_NAME = "InductorTable";

	private const int COL_NUM = 0;

	private KitValues kitValues = new KitValues();

	private DataTable kitValCapDataTable = new DataTable
	{
		TableName = "CapacitorTable"
	};

	private DataTable kitValIndDataTable = new DataTable
	{
		TableName = "InductorTable"
	};

	private bool ignoreTableChanges;

	public DataTable KitValCapDataTable
	{
		get
		{
			return kitValCapDataTable;
		}
		set
		{
			kitValCapDataTable.RowChanged -= kitValCapTable_RowChanged;
			kitValCapDataTable.RowDeleting -= kitValCapTable_RowDeleting;
			kitValCapDataTable = value;
			kitValCapDataTable.TableName = "CapacitorTable";
			kitValCapDataTable.RowChanged += kitValCapTable_RowChanged;
			kitValCapDataTable.RowDeleting += kitValCapTable_RowDeleting;
		}
	}

	public DataTable KitValIndDataTable
	{
		get
		{
			return kitValIndDataTable;
		}
		set
		{
			kitValIndDataTable.RowChanged -= kitValIndTable_RowChanged;
			kitValIndDataTable.RowDeleting -= kitValIndTable_RowDeleting;
			kitValIndDataTable = value;
			kitValIndDataTable.TableName = "InductorTable";
			kitValIndDataTable.RowChanged += kitValIndTable_RowChanged;
			kitValIndDataTable.RowDeleting += kitValIndTable_RowDeleting;
		}
	}

	public KitValues KitValues
	{
		get
		{
			return kitValues;
		}
		set
		{
			kitValues = value;
		}
	}

	public KitValuesConverter(string refDesName = "", KitValues kitVals = null)
	{
		if (kitVals == null)
		{
			kitValues = new KitValues();
		}
		else
		{
			kitValues = kitVals;
		}
		kitValues.RefDesignator = refDesName;
		kitValCapDataTable = new DataTable
		{
			TableName = "CapacitorTable"
		};
		kitValIndDataTable = new DataTable
		{
			TableName = "InductorTable"
		};
		kitValCapDataTable.RowChanged += kitValCapTable_RowChanged;
		kitValCapDataTable.RowDeleting += kitValCapTable_RowDeleting;
		kitValIndDataTable.RowChanged += kitValIndTable_RowChanged;
		kitValIndDataTable.RowDeleting += kitValIndTable_RowDeleting;
		ReloadKitValueTables();
	}

	public void ReloadKitValueTables()
	{
		ignoreTableChanges = true;
		kitValCapDataTable.Clear();
		kitValCapDataTable.Rows.Clear();
		kitValCapDataTable.Columns.Clear();
		kitValIndDataTable.Clear();
		kitValIndDataTable.Rows.Clear();
		kitValIndDataTable.Columns.Clear();
		kitValCapDataTable.Columns.Add("Capacitor Values", typeof(double));
		for (int i = 0; i < kitValues.CapacitorValues.Count; i++)
		{
			if (kitValCapDataTable.Rows.Count <= i)
			{
				kitValCapDataTable.Rows.Add(kitValues.CapacitorValues[i]);
			}
			else
			{
				kitValCapDataTable.Rows[i][0] = kitValues.CapacitorValues[i];
			}
		}
		kitValIndDataTable.Columns.Add("Inductor Values", typeof(double));
		for (int j = 0; j < kitValues.InductorValues.Count; j++)
		{
			if (kitValIndDataTable.Rows.Count <= j)
			{
				kitValIndDataTable.Rows.Add(kitValues.InductorValues[j]);
			}
			else
			{
				kitValIndDataTable.Rows[j][0] = kitValues.InductorValues[j];
			}
		}
		ignoreTableChanges = false;
	}

	public KitValuesConverter(KitValuesConverter kitValuesConverter)
	{
		KitValCapDataTable = kitValuesConverter.kitValCapDataTable.Copy();
		KitValIndDataTable = kitValuesConverter.kitValIndDataTable.Copy();
		KitValues = new KitValues
		{
			RefDesignator = kitValuesConverter.KitValues.RefDesignator.Clone().ToString(),
			CapacitorValues = kitValuesConverter.KitValues.CapacitorValues.ToList(),
			InductorValues = kitValuesConverter.kitValues.InductorValues.ToList()
		};
	}

	private void kitValCapTable_RowDeleting(object sender, DataRowChangeEventArgs e)
	{
		HandleDeletedRow(isCapTable: true, e);
	}

	private void kitValCapTable_RowChanged(object sender, DataRowChangeEventArgs e)
	{
		HandleRowChanged(isCapTable: true, e);
	}

	private void kitValIndTable_RowDeleting(object sender, DataRowChangeEventArgs e)
	{
		HandleDeletedRow(isCapTable: false, e);
	}

	private void kitValIndTable_RowChanged(object sender, DataRowChangeEventArgs e)
	{
		HandleRowChanged(isCapTable: false, e);
	}

	private void HandleDeletedRow(bool isCapTable, DataRowChangeEventArgs e)
	{
		if (!ignoreTableChanges)
		{
			int index = e.Row.Table.Rows.IndexOf(e.Row);
			if (isCapTable)
			{
				kitValues.CapacitorValues.RemoveAt(index);
			}
			else
			{
				KitValues.InductorValues.RemoveAt(index);
			}
		}
	}

	private void HandleRowChanged(bool isCapTable, DataRowChangeEventArgs e)
	{
		if (ignoreTableChanges || !(e.Row[0] is double))
		{
			return;
		}
		int num = e.Row.Table.Rows.IndexOf(e.Row);
		if (isCapTable)
		{
			if (kitValues.CapacitorValues.Count <= num)
			{
				kitValues.CapacitorValues.Add((double)e.Row[0]);
			}
			else
			{
				kitValues.CapacitorValues[num] = (double)e.Row[0];
			}
		}
		else if (kitValues.InductorValues.Count <= num)
		{
			kitValues.InductorValues.Add((double)e.Row[0]);
		}
		else
		{
			kitValues.InductorValues[num] = (double)e.Row[0];
		}
	}
}
