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.Metadata;

public class MetadataUITypeEditor : 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;
		MetadataDialog metadataDialog = new MetadataDialog(owner);
		if (owner != null)
		{
			if (owner.Properties.Metadata != null)
			{
				metadataDialog.Metadata = owner.Properties.Metadata.Clone();
			}
			if (metadataDialog.ShowDialog(WpfToWin32Window.GetCurrentWin32Window()) == DialogResult.OK)
			{
				owner.Properties.Metadata = metadataDialog.Metadata;
				if (owner.Properties.Metadata != null)
				{
					return "True";
				}
				return "False";
			}
		}
		return base.EditValue(context, provider, value);
	}
}
