using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace QFDT_Components;

public class AccessComponentInThreadHelper
{
	public delegate void Helper_SetAttributeCB<T, T2>(UserControl parentPtr, T set_component, SetAttribute attr, T2 value);

	public delegate T2 Helper_GetAttributeCB<T, T2>(UserControl parentPtr, T get_component, SetAttribute attr);

	public static bool getIsChecked(UserControl parentPtr, CheckBox component, SetAttribute attr)
	{
		bool result = false;
		if (component.Dispatcher.CheckAccess())
		{
			if (attr == SetAttribute.IsChecked)
			{
				result = component.IsChecked == true;
			}
		}
		else
		{
			Helper_GetAttributeCB<CheckBox, bool> method = getIsChecked;
			result = (bool)parentPtr.Dispatcher.Invoke(method, parentPtr, component, attr);
		}
		return result;
	}

	public static availablePathType GetSelectedPath(UserControl parentPtr, ListBox component, SetAttribute attr)
	{
		availablePathType result = null;
		if (component.Dispatcher.CheckAccess())
		{
			if (attr == SetAttribute.SelectedItem)
			{
				result = (availablePathType)component.SelectedItem;
			}
		}
		else
		{
			Helper_GetAttributeCB<ListBox, availablePathType> method = GetSelectedPath;
			result = (availablePathType)parentPtr.Dispatcher.Invoke(method, parentPtr, component, attr);
		}
		return result;
	}

	public static object GetSelectorSelectedItem(UserControl parentPtr, Selector component, SetAttribute attr)
	{
		object result = null;
		if (component.Dispatcher.CheckAccess())
		{
			if (attr == SetAttribute.SelectedItem)
			{
				result = component.SelectedItem;
			}
		}
		else
		{
			Helper_GetAttributeCB<Selector, object> method = GetSelectorSelectedItem;
			result = parentPtr.Dispatcher.Invoke(method, parentPtr, component, attr);
		}
		return result;
	}

	public static string GetText<T>(UserControl parentPtr, T component, SetAttribute attr)
	{
		string result = "";
		if (component is TextBox)
		{
			TextBox textBox = component as TextBox;
			if (textBox.Dispatcher.CheckAccess())
			{
				if (attr == SetAttribute.Text)
				{
					result = textBox.Text;
				}
			}
			else
			{
				Helper_GetAttributeCB<T, string> method = GetText;
				result = (string)parentPtr.Dispatcher.Invoke(method, parentPtr, textBox, attr);
			}
		}
		return result;
	}

	public static void SetColor<T>(UserControl parentPtr, T component, SetAttribute attr, UserDefinedColorType value)
	{
		if (!(component is TextBox))
		{
			return;
		}
		TextBox textBox = component as TextBox;
		if (textBox.Dispatcher.CheckAccess())
		{
			if (attr == SetAttribute.BGColor)
			{
				switch (value)
				{
				case UserDefinedColorType.RED:
					textBox.Background = new SolidColorBrush(Color.FromArgb(byte.MaxValue, byte.MaxValue, 0, 0));
					break;
				case UserDefinedColorType.GREEN:
					textBox.Background = new SolidColorBrush(Color.FromArgb(byte.MaxValue, 0, byte.MaxValue, 0));
					break;
				case UserDefinedColorType.DEFAULT:
					textBox.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
					break;
				}
			}
		}
		else
		{
			Helper_SetAttributeCB<T, UserDefinedColorType> method = SetColor;
			parentPtr.Dispatcher.BeginInvoke(method, parentPtr, component, attr, value);
		}
	}

	public static void SetText<T>(UserControl parentPtr, T component, SetAttribute attr, string text)
	{
		if (component is TextBox)
		{
			TextBox textBox = component as TextBox;
			if (textBox.Dispatcher.CheckAccess())
			{
				switch (attr)
				{
				case SetAttribute.Text:
					textBox.Text = text;
					break;
				case SetAttribute.ToolTip:
					textBox.ToolTip = text;
					break;
				}
			}
			else
			{
				Helper_SetAttributeCB<T, string> method = SetText;
				parentPtr.Dispatcher.BeginInvoke(method, parentPtr, component, attr, text);
			}
		}
		else
		{
			if (!(component is Label))
			{
				return;
			}
			Label label = component as Label;
			if (label.Dispatcher.CheckAccess())
			{
				if (attr == SetAttribute.Text)
				{
					label.Content = text;
				}
				return;
			}
			Helper_SetAttributeCB<T, string> method2 = SetText;
			parentPtr.Dispatcher.BeginInvoke(method2, parentPtr, component, attr, text);
		}
	}

	public static void SetEnableOrVisibility<T>(UserControl parentPtr, T component, SetAttribute attr, bool bEnableOrVisbile)
	{
		if (!(component is FrameworkElement))
		{
			return;
		}
		FrameworkElement frameworkElement = component as FrameworkElement;
		if (frameworkElement.Dispatcher.CheckAccess())
		{
			switch (attr)
			{
			case SetAttribute.IsEnable:
				frameworkElement.IsEnabled = bEnableOrVisbile;
				break;
			case SetAttribute.Visibility:
				if (bEnableOrVisbile)
				{
					frameworkElement.Visibility = Visibility.Visible;
				}
				else
				{
					frameworkElement.Visibility = Visibility.Collapsed;
				}
				break;
			}
		}
		else
		{
			Helper_SetAttributeCB<T, bool> method = SetEnableOrVisibility;
			parentPtr.Dispatcher.BeginInvoke(method, parentPtr, component, attr, bEnableOrVisbile);
		}
	}
}
