using System.Collections.Generic;
using System.Windows.Controls;
using QC.QsprTestObjects.Tests.Base;
using QC.QTMSecurity;

namespace QC.QTMApplication;

public class TestTreeEditor
{
	public TreeView TestTreeView { get; protected set; }

	public List<BaseTest> CutTests { get; protected set; }

	public event TestEditEventHandler BeforeTestNameEdit;

	public event TestEditEventHandler AfterTestNameEdit;

	public event TestEditEventHandler BeforeTestCut;

	public event TestEditEventHandler AfterTestCut;

	public event TestEditEventHandler BeforeTestCopy;

	public event TestEditEventHandler AfterTestCopy;

	public TestTreeEditor(TreeView testTreeView)
	{
		TestTreeView = testTreeView;
		CutTests = new List<BaseTest>();
	}

	public void BeginTestNameEdit(BaseTest baseTest)
	{
		if (QSecurity.LockedQGroup || !QSecurity.IsTechnician)
		{
			return;
		}
		if (this.BeforeTestNameEdit != null)
		{
			List<BaseTest> list = new List<BaseTest>();
			list.Add(baseTest);
			TestTreeNodeEventArgs testTreeNodeEventArgs = new TestTreeNodeEventArgs(list);
			this.BeforeTestNameEdit(this, testTreeNodeEventArgs);
			if (testTreeNodeEventArgs.Cancel)
			{
				return;
			}
		}
		baseTest.TestUserInterfaceProperties.IsInEditMode = true;
	}

	public void EndTestNameEdit(BaseTest baseTest, bool commit)
	{
		if (!commit)
		{
			baseTest.Properties.TestName = baseTest.TestUserInterfaceProperties.OriginalTestNameBeforeEdit;
		}
		baseTest.TestUserInterfaceProperties.IsInEditMode = false;
		if (this.AfterTestNameEdit != null && commit)
		{
			this.AfterTestNameEdit(this, new TestTreeNodeEventArgs(new List<BaseTest> { baseTest }));
		}
	}

	public void Cut(List<BaseTest> tests)
	{
		if (QSecurity.LockedQGroup || !QSecurity.IsTechnician)
		{
			return;
		}
		if (this.BeforeTestCut != null)
		{
			TestTreeNodeEventArgs testTreeNodeEventArgs = new TestTreeNodeEventArgs(tests);
			this.BeforeTestCut(this, testTreeNodeEventArgs);
			if (testTreeNodeEventArgs.Cancel)
			{
				return;
			}
		}
		CutTests.Clear();
		CutTests.AddRange(tests);
		foreach (BaseTest test in tests)
		{
			FolderTest parent = test.Parent;
			parent.TestList.Remove(test);
		}
		if (this.AfterTestCut != null)
		{
			TestTreeNodeEventArgs e = new TestTreeNodeEventArgs(tests);
			this.AfterTestCut(this, e);
		}
	}

	public void Copy(List<BaseTest> tests)
	{
		if (QSecurity.LockedQGroup || !QSecurity.IsTechnician)
		{
			return;
		}
		if (this.BeforeTestCopy != null)
		{
			TestTreeNodeEventArgs testTreeNodeEventArgs = new TestTreeNodeEventArgs(tests);
			this.BeforeTestCopy(this, testTreeNodeEventArgs);
			if (testTreeNodeEventArgs.Cancel)
			{
				return;
			}
		}
		if (this.AfterTestCopy != null)
		{
			TestTreeNodeEventArgs e = new TestTreeNodeEventArgs(tests);
			this.AfterTestCopy(this, e);
		}
	}
}
