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

namespace QC.QTMApplication;

public class TestTreeBrowser
{
	public RootTest Root { get; protected set; }

	public TestTreeBrowser(RootTest root)
	{
		Root = root;
	}

	public static TreeViewItem GetTreeItemFromTest(ItemsControl testTree, BaseTest testToFind)
	{
		TreeViewItem treeViewItem = null;
		foreach (object item in (IEnumerable)testTree.Items)
		{
			TreeViewItem treeViewItem2 = (TreeViewItem)testTree.ItemContainerGenerator.ContainerFromItem(item);
			if (treeViewItem2 != null && item == testToFind)
			{
				treeViewItem = treeViewItem2;
				return treeViewItem;
			}
		}
		if (treeViewItem == null)
		{
			foreach (object item2 in (IEnumerable)testTree.Items)
			{
				TreeViewItem treeViewItem3 = (TreeViewItem)testTree.ItemContainerGenerator.ContainerFromItem(item2);
				if (treeViewItem3 != null && treeViewItem3.Items.Count > 0)
				{
					bool isExpanded = treeViewItem3.IsExpanded;
					treeViewItem3.IsExpanded = true;
					treeViewItem3.UpdateLayout();
					treeViewItem = GetTreeItemFromTest(treeViewItem3, testToFind);
					if (treeViewItem != null)
					{
						return treeViewItem;
					}
					treeViewItem3.IsExpanded = isExpanded;
				}
			}
		}
		return treeViewItem;
	}

	public static BaseTest GetNextTest(BaseTest baseTest)
	{
		BaseTest baseTest2 = null;
		if (baseTest is FolderTest)
		{
			FolderTest folderTest = baseTest as FolderTest;
			if (folderTest.TestList.Count > 0 && folderTest.Properties.Expanded)
			{
				return folderTest.TestList[0];
			}
			return GetNextSibling(baseTest, checkParentSiblings: true);
		}
		return GetNextSibling(baseTest, checkParentSiblings: true);
	}

	public static BaseTest GetNextSibling(BaseTest baseTest, bool checkParentSiblings = false)
	{
		BaseTest result = null;
		if (baseTest.Parent != null)
		{
			FolderTest parent = baseTest.Parent;
			int num = parent.TestList.IndexOf(baseTest);
			if (num < parent.TestList.Count - 1)
			{
				result = parent.TestList[num + 1];
			}
			else if (checkParentSiblings)
			{
				result = GetNextSibling(parent);
			}
		}
		return result;
	}

	public static BaseTest GetPreviousTest(BaseTest baseTest)
	{
		BaseTest result = null;
		if (baseTest is FolderTest)
		{
			FolderTest folderTest = baseTest as FolderTest;
			if (folderTest.Parent != null)
			{
				FolderTest parent = folderTest.Parent;
				if (parent.Properties.Expanded)
				{
					int num = parent.TestList.IndexOf(folderTest);
					if (num > 0)
					{
						BaseTest previousSibling = GetPreviousSibling(folderTest);
						if (previousSibling is FolderTest)
						{
							BaseTest baseTest2 = previousSibling;
							while (baseTest2 is FolderTest && baseTest2 != null && ((FolderTest)baseTest2).TestList.Count > 0 && baseTest2.Properties.Expanded)
							{
								FolderTest folderTest2 = baseTest2 as FolderTest;
								baseTest2 = folderTest2.TestList[folderTest2.TestList.Count - 1];
							}
							result = baseTest2;
						}
						else
						{
							result = previousSibling;
						}
					}
					else
					{
						result = parent;
					}
				}
				else
				{
					result = GetPreviousSibling(parent);
				}
			}
		}
		else if (baseTest.Parent != null)
		{
			int num2 = baseTest.Parent.TestList.IndexOf(baseTest);
			result = ((num2 <= 0) ? baseTest.Parent : GetPreviousSibling(baseTest));
		}
		return result;
	}

	public static BaseTest GetPreviousSibling(BaseTest baseTest)
	{
		BaseTest result = null;
		if (baseTest.Parent != null)
		{
			FolderTest parent = baseTest.Parent;
			int num = parent.TestList.IndexOf(baseTest);
			if (num > 0)
			{
				result = parent.TestList[num - 1];
			}
		}
		return result;
	}

	public static void CollapseAll(BaseTest baseTest)
	{
		if (!(baseTest is FolderTest))
		{
			return;
		}
		FolderTest folderTest = baseTest as FolderTest;
		foreach (BaseTest test in folderTest.TestList)
		{
			CollapseAll(test);
		}
		folderTest.Properties.Expanded = false;
	}

	public static void ExpandAll(BaseTest baseTest)
	{
		if (!(baseTest is FolderTest))
		{
			return;
		}
		FolderTest folderTest = baseTest as FolderTest;
		foreach (BaseTest test in folderTest.TestList)
		{
			ExpandAll(test);
		}
		folderTest.Properties.Expanded = true;
	}
}
