using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using QC.QsprShared.Dialogs;
using QC.QsprTestObjects.Loops;
using QC.QsprTestObjects.Parameters;

namespace QC.QsprTestObjects.Tests.Base;

internal class LoopSetupUITypeEditor : 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;
		}
		if (baseTest.Properties.RetryCount != 0 && AppMessageDialog.Show("Retry count is set for this test. Opening Loop Setup will reset the retry count, but it will be available as Loop setup. \n  Do You want to continue? ", "Loop Setup", MessageBoxButtons.YesNo, DialogResult.Yes, MessageDialogType.Warning) == DialogResult.No)
		{
			return "Loop Setup";
		}
		FolderTest parent = baseTest.Parent;
		bool flag = false;
		FolderTest folderTest;
		if (baseTest is FolderTest)
		{
			folderTest = (FolderTest)baseTest;
		}
		else
		{
			flag = true;
			folderTest = new FolderTest("TempFolder");
			folderTest.AddDummyTest(baseTest);
			folderTest.Properties.Scenarios = baseTest.Properties.Scenarios;
			baseTest.Properties.RetryCount = 0;
		}
		LoopSetup loopSetup = new LoopSetup();
		loopSetup.LoopFolder = folderTest;
		if (loopSetup.ShowDialog() == DialogResult.OK && flag)
		{
			baseTest.Properties.Scenarios = folderTest.Properties.Scenarios;
		}
		baseTest.Parent = parent;
		if (folderTest.Properties.Scenarios != null)
		{
			return "Loop Setup";
		}
		return "No loop setup";
	}
}
