using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;

namespace AxCopilot.Views;

internal sealed class CustomMessageBox : Window
{
	private MessageBoxResult _result = MessageBoxResult.None;

	private CustomMessageBox(string message, string title, MessageBoxButton buttons, MessageBoxImage icon)
	{
		CustomMessageBox customMessageBox = this;
		base.Title = title;
		base.Width = 400.0;
		base.MinWidth = 320.0;
		base.MaxWidth = 520.0;
		base.SizeToContent = SizeToContent.Height;
		base.WindowStartupLocation = WindowStartupLocation.CenterScreen;
		base.ResizeMode = ResizeMode.NoResize;
		base.WindowStyle = WindowStyle.None;
		base.AllowsTransparency = true;
		base.Background = Brushes.Transparent;
		base.Topmost = true;
		Brush background = (Application.Current.TryFindResource("LauncherBackground") as Brush) ?? new SolidColorBrush(Color.FromRgb(26, 27, 46));
		Brush foreground = (Application.Current.TryFindResource("PrimaryText") as Brush) ?? Brushes.White;
		Brush foreground2 = (Application.Current.TryFindResource("SecondaryText") as Brush) ?? Brushes.Gray;
		Brush bg = (Application.Current.TryFindResource("AccentColor") as Brush) ?? new SolidColorBrush(Color.FromRgb(75, 94, 252));
		Brush borderBrush = (Application.Current.TryFindResource("BorderColor") as Brush) ?? Brushes.Gray;
		Brush bg2 = (Application.Current.TryFindResource("ItemBackground") as Brush) ?? new SolidColorBrush(Color.FromRgb(42, 43, 64));
		Border border = new Border
		{
			Background = background,
			CornerRadius = new CornerRadius(16.0),
			BorderBrush = borderBrush,
			BorderThickness = new Thickness(1.0),
			Padding = new Thickness(28.0, 24.0, 28.0, 20.0),
			Effect = new DropShadowEffect
			{
				BlurRadius = 24.0,
				ShadowDepth = 4.0,
				Opacity = 0.3,
				Color = Colors.Black
			}
		};
		StackPanel stackPanel = new StackPanel();
		Grid grid = new Grid
		{
			Margin = new Thickness(0.0, 0.0, 0.0, 16.0)
		};
		grid.MouseLeftButtonDown += delegate
		{
			try
			{
				customMessageBox.DragMove();
			}
			catch
			{
			}
		};
		StackPanel stackPanel2 = new StackPanel
		{
			Orientation = Orientation.Horizontal
		};
		var (text, foreground3) = GetIconInfo(icon);
		if (!string.IsNullOrEmpty(text))
		{
			stackPanel2.Children.Add(new TextBlock
			{
				Text = text,
				FontFamily = new FontFamily("Segoe MDL2 Assets"),
				FontSize = 18.0,
				Foreground = foreground3,
				VerticalAlignment = VerticalAlignment.Center,
				Margin = new Thickness(0.0, 0.0, 10.0, 0.0)
			});
		}
		stackPanel2.Children.Add(new TextBlock
		{
			Text = title,
			FontSize = 15.0,
			FontWeight = FontWeights.SemiBold,
			Foreground = foreground,
			VerticalAlignment = VerticalAlignment.Center
		});
		grid.Children.Add(stackPanel2);
		Button button = new Button
		{
			Content = "\ue8bb",
			FontFamily = new FontFamily("Segoe MDL2 Assets"),
			FontSize = 10.0,
			Foreground = foreground2,
			Background = Brushes.Transparent,
			BorderThickness = new Thickness(0.0),
			Padding = new Thickness(6.0),
			Cursor = Cursors.Hand,
			HorizontalAlignment = HorizontalAlignment.Right,
			VerticalAlignment = VerticalAlignment.Center
		};
		button.Click += delegate
		{
			customMessageBox._result = MessageBoxResult.Cancel;
			customMessageBox.Close();
		};
		grid.Children.Add(button);
		stackPanel.Children.Add(grid);
		stackPanel.Children.Add(new TextBlock
		{
			Text = message,
			FontSize = 13.0,
			Foreground = foreground,
			TextWrapping = TextWrapping.Wrap,
			LineHeight = 20.0,
			Margin = new Thickness(0.0, 0.0, 0.0, 24.0)
		});
		StackPanel stackPanel3 = new StackPanel
		{
			Orientation = Orientation.Horizontal,
			HorizontalAlignment = HorizontalAlignment.Right
		};
		switch (buttons)
		{
		case MessageBoxButton.OK:
			stackPanel3.Children.Add(CreateButton("확인", bg, isPrimary: true, MessageBoxResult.OK));
			break;
		case MessageBoxButton.OKCancel:
			stackPanel3.Children.Add(CreateButton("취소", bg2, isPrimary: false, MessageBoxResult.Cancel));
			stackPanel3.Children.Add(CreateButton("확인", bg, isPrimary: true, MessageBoxResult.OK));
			break;
		case MessageBoxButton.YesNo:
			stackPanel3.Children.Add(CreateButton("아니오", bg2, isPrimary: false, MessageBoxResult.No));
			stackPanel3.Children.Add(CreateButton("예", bg, isPrimary: true, MessageBoxResult.Yes));
			break;
		case MessageBoxButton.YesNoCancel:
			stackPanel3.Children.Add(CreateButton("취소", bg2, isPrimary: false, MessageBoxResult.Cancel));
			stackPanel3.Children.Add(CreateButton("아니오", bg2, isPrimary: false, MessageBoxResult.No));
			stackPanel3.Children.Add(CreateButton("예", bg, isPrimary: true, MessageBoxResult.Yes));
			break;
		}
		stackPanel.Children.Add(stackPanel3);
		border.Child = stackPanel;
		base.Content = border;
		base.KeyDown += delegate(object _, KeyEventArgs e)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Invalid comparison between Unknown and I4
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Invalid comparison between Unknown and I4
			if ((int)e.Key == 13)
			{
				customMessageBox._result = ((buttons == MessageBoxButton.YesNo) ? MessageBoxResult.No : MessageBoxResult.Cancel);
				customMessageBox.Close();
			}
			else if ((int)e.Key == 6)
			{
				if (1 == 0)
				{
				}
				MessageBoxButton messageBoxButton = buttons;
				MessageBoxResult result = (((uint)(messageBoxButton - 3) > 1u) ? MessageBoxResult.OK : MessageBoxResult.Yes);
				if (1 == 0)
				{
				}
				customMessageBox._result = result;
				customMessageBox.Close();
			}
		};
	}

	private Button CreateButton(string text, Brush bg, bool isPrimary, MessageBoxResult result)
	{
		Button button = new Button();
		button.Content = text;
		button.FontSize = 12.5;
		button.FontWeight = (isPrimary ? FontWeights.SemiBold : FontWeights.Normal);
		button.Foreground = (isPrimary ? Brushes.White : ((Application.Current.TryFindResource("PrimaryText") as Brush) ?? Brushes.White));
		button.Background = bg;
		button.BorderThickness = new Thickness(0.0);
		button.Padding = new Thickness(20.0, 8.0, 20.0, 8.0);
		button.Margin = new Thickness(6.0, 0.0, 0.0, 0.0);
		button.Cursor = Cursors.Hand;
		button.MinWidth = 80.0;
		Button button2 = button;
		ControlTemplate controlTemplate = new ControlTemplate(typeof(Button));
		FrameworkElementFactory frameworkElementFactory = new FrameworkElementFactory(typeof(Border));
		frameworkElementFactory.SetValue(Border.BackgroundProperty, bg);
		frameworkElementFactory.SetValue(Border.CornerRadiusProperty, new CornerRadius(10.0));
		frameworkElementFactory.SetValue(Border.PaddingProperty, new Thickness(20.0, 8.0, 20.0, 8.0));
		FrameworkElementFactory frameworkElementFactory2 = new FrameworkElementFactory(typeof(ContentPresenter));
		frameworkElementFactory2.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center);
		frameworkElementFactory2.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Center);
		frameworkElementFactory.AppendChild(frameworkElementFactory2);
		controlTemplate.VisualTree = frameworkElementFactory;
		button2.Template = controlTemplate;
		button2.Click += delegate
		{
			_result = result;
			Close();
		};
		return button2;
	}

	private static (string icon, Brush color) GetIconInfo(MessageBoxImage image)
	{
		if (1 == 0)
		{
		}
		(string, SolidColorBrush) tuple = image switch
		{
			MessageBoxImage.Hand => ("\uea39", new SolidColorBrush(Color.FromRgb(229, 62, 62))), 
			MessageBoxImage.Exclamation => ("\ue7ba", new SolidColorBrush(Color.FromRgb(221, 107, 32))), 
			MessageBoxImage.Asterisk => ("\ue946", new SolidColorBrush(Color.FromRgb(75, 94, 252))), 
			MessageBoxImage.Question => ("\ue9ce", new SolidColorBrush(Color.FromRgb(75, 94, 252))), 
			_ => ("", Brushes.Transparent), 
		};
		if (1 == 0)
		{
		}
		(string, SolidColorBrush) tuple2 = tuple;
		return (icon: tuple2.Item1, color: tuple2.Item2);
	}

	public static MessageBoxResult Show(string message)
	{
		return Show(message, "AX Copilot", MessageBoxButton.OK, MessageBoxImage.None);
	}

	public static MessageBoxResult Show(string message, string title)
	{
		return Show(message, title, MessageBoxButton.OK, MessageBoxImage.None);
	}

	public static MessageBoxResult Show(string message, string title, MessageBoxButton buttons)
	{
		return Show(message, title, buttons, MessageBoxImage.None);
	}

	public static MessageBoxResult Show(string message, string title, MessageBoxButton buttons, MessageBoxImage icon)
	{
		CustomMessageBox dlg = new CustomMessageBox(message, title, buttons, icon);
		dlg.Opacity = 0.0;
		dlg.Loaded += delegate
		{
			DoubleAnimation animation = new DoubleAnimation(0.0, 1.0, TimeSpan.FromMilliseconds(150.0));
			dlg.BeginAnimation(UIElement.OpacityProperty, animation);
		};
		Window window = Application.Current.Windows.OfType<Window>().FirstOrDefault((Window w) => w.IsActive && !(w is CustomMessageBox)) ?? Application.Current.MainWindow;
		Border toast = null;
		Grid rootGrid = null;
		if (window?.Content is Border { Child: Grid child })
		{
			rootGrid = child;
			toast = CreateToastBanner(title, icon);
			rootGrid.Children.Add(toast);
		}
		else if (window?.Content is Grid grid)
		{
			rootGrid = grid;
			toast = CreateToastBanner(title, icon);
			rootGrid.Children.Add(toast);
		}
		dlg.ShowDialog();
		if (toast != null && rootGrid != null)
		{
			DoubleAnimation doubleAnimation = new DoubleAnimation(1.0, 0.0, TimeSpan.FromMilliseconds(400.0))
			{
				EasingFunction = new QuadraticEase
				{
					EasingMode = EasingMode.EaseIn
				}
			};
			doubleAnimation.Completed += delegate
			{
				rootGrid.Children.Remove(toast);
			};
			toast.BeginAnimation(UIElement.OpacityProperty, doubleAnimation);
		}
		return dlg._result;
	}

	private static Border CreateToastBanner(string title, MessageBoxImage icon)
	{
		var (text, foreground) = GetIconInfo(icon);
		if (string.IsNullOrEmpty(text))
		{
			text = "\ue946";
			foreground = new SolidColorBrush(Color.FromRgb(75, 94, 252));
		}
		StackPanel stackPanel = new StackPanel
		{
			Orientation = Orientation.Horizontal
		};
		stackPanel.Children.Add(new TextBlock
		{
			Text = text,
			FontFamily = new FontFamily("Segoe MDL2 Assets"),
			FontSize = 12.0,
			Foreground = foreground,
			VerticalAlignment = VerticalAlignment.Center,
			Margin = new Thickness(0.0, 0.0, 8.0, 0.0)
		});
		stackPanel.Children.Add(new TextBlock
		{
			Text = title,
			FontSize = 11.5,
			FontWeight = FontWeights.SemiBold,
			Foreground = Brushes.White,
			VerticalAlignment = VerticalAlignment.Center
		});
		Border toast = new Border
		{
			Background = new SolidColorBrush(Color.FromArgb(232, 42, 43, 64)),
			CornerRadius = new CornerRadius(10.0),
			Padding = new Thickness(16.0, 8.0, 16.0, 8.0),
			HorizontalAlignment = HorizontalAlignment.Center,
			VerticalAlignment = VerticalAlignment.Bottom,
			Margin = new Thickness(0.0, 0.0, 0.0, 12.0),
			Effect = new DropShadowEffect
			{
				BlurRadius = 12.0,
				ShadowDepth = 2.0,
				Opacity = 0.3,
				Color = Colors.Black
			},
			Opacity = 0.0,
			Child = stackPanel
		};
		Grid.SetColumnSpan(toast, 10);
		Grid.SetRowSpan(toast, 10);
		TranslateTransform translate = new TranslateTransform(0.0, 20.0);
		toast.RenderTransform = translate;
		toast.Loaded += delegate
		{
			DoubleAnimation animation = new DoubleAnimation(0.0, 1.0, TimeSpan.FromMilliseconds(250.0))
			{
				EasingFunction = new QuadraticEase
				{
					EasingMode = EasingMode.EaseOut
				}
			};
			DoubleAnimation animation2 = new DoubleAnimation(20.0, 0.0, TimeSpan.FromMilliseconds(300.0))
			{
				EasingFunction = new BackEase
				{
					EasingMode = EasingMode.EaseOut,
					Amplitude = 0.3
				}
			};
			toast.BeginAnimation(UIElement.OpacityProperty, animation);
			translate.BeginAnimation(TranslateTransform.YProperty, animation2);
		};
		return toast;
	}

	public static MessageBoxResult ShowStandalone(string message, string title, MessageBoxButton buttons = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None)
	{
		CustomMessageBox customMessageBox = new CustomMessageBox(message, title, buttons, icon);
		customMessageBox.ShowDialog();
		return customMessageBox._result;
	}
}
