using System;
using System.Collections;
using System.Windows.Forms;
using System.Xml.Serialization;
using QC.QsprShared.Dialogs;
using QC.QsprShared.Trace;
using QC.QsprTestObjects.TestConditions;
using QC.QsprTestObjects.Tests.Base;
using QC.QTMInterface;

namespace QC.QsprTestObjects.Loops;

public class LoopScenarioValues
{
	private const string INPUT_PARAMETER = "INPUT";

	public const string DESCRIPTION_STRING_SEPARATOR = ";";

	private ArrayList loopConditions = new ArrayList();

	private ArrayList loopSelections = new ArrayList();

	private ArrayList loopDescriptions = new ArrayList();

	private ArrayList loopDontRunConds = new ArrayList();

	private ConditionMgr scenarioConditionMgr;

	private bool suppressWarnings;

	[XmlElement(Type = typeof(LoopCondition))]
	public ArrayList LoopConditions
	{
		get
		{
			return loopConditions;
		}
		set
		{
			loopConditions = value;
		}
	}

	[XmlIgnore]
	public ArrayList DontRunCondition
	{
		get
		{
			return loopDontRunConds;
		}
		set
		{
			loopDontRunConds = value;
		}
	}

	public string DontRunConditionAsString
	{
		get
		{
			string text = "";
			GetLoopInfo(out var noOfValues, out var loopType, out var originalValue);
			if (loopDontRunConds.Count > 0 && noOfValues < 20000)
			{
				for (int i = 0; i < loopDontRunConds.Count - 1; i++)
				{
					text = string.Concat(text, loopDontRunConds[i], ",");
				}
				text += loopDontRunConds[loopDontRunConds.Count - 1];
			}
			else if (noOfValues > 20000 && loopType == LoopTypes.STARTSTOPSTEP)
			{
				text = "LoopExceedMax:" + originalValue;
			}
			return text;
		}
		set
		{
			try
			{
				GetLoopInfo(out var noOfValues, out var loopType, out var _);
				if (value.Contains("LoopExceedMax") && loopType == LoopTypes.STARTSTOPSTEP)
				{
					for (int i = 0; i < noOfValues; i++)
					{
						loopDontRunConds.Add("");
					}
					return;
				}
				string[] array = value.Split(',');
				string[] array2 = array;
				foreach (string value2 in array2)
				{
					try
					{
						loopDontRunConds.Add(value2);
					}
					catch (Exception ex)
					{
						TraceMessage.Write(ToString(), "DontRunConditionAsString 1 " + ex.Message, TraceMessageType.Exception);
					}
				}
			}
			catch (Exception ex2)
			{
				TraceMessage.Write(ToString(), "DontRunConditionAsString 2 " + ex2.Message, TraceMessageType.Exception);
			}
		}
	}

	[XmlIgnore]
	public ArrayList ConditionCheck
	{
		get
		{
			return loopSelections;
		}
		set
		{
			loopSelections = value;
		}
	}

	public string ConditionCheckAsString
	{
		get
		{
			string text = "";
			GetLoopInfo(out var noOfValues, out var loopType, out var originalValue);
			if (loopSelections.Count > 0 && noOfValues < 20000)
			{
				for (int i = 0; i < loopSelections.Count - 1; i++)
				{
					text = string.Concat(text, loopSelections[i], ",");
				}
				text += loopSelections[loopSelections.Count - 1];
			}
			else if (noOfValues > 20000 && loopType == LoopTypes.STARTSTOPSTEP)
			{
				text = "LoopExceedMax:" + originalValue;
			}
			return text;
		}
		set
		{
			try
			{
				GetLoopInfo(out var noOfValues, out var loopType, out var _);
				if (value.Contains("LoopExceedMax") && loopType == LoopTypes.STARTSTOPSTEP)
				{
					for (int i = 0; i < noOfValues; i++)
					{
						loopSelections.Add(true);
					}
					return;
				}
				string[] array = value.Split(',');
				string[] array2 = array;
				foreach (string value2 in array2)
				{
					try
					{
						loopSelections.Add(bool.Parse(value2));
					}
					catch (Exception ex)
					{
						TraceMessage.Write(ToString(), "ConditionCheckAsString 1 " + ex.Message, TraceMessageType.Exception);
					}
				}
			}
			catch (Exception ex2)
			{
				TraceMessage.Write(ToString(), "ConditionCheckAsString 2 " + ex2.Message, TraceMessageType.Exception);
			}
		}
	}

	[XmlIgnore]
	public ArrayList Descriptions
	{
		get
		{
			return loopDescriptions;
		}
		set
		{
			loopDescriptions = value;
		}
	}

	public string DescriptionsAsString
	{
		get
		{
			string text = "";
			GetLoopInfo(out var noOfValues, out var loopType, out var originalValue);
			if (loopDescriptions.Count > 0 && noOfValues < 20000)
			{
				for (int i = 0; i < loopDescriptions.Count - 1; i++)
				{
					text = string.Concat(text, loopDescriptions[i], ";");
				}
				text += loopDescriptions[loopDescriptions.Count - 1];
			}
			else if (noOfValues > 20000 && loopType == LoopTypes.STARTSTOPSTEP)
			{
				text = "LoopExceedMax:" + originalValue;
			}
			return text;
		}
		set
		{
			try
			{
				GetLoopInfo(out var noOfValues, out var loopType, out var _);
				if (value.Contains("LoopExceedMax") && loopType == LoopTypes.STARTSTOPSTEP)
				{
					for (int i = 0; i < noOfValues; i++)
					{
						loopDescriptions.Add("");
					}
					return;
				}
				string[] array = value.Split(";".ToCharArray()[0]);
				string[] array2 = array;
				foreach (string value2 in array2)
				{
					try
					{
						loopDescriptions.Add(value2);
					}
					catch (Exception ex)
					{
						TraceMessage.Write(ToString(), "DescriptionsAsString 1 " + ex.Message, TraceMessageType.Exception);
					}
				}
			}
			catch (Exception ex2)
			{
				TraceMessage.Write(ToString(), "DescriptionsAsString 2 " + ex2.Message, TraceMessageType.Exception);
			}
		}
	}

	[XmlIgnore]
	public int Count => loopConditions.Count;

	public ConditionMgr Condition
	{
		get
		{
			return scenarioConditionMgr;
		}
		set
		{
			scenarioConditionMgr = value;
		}
	}

	public bool SuppressWarnings
	{
		get
		{
			return suppressWarnings;
		}
		set
		{
			suppressWarnings = value;
		}
	}

	public void Clear()
	{
		try
		{
			loopConditions.Clear();
			loopSelections.Clear();
			loopDontRunConds.Clear();
			loopDescriptions.Clear();
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "Clear " + ex.Message, TraceMessageType.Exception);
		}
	}

	public static void UpdateRetryCount(ref LoopScenarioValues loopSetup, int newCount)
	{
		int iIndex = -1;
		LoopCondition loopCondition = (LoopCondition)loopSetup.loopConditions[0];
		loopCondition.OriginalValue = newCount.ToString();
		loopSetup.DeleteLoopCondition(0);
		loopSetup.AddLoopCondition(loopCondition, ref iIndex);
	}

	public static LoopScenarioValues CreateRetyrLoop(string testname, string testindex, int byCountTimes, RootTest rootTest)
	{
		int iIndex = -1;
		LoopScenarioValues loopScenarioValues = new LoopScenarioValues();
		try
		{
			LoopCondition loopCondition = new LoopCondition();
			loopCondition.LoopName = "RetryCount";
			loopCondition.LoopOnType = LoopOnTypes.INPUTPARAM;
			loopCondition.IgnoreIntermediateResults = true;
			loopCondition.LoopType = LoopTypes.BYCOUNT;
			loopCondition.OriginalValue = byCountTimes.ToString();
			loopCondition.DataType = DataType.Int;
			loopScenarioValues.AddLoopCondition(loopCondition, ref iIndex);
			loopScenarioValues.ConditionCheckAsString = "True";
			loopScenarioValues.DescriptionsAsString = "";
			loopScenarioValues.DontRunCondition.Add("");
			BaseCondition cond = new ScenarioCondition("C1", ConditionType.TestResult, TestResult.Passed, testindex, rootTest);
			ConditionMgr conditionMgr = new ConditionMgr("C1");
			conditionMgr.AddCondition(cond);
			loopScenarioValues.scenarioConditionMgr = conditionMgr;
			return loopScenarioValues;
		}
		catch
		{
			return null;
		}
	}

	public LoopScenarioValues Clone()
	{
		LoopScenarioValues loopScenarioValues = new LoopScenarioValues();
		ArrayList arrayList = new ArrayList();
		foreach (LoopCondition loopCondition in loopConditions)
		{
			arrayList.Add(loopCondition.Clone());
		}
		loopScenarioValues.LoopConditions = arrayList;
		ArrayList arrayList2 = new ArrayList();
		foreach (bool item in ConditionCheck)
		{
			arrayList2.Add(item);
		}
		loopScenarioValues.ConditionCheck = arrayList2;
		ArrayList arrayList3 = new ArrayList();
		foreach (string loopDescription in loopDescriptions)
		{
			arrayList3.Add(loopDescription);
		}
		loopScenarioValues.loopDescriptions = arrayList3;
		ArrayList arrayList4 = new ArrayList();
		foreach (string loopDontRunCond in loopDontRunConds)
		{
			arrayList4.Add(loopDontRunCond);
		}
		loopScenarioValues.DontRunCondition = arrayList4;
		if (scenarioConditionMgr != null)
		{
			loopScenarioValues.Condition = scenarioConditionMgr.Clone();
		}
		loopScenarioValues.SuppressWarnings = SuppressWarnings;
		return loopScenarioValues;
	}

	public bool ValidateLoopParameter(LoopCondition loop)
	{
		try
		{
			for (int i = 0; i < loopConditions.Count; i++)
			{
				if (!(((LoopCondition)loopConditions[i]).TestIndex == loop.TestIndex) || !(((LoopCondition)loopConditions[i]).ParamName == loop.ParamName) || ((LoopCondition)loopConditions[i]).ArrayIndex != loop.ArrayIndex || !(((LoopCondition)loopConditions[i]).Array2DIndex == loop.Array2DIndex))
				{
					continue;
				}
				if (loop.LoopOnType == LoopOnTypes.INPUTPARAM)
				{
					if (((LoopCondition)loopConditions[i]).LoopName == loop.LoopName)
					{
						DialogResult dialogResult = AppMessageDialog.Show("Do you want to overwrite the current Loop Condition?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, DialogResult.Yes, MessageDialogType.Warning);
						if (dialogResult == DialogResult.Yes)
						{
							return true;
						}
						return false;
					}
					AppMessageDialog.Show("Loop on " + loop.TestName + " - " + loop.ParamName + " pair already exists.\n*Hints: \n1. Choose Different Parameter.\n2. Right Click on the column to change the Loop name", "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Error);
					return false;
				}
				if (((LoopCondition)loopConditions[i]).LoopName == loop.LoopName)
				{
					DialogResult dialogResult2 = AppMessageDialog.Show("Do you want to overwrite the current Limits?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, DialogResult.OK, MessageDialogType.Warning);
					if (dialogResult2 == DialogResult.Yes)
					{
						return true;
					}
					return false;
				}
				if ((((LoopCondition)loopConditions[i]).LoopName.EndsWith("UL") && loop.LoopName.EndsWith("UL")) || (((LoopCondition)loopConditions[i]).LoopName.EndsWith("LL") && loop.LoopName.EndsWith("LL")))
				{
					AppMessageDialog.Show("Loop on " + loop.TestName + " - " + loop.ParamName + " pair already exist.\n*Hints: \n1. Choose Different Parameter.\n2. Right Click on the column to change the Loop name", "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Error);
					return false;
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "ValidateLoopParameter " + ex.Message, TraceMessageType.Exception);
			return false;
		}
		return true;
	}

	public bool AddLoopCondition(LoopCondition loopCond, ref int iIndex)
	{
		try
		{
			for (int i = 0; i < loopConditions.Count; i++)
			{
				if (((LoopCondition)loopConditions[i]).LoopName.Trim().ToUpper() == loopCond.LoopName.Trim().ToUpper())
				{
					if (((LoopCondition)loopConditions[i]).TestIndex == loopCond.TestIndex && ((LoopCondition)loopConditions[i]).ParamName == loopCond.ParamName && ((LoopCondition)loopConditions[i]).ArrayIndex == loopCond.ArrayIndex)
					{
						loopConditions[i] = loopCond;
						iIndex = i;
						return false;
					}
					iIndex = -1;
					return false;
				}
			}
			iIndex = loopConditions.Add(loopCond);
			return true;
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "AddLoopCondition " + ex.Message, TraceMessageType.Exception);
			return false;
		}
	}

	public bool DeleteLoopCondition(int index)
	{
		try
		{
			loopConditions.RemoveAt(index);
			return true;
		}
		catch
		{
			return false;
		}
	}

	public bool MoveConditionLeft(int indexFrom)
	{
		try
		{
			loopConditions.Reverse(indexFrom - 1, 2);
			return true;
		}
		catch
		{
			return false;
		}
	}

	public bool MoveConditionRight(int indexFrom)
	{
		try
		{
			loopConditions.Reverse(indexFrom, 2);
			return true;
		}
		catch
		{
			return false;
		}
	}

	public bool ClearBottomEmptyRows()
	{
		int count = loopSelections.Count;
		if (count == 1)
		{
			return true;
		}
		ArrayList arrayList = new ArrayList();
		bool flag = false;
		for (int num = count - 1; num >= 0; num--)
		{
			foreach (LoopCondition loopCondition3 in LoopConditions)
			{
				try
				{
					if (num < loopCondition3.LoopValues.Count && loopCondition3.LoopValues[num].ToString() != "")
					{
						flag = true;
					}
				}
				catch
				{
				}
			}
			if (flag)
			{
				break;
			}
			arrayList.Add(num);
		}
		foreach (int item in arrayList)
		{
			foreach (LoopCondition loopCondition4 in LoopConditions)
			{
				try
				{
					if (loopCondition4.LoopType == LoopTypes.LIST && item < loopCondition4.LoopValues.Count)
					{
						loopCondition4.LoopValues.RemoveAt(item);
					}
				}
				catch
				{
				}
			}
			try
			{
				loopSelections.RemoveAt(item);
				loopDontRunConds.RemoveAt(item);
				loopDescriptions.RemoveAt(item);
			}
			catch
			{
			}
		}
		if (arrayList.Count > 0)
		{
			return false;
		}
		return true;
	}

	public ArrayList GetConditionRows(int TotalRows)
	{
		try
		{
			ArrayList arrayList = new ArrayList();
			if (loopSelections.Count == 1)
			{
				if ((bool)loopSelections[0])
				{
					arrayList.Add(true);
				}
				else
				{
					arrayList.Add(false);
				}
			}
			return arrayList;
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "GetConditionRows " + ex.Message, TraceMessageType.Exception);
		}
		return null;
	}

	private void GetLoopInfo(out int noOfValues, out LoopTypes loopType, out string originalValue)
	{
		noOfValues = 0;
		loopType = LoopTypes.LIST;
		originalValue = "";
		foreach (LoopCondition loopCondition in LoopConditions)
		{
			noOfValues += loopCondition.LoopValues.Count;
			loopType = loopCondition.LoopType;
			originalValue = loopCondition.OriginalValue;
		}
	}
}
