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 OpenFileUITypeEditor : UITypeEditor
{
	private const string TEST_TREE_DIALOG_FILTER = "QSPR Compressed xtt files (*.cxtt)|*.cxtt|QSPR xtt files (*.xtt)|*.xtt|All files (*.*)|*.*";

	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;
		}
		OpenFileDialog openFileDialog = new OpenFileDialog();
		openFileDialog.CheckFileExists = true;
		openFileDialog.FilterIndex = 1;
		openFileDialog.RestoreDirectory = true;
		openFileDialog.Title = "Select the file for the property";
		if (value != null && value is string)
		{
			string path = value.ToString();
			if (Path.IsPathRooted(path))
			{
				openFileDialog.InitialDirectory = Path.GetDirectoryName(path);
			}
			else
			{
				openFileDialog.InitialDirectory = Environment.CurrentDirectory;
			}
			if (context.PropertyDescriptor.Name == "LinkLocation")
			{
				openFileDialog.Filter = "QSPR Compressed xtt files (*.cxtt)|*.cxtt|QSPR xtt files (*.xtt)|*.xtt|All files (*.*)|*.*";
			}
			else if (context.PropertyDescriptor.Name.Contains("Excel"))
			{
				openFileDialog.Filter = "Dynamic Limit Files (*.xls;*.xlsx;*.xlsm)|*.xls;*.xlsx;*.xlsm";
			}
			else
			{
				openFileDialog.Filter = "Xml files (*.Xml)|*.Xml|All files (*.*)|*.*";
			}
		}
		if (openFileDialog.ShowDialog() == DialogResult.OK)
		{
			return openFileDialog.FileName;
		}
		return base.EditValue(context, provider, value);
	}
}
