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

namespace QC.QsprTestObjects.TestConditions;

public class PreConditionUITypeEditor : 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;
		}
		BaseTest baseTest = null;
		if (context.Instance is TestProperties)
		{
			TestProperties testProperties = (TestProperties)context.Instance;
			baseTest = testProperties.Owner;
		}
		else
		{
			if (!(context.Instance is LinkedFolderPropView))
			{
				return null;
			}
			LinkedFolderPropView linkedFolderPropView = (LinkedFolderPropView)context.Instance;
			baseTest = linkedFolderPropView.Owner;
		}
		PreConditionDialog preConditionDialog = new PreConditionDialog(baseTest);
		if (baseTest != null)
		{
			if (baseTest.Properties.PreCondition != null)
			{
				preConditionDialog.PreConditionMgr = baseTest.Properties.PreCondition.Clone();
			}
			if (preConditionDialog.ShowDialog(WpfToWin32Window.GetCurrentWin32Window()) == DialogResult.OK)
			{
				baseTest.Properties.PreCondition = preConditionDialog.PreConditionMgr;
				if (baseTest.Properties.PreCondition != null)
				{
					return "True";
				}
				return "False";
			}
		}
		return base.EditValue(context, provider, value);
	}
}
