using System;
using System.Collections.Generic;
using System.Data;

namespace QC.SmartMatchApp;

public class TrialDataConverter
{
	private TrialData trialData = new TrialData();

	private DataTable trialDataTable = new DataTable();

	private bool ignoreTableChanges;

	public DataTable TrialDataTable
	{
		get
		{
			return trialDataTable;
		}
		set
		{
			trialDataTable.RowChanged -= trialDataTable_RowChanged;
			trialDataTable.RowDeleting -= trialDataTable_RowDeleting;
			trialDataTable = value;
			trialDataTable.RowChanged += trialDataTable_RowChanged;
			trialDataTable.RowDeleting += trialDataTable_RowDeleting;
		}
	}

	public TrialData TrialData
	{
		get
		{
			return trialData;
		}
		set
		{
			trialData = value;
		}
	}

	public TrialDataConverter()
	{
		trialDataTable.RowChanged += trialDataTable_RowChanged;
		trialDataTable.RowDeleting += trialDataTable_RowDeleting;
	}

	public TrialDataConverter(string bandClassName = null)
	{
		if (bandClassName != null)
		{
			trialData.Name = bandClassName;
		}
		trialDataTable.RowChanged += trialDataTable_RowChanged;
		trialDataTable.RowDeleting += trialDataTable_RowDeleting;
	}

	public TrialDataConverter(TrialData trialData)
	{
		TrialData = trialData;
		TrialDataTable = new DataTable();
		ignoreTableChanges = true;
		trialDataTable.Columns.Add("Ref Designator");
		trialDataTable.Columns.Add("Use Custom Kit Values?", typeof(bool));
		for (int i = 0; i < trialData.RefDesignators.Count; i++)
		{
			trialDataTable.Rows.Add(trialData.RefDesignators[i], false);
		}
		while (trialData.UseCustomKitValues.Count < trialData.RefDesignators.Count)
		{
			trialData.UseCustomKitValues.Add(item: false);
		}
		for (int j = 0; j < trialData.UseCustomKitValues.Count; j++)
		{
			if (trialDataTable.Rows.Count > j)
			{
				trialDataTable.Rows[j][1] = trialData.UseCustomKitValues[j];
			}
		}
		for (int k = 1; k <= trialData.Trials.Count; k++)
		{
			trialDataTable.Columns.Add("Trial" + k + " Value", typeof(double));
			trialDataTable.Columns.Add("Trial" + k + " Units", typeof(TrialUnits));
			trialDataTable.Columns.Add("Trial" + k + " Topology", typeof(TrialTopology));
		}
		for (int l = 0; l < trialData.RefDesignators.Count; l++)
		{
			for (int m = 0; m < trialData.Trials.Count; m++)
			{
				if (trialData.Trials[m].Value.Count > l)
				{
					if (trialData.Trials[m].Value[l].HasValue)
					{
						trialDataTable.Rows[l][m * 3 + 2] = trialData.Trials[m].Value[l];
					}
					else
					{
						trialDataTable.Rows[l][m * 3 + 2] = DBNull.Value;
					}
				}
				if (trialData.Trials[m].Units.Count > l)
				{
					if (trialData.Trials[m].Units[l].HasValue)
					{
						trialDataTable.Rows[l][m * 3 + 3] = trialData.Trials[m].Units[l];
					}
					else
					{
						trialDataTable.Rows[l][m * 3 + 3] = DBNull.Value;
					}
				}
				if (trialData.Trials[m].Topology.Count > l)
				{
					if (trialData.Trials[m].Topology[l].HasValue)
					{
						trialDataTable.Rows[l][m * 3 + 4] = trialData.Trials[m].Topology[l];
					}
					else
					{
						trialDataTable.Rows[l][m * 3 + 4] = DBNull.Value;
					}
				}
			}
		}
		ignoreTableChanges = false;
	}

	private void trialDataTable_RowDeleting(object sender, DataRowChangeEventArgs e)
	{
		if (ignoreTableChanges)
		{
			return;
		}
		string item = e.Row[0].ToString();
		if (trialData.RefDesignators.Contains(item))
		{
			int index = trialData.RefDesignators.IndexOf(item);
			trialData.RefDesignators.RemoveAt(index);
			trialData.UseCustomKitValues.RemoveAt(index);
			trialData.Trials.RemoveAt(trialData.Trials.Count - 1);
			foreach (Trial trial in trialData.Trials)
			{
				trial.Value.RemoveAt(index);
				trial.Units.RemoveAt(index);
				trial.Topology.RemoveAt(index);
			}
		}
		int num = trialDataTable.Columns.Count - 3;
		while (trialDataTable.Columns.Count > num)
		{
			trialDataTable.Columns.RemoveAt(num);
		}
	}

	private void AddRefDesignator(string refDes)
	{
		AddTrial();
		trialData.RefDesignators.Add(refDes);
		trialData.UseCustomKitValues.Add(item: false);
		foreach (Trial trial in trialData.Trials)
		{
			trial.Value.Add(null);
			trial.Units.Add(null);
			trial.Topology.Add(null);
		}
	}

	private void AddTrial()
	{
		Trial trial = new Trial();
		for (int i = 0; i < trialData.RefDesignators.Count; i++)
		{
			trial.Value.Add(null);
			trial.Units.Add(null);
			trial.Topology.Add(null);
		}
		trialData.Trials.Add(trial);
	}

	private void trialDataTable_RowChanged(object sender, DataRowChangeEventArgs e)
	{
		if (!ignoreTableChanges)
		{
			if (e.Action == DataRowAction.Add)
			{
				AddRefDesignator(e.Row[0].ToString());
				int num = trialData.RefDesignators.Count + 1;
				trialDataTable.Columns.Add(new DataColumn("Trial" + num + " Value", typeof(double)));
				trialDataTable.Columns.Add(new DataColumn("Trial" + num + " Units", typeof(TrialUnits)));
				trialDataTable.Columns.Add(new DataColumn("Trial" + num + " Topology", typeof(TrialTopology)));
			}
			int rowNum = e.Row.Table.Rows.IndexOf(e.Row);
			UpdateTrialData(rowNum);
		}
	}

	private void UpdateTrialData(int rowNum)
	{
		DataRow dataRow = trialDataTable.Rows[rowNum];
		trialData.RefDesignators[rowNum] = dataRow[0].ToString();
		bool result = false;
		bool.TryParse(dataRow[1].ToString(), out result);
		trialData.UseCustomKitValues[rowNum] = result;
		int num = 0;
		for (int i = 2; i < trialDataTable.Columns.Count; i += 3)
		{
			double result2 = 0.0;
			if (dataRow[i] != DBNull.Value && double.TryParse(dataRow[i].ToString(), out result2))
			{
				trialData.Trials[num].Value[rowNum] = result2;
			}
			else
			{
				trialData.Trials[num].Value[rowNum] = null;
			}
			if (dataRow[i + 1] != DBNull.Value)
			{
				trialData.Trials[num].Units[rowNum] = (TrialUnits)dataRow[i + 1];
			}
			else
			{
				trialData.Trials[num].Units[rowNum] = null;
			}
			if (dataRow[i + 2] != DBNull.Value)
			{
				trialData.Trials[num].Topology[rowNum] = (TrialTopology)dataRow[i + 2];
			}
			else
			{
				trialData.Trials[num].Topology[rowNum] = null;
			}
			num++;
		}
	}

	public List<string> ConvertToLoopRow(int trialNumber)
	{
		List<string> list = new List<string>();
		if (trialData.Trials.Count >= trialNumber)
		{
			list.Add(trialNumber.ToString());
			Trial trial = trialData.Trials[trialNumber];
			string text = "";
			string text2 = "";
			for (int i = 0; i < trial.Value.Count; i++)
			{
				TrialUnits? trialUnits = trial.Units[i];
				if (trialUnits.GetValueOrDefault() == TrialUnits.pF && trialUnits.HasValue)
				{
					text = text + trial.Value[i] + "e-12;";
					text2 = text2 + trial.Topology[i].ToString() + "_C;";
				}
				else if (trial.Units[i] == TrialUnits.nH)
				{
					text = text + trial.Value[i] + "e-09;";
					text2 = text2 + trial.Topology[i].ToString() + "_L;";
				}
				else
				{
					text += ";";
					text2 += ";";
				}
			}
			list.Add(text);
			list.Add(text2);
		}
		return list;
	}

	public void LoadDefaultData(string bandClassName = null)
	{
		if (bandClassName != null)
		{
			trialData = new TrialData(bandClassName);
		}
		else
		{
			trialData = new TrialData();
		}
		trialDataTable = new DataTable();
		trialData.RefDesignators.Add(null);
		trialData.UseCustomKitValues.Add(item: false);
		trialData.Trials.Add(new Trial
		{
			Value = new List<double?> { null },
			Units = new List<TrialUnits?> { null },
			Topology = new List<TrialTopology?> { null }
		});
		trialData.Trials.Add(new Trial
		{
			Value = new List<double?> { null },
			Units = new List<TrialUnits?> { null },
			Topology = new List<TrialTopology?> { null }
		});
		trialDataTable.Columns.Add("Ref Designator");
		trialDataTable.Columns.Add("Use Custom Kit Values?", typeof(bool));
		for (int i = 0; i < trialData.RefDesignators.Count; i++)
		{
			trialDataTable.Rows.Add(trialData.RefDesignators[i], false);
		}
		while (trialData.UseCustomKitValues.Count < trialData.RefDesignators.Count)
		{
			trialData.UseCustomKitValues.Add(item: false);
		}
		for (int j = 0; j < trialData.UseCustomKitValues.Count; j++)
		{
			if (trialDataTable.Rows.Count > j)
			{
				trialDataTable.Rows[j][1] = trialData.UseCustomKitValues[j];
			}
		}
		for (int k = 1; k <= trialData.Trials.Count; k++)
		{
			trialDataTable.Columns.Add("Trial" + k + " Value", typeof(double));
			trialDataTable.Columns.Add("Trial" + k + " Units", typeof(TrialUnits));
			trialDataTable.Columns.Add("Trial" + k + " Topology", typeof(TrialTopology));
		}
		trialDataTable.RowChanged += trialDataTable_RowChanged;
		trialDataTable.RowDeleting += trialDataTable_RowDeleting;
	}

	public List<string> GetCustomKitRefDesList()
	{
		List<string> list = new List<string>();
		for (int i = 0; i < TrialData.UseCustomKitValues.Count; i++)
		{
			bool result = false;
			bool.TryParse(TrialDataTable.Rows[i][1].ToString(), out result);
			if (result)
			{
				list.Add(TrialDataTable.Rows[i][0].ToString());
			}
		}
		return list;
	}

	public void BeginEditing()
	{
		ignoreTableChanges = true;
	}

	public void EndEditing()
	{
		ignoreTableChanges = false;
		for (int i = 0; i < trialDataTable.Rows.Count; i++)
		{
			UpdateTrialData(i);
		}
	}
}
