using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using QC.QsprTestObjects.Tests.Base;
using QsprMiscShared.Utilities;

namespace QC.QsprTestObjects.TestConditions;

public class PostConditionUITypeEditor : UITypeEditor
{
	public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
	{
		return UITypeEditorEditStyle.Modal;
	}

	public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
	{
		IWindowsFormsEditorService windowsFormsEditorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
		if (windowsFormsEditorService == null)
		{
			return null;
		}
		TestProperties testProperties = (TestProperties)context.Instance;
		BaseTest owner = testProperties.Owner;
		PostConditionDialog postConditionDialog = new PostConditionDialog(owner);
		if (owner != null)
		{
			postConditionDialog.Parameters = owner.Properties.Parameters;
			if (owner.Properties.PostCondition != null)
			{
				postConditionDialog.PostConditionMgr = owner.Properties.PostCondition.Clone();
			}
			if (postConditionDialog.ShowDialog(WpfToWin32Window.GetCurrentWin32Window()) == DialogResult.OK)
			{
				owner.Properties.PostCondition = postConditionDialog.PostConditionMgr;
				if (!owner.Properties.PostCondition.Enabled)
				{
					return PostConditionActions.None.ToString();
				}
				return owner.Properties.PostCondition.Action.ToString();
			}
		}
		return base.EditValue(context, provider, value);
	}
}
