using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.IO;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace QC.QsprTestObjects.Tests.Base;

public class OpenDirectoryUITypeEditor : 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;
		}
		FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
		if (value != null && value is string)
		{
			string text = value.ToString();
			if (Directory.Exists(text))
			{
				folderBrowserDialog.SelectedPath = text;
			}
		}
		if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
		{
			return folderBrowserDialog.SelectedPath;
		}
		return base.EditValue(context, provider, value);
	}
}
