using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using QC.QsprShared.GlobalData;
using QC.QsprTestObjects.Tests.Base;

namespace QC.QsprTestObjects.Registry;

public class QTMTestContainerUserInterfaceProperties : INotifyPropertyChanged
{
	private string nodeImageUri;

	private string fontColor;

	private FontStyle fontStyle;

	private string key;

	private bool isInEditMode;

	private string editName = string.Empty;

	private bool unloadSupported;

	private string toolTipText;

	private Visibility tooltipVisiblility = Visibility.Collapsed;

	public QTMTestContainer TestContainer { get; protected set; }

	public string NodeImageUri
	{
		get
		{
			return nodeImageUri;
		}
		set
		{
			nodeImageUri = value;
			RaisePropertyChangedEvent("NodeImageUri");
		}
	}

	public string FontColor
	{
		get
		{
			return fontColor;
		}
		set
		{
			fontColor = value;
			RaisePropertyChangedEvent("FontColor");
		}
	}

	public FontStyle FontStyle
	{
		get
		{
			return fontStyle;
		}
		set
		{
			fontStyle = value;
			RaisePropertyChangedEvent("FontStyle");
		}
	}

	public string Key
	{
		get
		{
			return key;
		}
		set
		{
			key = value;
			RaisePropertyChangedEvent("Key");
		}
	}

	public bool IsInEditMode
	{
		get
		{
			return isInEditMode;
		}
		set
		{
			isInEditMode = value;
			RaisePropertyChangedEvent("IsInEditMode");
		}
	}

	public string EditName
	{
		get
		{
			return editName;
		}
		set
		{
			editName = value;
			RaisePropertyChangedEvent("EditName");
		}
	}

	public bool UnloadSupported
	{
		get
		{
			return unloadSupported;
		}
		set
		{
			unloadSupported = value;
			RaisePropertyChangedEvent("UnloadSupported");
		}
	}

	public string ToolTipText
	{
		get
		{
			toolTipText = GetToolTipText(TestContainer);
			return toolTipText;
		}
		set
		{
			toolTipText = value;
			RaisePropertyChangedEvent("ToolTipText");
		}
	}

	public Visibility ToolTipVisibility
	{
		get
		{
			if (tooltipVisiblility == Visibility.Collapsed)
			{
				tooltipVisiblility = GetToolTipVisibility();
			}
			return tooltipVisiblility;
		}
		set
		{
			tooltipVisiblility = value;
			RaisePropertyChangedEvent("ToolTipVisibility");
		}
	}

	private QTMData qtmData => TestContainer.theRegistry.scheduler.QTMData;

	public ObservableCollection<QTMTestContainerItem> Items { get; set; }

	public ObservableCollection<QTMTestContainerItem> DisplayedItems
	{
		get
		{
			if (TestContainer.HideTests)
			{
				return new ObservableCollection<QTMTestContainerItem>();
			}
			return Items;
		}
		set
		{
			RaisePropertyChangedEvent("DisplayedItems");
		}
	}

	public event PropertyChangedEventHandler PropertyChanged;

	private Visibility GetToolTipVisibility()
	{
		if (!qtmData.WConfig.ShowToolTips)
		{
			return Visibility.Hidden;
		}
		return Visibility.Visible;
	}

	private string GetToolTipText(object value)
	{
		StringBuilder stringBuilder = new StringBuilder();
		if (value is QTMTestContainerItem)
		{
			QTMTestContainerItem qTMTestContainerItem = (QTMTestContainerItem)value;
			if (qTMTestContainerItem.Object is QTMTest)
			{
				QTMTest qTMTest = qTMTestContainerItem.Object as QTMTest;
				TestContainer.theRegistry.GetContainerForTest(qTMTest.Name, "This PC", out var tContainer);
				stringBuilder.Append(qTMTest.Name);
				string testDescription = tContainer.GetTestDescription(qTMTest.Name);
				if (!string.IsNullOrEmpty(testDescription))
				{
					stringBuilder.Append(Environment.NewLine + testDescription);
				}
			}
			else
			{
				value = qTMTestContainerItem.TestContainer;
			}
		}
		if (value is QTMTestContainer)
		{
			QTMTestContainer qTMTestContainer = (QTMTestContainer)value;
			FileVersionInfo fileVersionInfo = null;
			FileInfo fileInfo = null;
			if (File.Exists(qTMTestContainer.Name))
			{
				fileVersionInfo = FileVersionInfo.GetVersionInfo(qTMTestContainer.Name);
				fileInfo = new FileInfo(qTMTestContainer.Name);
			}
			QTMTestContainer qTMTestContainer2 = qTMTestContainer;
			if (qTMTestContainer2.ContainerType == QTMTestTypes.DotNetTestEx)
			{
				stringBuilder.Append("# Exclusively Loaded #   \n\n");
			}
			stringBuilder.Append(qTMTestContainer.Name + " ");
			stringBuilder.Append(qTMTestContainer.IsDllPathUpdated ? ("\n  Dll Path specified in the test tree: " + qTMTestContainer.OriginalName) : "");
			if (fileVersionInfo != null && fileInfo != null)
			{
				stringBuilder.Append("\n   Version: " + fileVersionInfo.FileVersion);
				stringBuilder.Append("\n   Creation Time: " + fileInfo.CreationTime.ToString());
				stringBuilder.Append("\n   Last Modified Time: " + fileInfo.LastWriteTime.ToString());
				if (fileVersionInfo.LegalCopyright.Trim().Length > 0)
				{
					stringBuilder.Append("\n   Copyright: " + fileVersionInfo.LegalCopyright);
				}
				if (fileVersionInfo.Comments.Trim().Length > 0)
				{
					stringBuilder.Append("\n   Comments: " + fileVersionInfo.Comments);
				}
			}
		}
		return stringBuilder.ToString();
	}

	public QTMTestContainerUserInterfaceProperties(QTMTestContainer testContainer)
	{
		TestContainer = testContainer;
		Items = new ObservableCollection<QTMTestContainerItem>();
		FontStyle = FontStyles.Normal;
	}

	protected void RaisePropertyChangedEvent(string propertyName)
	{
		if (this.PropertyChanged != null)
		{
			this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}
	}
}
