using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;
using QC.QsprShared.Utilities;
using QsprMiscShared.Utilities;

namespace QC.QTMApplication;

public class MenuToolBarManager
{
	private MenuBase menu = new Menu();

	private ToolBar toolBar = new ToolBar();

	private UIElement parentWindow;

	private Dictionary<string, List<Control>> toolsDict = new Dictionary<string, List<Control>>();

	public MenuToolBarManager(MenuBase menu, ToolBar toolBar, UIElement parentWindow)
	{
		this.menu = menu;
		this.toolBar = toolBar;
		this.parentWindow = parentWindow;
		PopulateToolsDict();
	}

	private void PopulateToolsDict()
	{
		if (menu != null)
		{
			foreach (object item in (IEnumerable)menu.Items)
			{
				if (item is MenuItem)
				{
					MenuItem menuItem = (MenuItem)item;
					AddMenuItem(menuItem);
				}
			}
		}
		if (toolBar == null)
		{
			return;
		}
		foreach (object item2 in (IEnumerable)toolBar.Items)
		{
			if (item2 is Button)
			{
				Button control = (Button)item2;
				string controlKey = GetControlKey(control);
				AddToToolsDict(control, controlKey);
			}
		}
	}

	private void AddMenuItem(MenuItem menuItem)
	{
		if (menuItem == null)
		{
			return;
		}
		string controlKey = GetControlKey(menuItem);
		AddToToolsDict(menuItem, controlKey);
		if (menuItem.Items == null || menuItem.Items.Count <= 0)
		{
			return;
		}
		foreach (object item in (IEnumerable)menuItem.Items)
		{
			if (item is MenuItem)
			{
				MenuItem menuItem2 = (MenuItem)item;
				AddMenuItem(menuItem2);
			}
		}
	}

	public static string GetControlKey(Control control)
	{
		if (control.Tag != null)
		{
			return control.Tag.ToString();
		}
		return null;
	}

	private void AddToToolsDict(Control control, string key)
	{
		if (!string.IsNullOrWhiteSpace(key))
		{
			if (toolsDict.ContainsKey(key))
			{
				toolsDict[key].Add(control);
				return;
			}
			List<Control> list = new List<Control>();
			list.Add(control);
			toolsDict[key] = list;
		}
	}

	public void SetImage(string key, string imageUrl)
	{
		if (parentWindow.InvokeRequired())
		{
			parentWindow.Invoke(delegate
			{
				SetImage(key, imageUrl);
			});
			return;
		}
		List<Control> matchingControls = GetMatchingControls(key);
		if (matchingControls == null || matchingControls.Count <= 0)
		{
			return;
		}
		for (int i = 0; i < matchingControls.Count; i++)
		{
			if (matchingControls[i] is Button)
			{
				Button button = (Button)matchingControls[i];
				if (button.Content is Image image)
				{
					image.Source = new BitmapImage(new Uri(imageUrl));
				}
			}
			else if (matchingControls[i] is MenuItem)
			{
				MenuItem menuItem = (MenuItem)matchingControls[i];
				if (menuItem.Icon is Image image2)
				{
					image2.Source = new BitmapImage(new Uri(imageUrl));
				}
			}
		}
	}

	public void SetEnabled(string key, bool enabledValue)
	{
		PerformActionOnSetofTools(key, delegate(Control ctrl)
		{
			ctrl.IsEnabled = enabledValue;
		});
	}

	public void SetVisible(string key, bool visiblilty)
	{
		Visibility visibilityState = QTMUtility.ConvertBoolVisibilityToWPFVisibility(visiblilty);
		PerformActionOnSetofTools(key, delegate(Control ctrl)
		{
			ctrl.Visibility = visibilityState;
		});
	}

	public void SetToolTipText(string key, string toolTipText)
	{
		ToolTip toolTip = new ToolTip();
		toolTip.Content = toolTipText;
		PerformActionOnSetofTools(key, delegate(Control ctrl)
		{
			ctrl.ToolTip = toolTip;
		});
	}

	private List<Control> GetMatchingControls(string toolkey)
	{
		if (!string.IsNullOrWhiteSpace(toolkey) && toolsDict.ContainsKey(toolkey))
		{
			return toolsDict[toolkey];
		}
		return null;
	}

	private void PerformActionOnSetofTools(string toolKey, Action<Control> actionToPerform)
	{
		if (actionToPerform == null)
		{
			return;
		}
		List<Control> matchingControls = GetMatchingControls(toolKey);
		if (matchingControls == null || matchingControls.Count <= 0)
		{
			return;
		}
		matchingControls.ForEach(delegate(Control ctrl)
		{
			if (parentWindow.InvokeRequired())
			{
				parentWindow.Invoke(delegate
				{
					actionToPerform(ctrl);
				});
			}
			else
			{
				actionToPerform(ctrl);
			}
		});
	}
}
