using QC.QsprTestObjects.Loops;

namespace QC.QTMApplication;

public class TransformValues
{
	public int GetDisplayHint(int displayHint, string constraints, string dataType, string dataValue)
	{
		HandleDipslayHint(displayHint, constraints, dataType, dataValue, out var concludedDisplayHint, out var _);
		return concludedDisplayHint;
	}

	public string GetDataValue(int displayHint, string constraints, string dataType, string dataValue)
	{
		HandleDipslayHint(displayHint, constraints, dataType, dataValue, out var _, out var concludedDataValue);
		return concludedDataValue;
	}

	private void HandleDipslayHint(int displayHint, string constraints, string dataType, string dataValue, out int concludedDisplayHint, out string concludedDataValue)
	{
		concludedDisplayHint = displayHint;
		concludedDataValue = dataValue;
		if ((displayHint & 3) == 3)
		{
			return;
		}
		if ((displayHint & 2) == 2)
		{
			if (displayHint == 2)
			{
				if (constraints.Trim().Length == 0)
				{
					constraints = "Yes;No";
				}
				if (constraints.StartsWith(dataValue))
				{
					concludedDataValue = "1";
				}
				else
				{
					concludedDataValue = "0";
				}
				concludedDisplayHint = 4096;
			}
			else if ((displayHint & 0x3000) == 12288)
			{
				if (constraints.Trim().Length == 0)
				{
					constraints = ("True;False".Contains(dataValue) ? "True;False" : ("Yes;No".Contains(dataValue) ? "Yes;No" : ((!"On;Off".Contains(dataValue)) ? "Yes;No" : "On;Off")));
				}
				if (constraints.StartsWith(dataValue))
				{
					concludedDataValue = "1";
				}
				else
				{
					concludedDataValue = "0";
				}
				concludedDisplayHint = 12288;
			}
			else if ((displayHint & 0x1000) == 4096)
			{
				if (constraints.Trim().Length == 0)
				{
					constraints = ("True;False".Contains(dataValue) ? "True;False" : ("Yes;No".Contains(dataValue) ? "Yes;No" : ((!"On;Off".Contains(dataValue)) ? "Yes;No" : "On;Off")));
				}
				if (constraints.StartsWith(dataValue))
				{
					concludedDataValue = "1";
				}
				else
				{
					concludedDataValue = "0";
				}
				concludedDisplayHint = 4096;
			}
			else if ((displayHint & 0x2000) == 8192)
			{
				if (dataType == "boolean")
				{
					concludedDisplayHint = 2;
					if (dataValue.StartsWith("On"))
					{
						concludedDataValue = "1";
					}
					else
					{
						concludedDataValue = "0";
					}
				}
				else
				{
					concludedDisplayHint = 8192;
				}
			}
			else if ((displayHint & 0x100) == 256)
			{
				if (constraints.Trim().Length == 0)
				{
					constraints = "Yes;No";
				}
				if (constraints.StartsWith(dataValue))
				{
					concludedDataValue = "1";
				}
				else
				{
					concludedDataValue = "0";
				}
			}
			else
			{
				if (constraints.Trim().Length == 0)
				{
					constraints = "Yes;No";
				}
				if (constraints.StartsWith(dataValue))
				{
					concludedDataValue = "1";
				}
				else
				{
					concludedDataValue = "0";
				}
			}
		}
		else if (constraints.Trim().Length == 0)
		{
			if (dataValue == "Yes")
			{
				concludedDataValue = "1";
			}
			else if (dataValue == "No")
			{
				concludedDataValue = "0";
			}
			else
			{
				concludedDataValue = dataValue;
			}
		}
		else if ((displayHint & 0x1000) == 4096)
		{
			if (dataValue.StartsWith("Yes"))
			{
				concludedDataValue = "1";
			}
			else
			{
				concludedDataValue = "0";
			}
		}
		else if ((displayHint & 0x2000) == 8192)
		{
			if (dataValue.StartsWith("On"))
			{
				concludedDataValue = "1";
			}
			else
			{
				concludedDataValue = "0";
			}
		}
		else if ((displayHint & 0x100) == 256)
		{
			if (constraints.Trim().Length == 0)
			{
				if (dataType == "boolean")
				{
					concludedDisplayHint = 2;
					if (dataValue.StartsWith("Yes"))
					{
						concludedDataValue = "1";
					}
					else
					{
						concludedDataValue = "0";
					}
				}
				else
				{
					concludedDisplayHint = 256;
				}
			}
			else if (dataType == "integer")
			{
				concludedDisplayHint = 8;
			}
			else if (dataType == "boolean")
			{
				concludedDisplayHint = 2;
				if (constraints.StartsWith(dataValue))
				{
					concludedDataValue = "0";
				}
				else
				{
					concludedDataValue = "1";
				}
			}
			else
			{
				concludedDisplayHint = 256;
			}
		}
		else if ((displayHint & 8) == 8)
		{
			concludedDisplayHint = 8;
		}
		else if (0 == 0 && constraints.Trim().Length > 0)
		{
			if (dataType == "integer")
			{
				concludedDisplayHint = 8;
			}
			else if (dataType == "boolean")
			{
				concludedDisplayHint = 2;
			}
			else
			{
				concludedDisplayHint = 256;
			}
		}
	}

	public string GetLoopValueForLoopByParam(string loopValue)
	{
		loopValue = loopValue.ToUpper().Replace(" ", "");
		LoopTypes assignedLoopType;
		if (loopValue.ToUpper().Contains("SSS"))
		{
			return LoopCondition.ParseSSSValue(loopValue, out assignedLoopType);
		}
		return loopValue;
	}

	public string GetLoopTypeForLoopByParam(string loopValue)
	{
		loopValue = loopValue.ToUpper().Replace(" ", "");
		if (loopValue.ToUpper().Contains("SSS"))
		{
			LoopCondition.ParseSSSValue(loopValue, out var assignedLoopType);
			return assignedLoopType.ToString();
		}
		return LoopTypes.LIST.ToString();
	}
}
