using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using QC.QsprShared.Dialogs;

namespace QC.QsprTestObjects.Parameters;

public class ParamCollectionPropertyDescriptor : PropertyDescriptor
{
	private const string P_VALUE = "Value ";

	private const string P_UL = "UL ";

	private const string P_LL = "LL ";

	private ParameterCollection collection;

	private int index = -1;

	public override AttributeCollection Attributes => new AttributeCollection((Attribute[])null);

	public override Type ComponentType => collection.GetType();

	public override string DisplayName
	{
		get
		{
			ParameterCls parameterCls = collection[index];
			ParameterCls parameter = collection.GetParameter(parameterCls.ParentName);
			string text = parameterCls.Name;
			while (parameter != null)
			{
				text = parameterCls.ParentName + "->" + text;
				parameter = collection.GetParameter(parameter.ParentName);
			}
			return text;
		}
	}

	public override string Description
	{
		get
		{
			ParameterCls parameterCls = collection[index];
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(parameterCls.Name);
			stringBuilder.Append(" ");
			if (parameterCls.NoOfElements > 1)
			{
				stringBuilder.Append("#Elts " + parameterCls.NoOfElements + " ");
			}
			stringBuilder.Append("Value " + parameterCls.Value + " ");
			stringBuilder.Append("UL " + parameterCls.UpperLimit + " ");
			stringBuilder.Append("LL " + parameterCls.LowerLimit + " ");
			stringBuilder.Append("Unit " + parameterCls.Unit + " ");
			stringBuilder.Append(string.Concat("Type ", parameterCls.Type, " "));
			stringBuilder.Append(string.Concat("DataType ", parameterCls.DataType, " "));
			return stringBuilder.ToString();
		}
	}

	public int ParentID { get; set; }

	public override bool IsReadOnly => false;

	public override string Name => "#" + index;

	public override Type PropertyType => collection[index].GetType();

	public ParamCollectionPropertyDescriptor(ParameterCollection coll, int idx)
		: base("#" + idx, null)
	{
		collection = coll;
		index = idx;
	}

	public override bool CanResetValue(object component)
	{
		return true;
	}

	public override object GetValue(object component)
	{
		return collection[index];
	}

	public override void ResetValue(object component)
	{
	}

	public override bool ShouldSerializeValue(object component)
	{
		return true;
	}

	public override void SetValue(object component, object value)
	{
		try
		{
			ParameterCls parameterCls = collection[index];
			string text = (string)value;
			text = text.Remove(0, "Value ".Length);
			int num = text.IndexOf("UL ");
			string text2 = text.Substring(0, num);
			text = text.Remove(0, num);
			string value2 = text2.Trim();
			text = text.Remove(0, "UL ".Length);
			num = text.IndexOf("LL ");
			text2 = text.Substring(0, num);
			text = text.Remove(0, num);
			string upperLimit = text2.Trim();
			text = text.Remove(0, "LL ".Length);
			string lowerLimit = text.Trim();
			parameterCls.Value = value2;
			parameterCls.LowerLimit = lowerLimit;
			parameterCls.UpperLimit = upperLimit;
		}
		catch (Exception ex)
		{
			AppMessageDialog.Show(" Error in values " + ex.Message, DialogResult.OK, MessageDialogType.Exception);
		}
	}
}
