using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Threading;

namespace AxCopilot.Views;

public class TrayMenuWindow : Window, IComponentConnector
{
	private struct POINT
	{
		public int X;

		public int Y;
	}

	private DispatcherTimer? _autoCloseTimer;

	private static readonly Brush BulbOnBrush = new SolidColorBrush(Color.FromRgb(byte.MaxValue, 185, 0));

	private static readonly Brush BulbOffBrush = new SolidColorBrush(Color.FromRgb(120, 120, 140));

	internal Border RootBorder;

	internal StackPanel MenuPanel;

	private bool _contentLoaded;

	public event Action? Opening;

	public TrayMenuWindow()
	{
		InitializeComponent();
		base.MouseLeave += delegate
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			DispatcherTimer? autoCloseTimer = _autoCloseTimer;
			if (autoCloseTimer != null)
			{
				autoCloseTimer.Stop();
			}
			_autoCloseTimer = new DispatcherTimer
			{
				Interval = TimeSpan.FromMilliseconds(400.0)
			};
			_autoCloseTimer.Tick += delegate
			{
				_autoCloseTimer.Stop();
				Hide();
			};
			_autoCloseTimer.Start();
		};
		base.MouseEnter += delegate
		{
			DispatcherTimer? autoCloseTimer = _autoCloseTimer;
			if (autoCloseTimer != null)
			{
				autoCloseTimer.Stop();
			}
		};
	}

	public TrayMenuWindow AddItem(string glyph, string text, Action onClick)
	{
		Border border = CreateItemBorder(glyph, text);
		border.MouseLeftButtonUp += delegate
		{
			Hide();
			onClick();
		};
		MenuPanel.Children.Add(border);
		return this;
	}

	public TrayMenuWindow AddItem(string glyph, string text, Action onClick, out Border itemRef)
	{
		Border border = CreateItemBorder(glyph, text);
		border.MouseLeftButtonUp += delegate
		{
			Hide();
			onClick();
		};
		MenuPanel.Children.Add(border);
		itemRef = border;
		return this;
	}

	public TrayMenuWindow AddToggleItem(string glyph, string text, bool initialChecked, Action<bool> onToggle, out Func<bool> getChecked, out Action<string> setText)
	{
		TextBlock glyphBlock = new TextBlock
		{
			Text = glyph,
			FontFamily = new FontFamily("Segoe MDL2 Assets"),
			FontSize = 14.0,
			Foreground = (initialChecked ? BulbOnBrush : BulbOffBrush),
			VerticalAlignment = VerticalAlignment.Center,
			Width = 20.0,
			TextAlignment = TextAlignment.Center
		};
		if (initialChecked)
		{
			glyphBlock.Effect = new DropShadowEffect
			{
				Color = Color.FromRgb(byte.MaxValue, 185, 0),
				BlurRadius = 12.0,
				ShadowDepth = 0.0,
				Opacity = 0.7
			};
		}
		TextBlock label = new TextBlock
		{
			Text = text,
			FontSize = 13.0,
			VerticalAlignment = VerticalAlignment.Center
		};
		label.SetResourceReference(TextBlock.ForegroundProperty, "PrimaryText");
		StackPanel child = new StackPanel
		{
			Orientation = Orientation.Horizontal,
			Children = 
			{
				(UIElement)glyphBlock,
				(UIElement)label
			}
		};
		Border border = new Border
		{
			Child = child,
			CornerRadius = new CornerRadius(6.0),
			Padding = new Thickness(12.0, 8.0, 16.0, 8.0),
			Cursor = Cursors.Hand,
			Background = Brushes.Transparent
		};
		border.MouseEnter += delegate
		{
			border.Background = (TryFindResource("ItemHoverBackground") as Brush) ?? Brushes.Transparent;
		};
		border.MouseLeave += delegate
		{
			border.Background = Brushes.Transparent;
		};
		border.MouseLeftButtonUp += delegate
		{
			initialChecked = !initialChecked;
			glyphBlock.Foreground = (initialChecked ? BulbOnBrush : BulbOffBrush);
			glyphBlock.Effect = (initialChecked ? new DropShadowEffect
			{
				Color = Color.FromRgb(byte.MaxValue, 185, 0),
				BlurRadius = 12.0,
				ShadowDepth = 0.0,
				Opacity = 0.7
			} : null);
			onToggle(initialChecked);
		};
		getChecked = () => initialChecked;
		setText = delegate(string t)
		{
			label.Text = t;
		};
		MenuPanel.Children.Add(border);
		return this;
	}

	public TrayMenuWindow AddSeparator()
	{
		Border border = new Border
		{
			Height = 1.0,
			Margin = new Thickness(16.0, 5.0, 16.0, 5.0)
		};
		border.SetResourceReference(Border.BackgroundProperty, "SeparatorColor");
		MenuPanel.Children.Add(border);
		return this;
	}

	public void ShowAtTray()
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		Rect workArea = SystemParameters.WorkArea;
		base.Opacity = 0.0;
		Show();
		UpdateLayout();
		double actualWidth = base.ActualWidth;
		double actualHeight = base.ActualHeight;
		POINT cursorPosition = GetCursorPosition();
		double pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
		double num = (double)cursorPosition.X / pixelsPerDip;
		double num2 = (double)cursorPosition.Y / pixelsPerDip;
		double num3 = num - actualWidth - 8.0;
		double num4 = num2 - actualHeight - 8.0;
		if (num3 < ((Rect)(ref workArea)).Left)
		{
			num3 = ((Rect)(ref workArea)).Left + 4.0;
		}
		if (num4 < ((Rect)(ref workArea)).Top)
		{
			num4 = ((Rect)(ref workArea)).Top + 4.0;
		}
		if (num3 + actualWidth > ((Rect)(ref workArea)).Right)
		{
			num3 = ((Rect)(ref workArea)).Right - actualWidth - 4.0;
		}
		if (num4 + actualHeight > ((Rect)(ref workArea)).Bottom)
		{
			num4 = ((Rect)(ref workArea)).Bottom - actualHeight - 4.0;
		}
		base.Left = num3;
		base.Top = num4;
		Activate();
		DoubleAnimation animation = new DoubleAnimation(0.0, 1.0, TimeSpan.FromMilliseconds(120.0))
		{
			EasingFunction = new CubicEase
			{
				EasingMode = EasingMode.EaseOut
			}
		};
		BeginAnimation(UIElement.OpacityProperty, animation);
	}

	public void ShowWithUpdate()
	{
		this.Opening?.Invoke();
		ShowAtTray();
	}

	private Border CreateItemBorder(string glyph, string text)
	{
		TextBlock glyphBlock = new TextBlock
		{
			Text = glyph,
			FontFamily = new FontFamily("Segoe MDL2 Assets"),
			FontSize = 14.0,
			VerticalAlignment = VerticalAlignment.Center,
			Width = 20.0,
			TextAlignment = TextAlignment.Center
		};
		glyphBlock.SetResourceReference(TextBlock.ForegroundProperty, "SecondaryText");
		TextBlock textBlock = new TextBlock
		{
			Text = text,
			FontSize = 13.0,
			VerticalAlignment = VerticalAlignment.Center
		};
		textBlock.SetResourceReference(TextBlock.ForegroundProperty, "PrimaryText");
		StackPanel child = new StackPanel
		{
			Orientation = Orientation.Horizontal,
			Children = 
			{
				(UIElement)glyphBlock,
				(UIElement)textBlock
			}
		};
		Border border = new Border
		{
			Child = child,
			CornerRadius = new CornerRadius(6.0),
			Padding = new Thickness(12.0, 8.0, 16.0, 8.0),
			Cursor = Cursors.Hand,
			Background = Brushes.Transparent
		};
		border.MouseEnter += delegate
		{
			border.Background = (TryFindResource("ItemHoverBackground") as Brush) ?? Brushes.Transparent;
			glyphBlock.Foreground = (TryFindResource("AccentColor") as Brush) ?? Brushes.Blue;
		};
		border.MouseLeave += delegate
		{
			border.Background = Brushes.Transparent;
			glyphBlock.SetResourceReference(TextBlock.ForegroundProperty, "SecondaryText");
		};
		return border;
	}

	private void Window_Deactivated(object sender, EventArgs e)
	{
		Hide();
	}

	[DllImport("user32.dll")]
	[return: MarshalAs(UnmanagedType.Bool)]
	private static extern bool GetCursorPos(out POINT lpPoint);

	private static POINT GetCursorPosition()
	{
		GetCursorPos(out var lpPoint);
		return lpPoint;
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "10.0.5.0")]
	public void InitializeComponent()
	{
		if (!_contentLoaded)
		{
			_contentLoaded = true;
			Uri resourceLocator = new Uri("/AxCopilot;component/views/traymenuwindow.xaml", UriKind.Relative);
			Application.LoadComponent(this, resourceLocator);
		}
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "10.0.5.0")]
	[EditorBrowsable(EditorBrowsableState.Never)]
	void IComponentConnector.Connect(int connectionId, object target)
	{
		switch (connectionId)
		{
		case 1:
			((TrayMenuWindow)target).Deactivated += Window_Deactivated;
			break;
		case 2:
			RootBorder = (Border)target;
			break;
		case 3:
			MenuPanel = (StackPanel)target;
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}
