using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Xml.Serialization;

namespace QC.SmartMatchApp;

[Serializable]
public class TrialData
{
	private string name;

	private List<string> refDesignators;

	private List<bool> useCustomKitValues;

	private List<Trial> trials;

	public string Name
	{
		get
		{
			return name;
		}
		set
		{
			name = value;
		}
	}

	public List<string> RefDesignators
	{
		get
		{
			return refDesignators;
		}
		set
		{
			refDesignators = value;
		}
	}

	public List<bool> UseCustomKitValues
	{
		get
		{
			return useCustomKitValues;
		}
		set
		{
			useCustomKitValues = value;
		}
	}

	public List<Trial> Trials
	{
		get
		{
			return trials;
		}
		set
		{
			trials = value;
		}
	}

	public TrialData()
	{
		name = "Antenna";
		refDesignators = new List<string>();
		useCustomKitValues = new List<bool>();
		trials = new List<Trial>();
	}

	public TrialData(string bandClassName = null)
	{
		if (bandClassName == null)
		{
			name = "Antenna";
		}
		else
		{
			name = bandClassName;
		}
		refDesignators = new List<string>();
		useCustomKitValues = new List<bool>();
		trials = new List<Trial>();
	}

	public TrialData(DataTable dt, string bandClassName = null)
	{
		if (bandClassName == null)
		{
			name = "Antenna";
		}
		else
		{
			name = bandClassName;
		}
		refDesignators = new List<string>();
		useCustomKitValues = new List<bool>();
		trials = new List<Trial>();
		if (dt == null)
		{
			return;
		}
		for (int i = 0; i < dt.Rows.Count; i++)
		{
			RefDesignators.Add(dt.Rows[i][0].ToString());
			UseCustomKitValues.Add((bool)dt.Rows[i][1]);
		}
		int num = 2;
		while (num < dt.Columns.Count)
		{
			Trial trial = new Trial();
			for (int j = 0; j < dt.Rows.Count; j++)
			{
				trial.Value.Add((double)dt.Rows[j][num]);
				trial.Units.Add((TrialUnits)dt.Rows[j][num + 1]);
				trial.Topology.Add((TrialTopology)dt.Rows[j][num + 2]);
			}
			num += 3;
			Trials.Add(trial);
		}
	}

	private void LoadDummyData()
	{
		refDesignators.Add("L1");
		refDesignators.Add("C1");
		refDesignators.Add("L2");
		useCustomKitValues.Add(item: false);
		useCustomKitValues.Add(item: false);
		useCustomKitValues.Add(item: false);
		Random random = new Random();
		for (int i = 0; i <= refDesignators.Count; i++)
		{
			Trial trial = new Trial();
			for (int j = 0; j < refDesignators.Count; j++)
			{
				trial.Value.Add(random.Next(1, 10));
				trial.Units.Add((TrialUnits)random.Next(0, 1));
				trial.Topology.Add((TrialTopology)random.Next(0, 3));
			}
			trials.Add(trial);
		}
	}

	public void SaveData(string fileName)
	{
		XmlSerializer xmlSerializer = new XmlSerializer(typeof(TrialData));
		TextWriter textWriter = new StreamWriter(fileName);
		xmlSerializer.Serialize(textWriter, this);
		textWriter.Close();
	}

	public DataTable LoadData(string filename)
	{
		TrialData trialData = new TrialData();
		FileStream fileStream = null;
		XmlSerializer xmlSerializer = new XmlSerializer(typeof(TrialData));
		fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
		if (fileStream != null)
		{
			trialData = (TrialData)xmlSerializer.Deserialize(fileStream);
			Name = trialData.Name;
			Trials = trialData.Trials;
			RefDesignators = trialData.RefDesignators;
			UseCustomKitValues = trialData.UseCustomKitValues;
			fileStream.Close();
		}
		DataTable dataTable = new DataTable();
		dataTable.Columns.Add("Ref Designator");
		DataColumn dataColumn = dataTable.Columns.Add("Use Custom Kit Values?", typeof(bool));
		dataColumn.AllowDBNull = false;
		for (int i = 0; i < refDesignators.Count; i++)
		{
			dataTable.Rows.Add(refDesignators[i], false);
		}
		for (int j = 0; j < useCustomKitValues.Count; j++)
		{
			if (dataTable.Rows.Count > j)
			{
				dataTable.Rows[j][1] = useCustomKitValues[j];
			}
		}
		for (int k = 1; k <= trials.Count; k++)
		{
			dataTable.Columns.Add("Trial" + k + " Value", typeof(double));
			dataTable.Columns.Add("Trial" + k + " Units", typeof(TrialUnits));
			dataTable.Columns.Add("Trial" + k + " Topology", typeof(TrialTopology));
		}
		for (int l = 0; l < refDesignators.Count; l++)
		{
			for (int m = 0; m < trials.Count; m++)
			{
				dataTable.Rows[l][m * 3 + 2] = trials[m].Value[l];
				dataTable.Rows[l][m * 3 + 3] = trials[m].Units[l];
				dataTable.Rows[l][m * 3 + 4] = trials[m].Topology[l];
			}
		}
		return dataTable;
	}
}
