using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text.Json;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.Windows.Shapes;

namespace AxCopilot.Views;

public class AboutWindow : Window, IComponentConnector
{
	internal Canvas DiamondIconCanvas;

	internal Image AppIconImage;

	internal TextBlock FallbackIcon;

	internal Image MascotImage;

	internal TextBlock AppNameText;

	internal TextBlock VersionText;

	internal Grid MascotOverlay;

	internal Image MascotOverlayImage;

	internal TextBlock PurposeText;

	internal TextBlock CompanyNameText;

	internal Button BlogLinkBtn;

	internal Grid ContributorsGrid;

	internal TextBlock ContributorsText;

	internal TextBlock BuildInfoText;

	private bool _contentLoaded;

	public AboutWindow()
	{
		InitializeComponent();
		base.Loaded += OnLoaded;
		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
			if ((int)e.Key == 13)
			{
				Close();
			}
		};
	}

	private void OnLoaded(object sender, RoutedEventArgs e)
	{
		Version version = Assembly.GetExecutingAssembly().GetName().Version;
		VersionText.Text = ((version != null) ? $"v{version.Major}.{version.Minor}.{version.Build}" : "v1.0");
		BuildInfoText.Text = "AX Copilot  ·  Commander + Agent  ·  © 2026";
		TryLoadBranding(version);
		TryLoadAppIcon();
		TryLoadMascot();
	}

	private void TryLoadBranding(Version? version)
	{
		try
		{
			Uri uriResource = new Uri("pack://application:,,,/Assets/about.json");
			StreamResourceInfo resourceStream = Application.GetResourceStream(uriResource);
			if (resourceStream == null)
			{
				return;
			}
			using StreamReader streamReader = new StreamReader(resourceStream.Stream);
			using JsonDocument jsonDocument = JsonDocument.Parse(streamReader.ReadToEnd());
			JsonElement root = jsonDocument.RootElement;
			string text = Get("appName");
			string text2 = Get("companyName");
			string text3 = Get("authorName");
			string text4 = Get("authorTitle");
			string text5 = Get("purpose");
			string value = Get("copyright");
			string text6 = Get("blogUrl");
			if (!string.IsNullOrWhiteSpace(text))
			{
				AppNameText.Text = text;
			}
			if (!string.IsNullOrWhiteSpace(text2))
			{
				CompanyNameText.Text = text2;
			}
			if (!string.IsNullOrWhiteSpace(text5))
			{
				PurposeText.Text = text5;
			}
			if (!string.IsNullOrWhiteSpace(value))
			{
				string value2 = ((version != null) ? $"v{version.Major}.{version.Minor}.{version.Build}" : "v1.0");
				BuildInfoText.Text = $"{text}  ·  {value2}  ·  Commander + Agent  ·  {value}";
			}
			if (!string.IsNullOrWhiteSpace(text6) && BlogLinkBtn != null)
			{
				BlogLinkBtn.Content = text6;
			}
			string text7 = Get("contributors");
			if (!string.IsNullOrWhiteSpace(text7))
			{
				ContributorsText.Text = text7;
				ContributorsGrid.Visibility = Visibility.Visible;
			}
			string Get(string key)
			{
				JsonElement value3;
				return root.TryGetProperty(key, out value3) ? (value3.GetString() ?? "") : "";
			}
		}
		catch
		{
		}
	}

	private void TryLoadAppIcon()
	{
		try
		{
			Uri uriResource = new Uri("pack://application:,,,/Assets/icon.ico");
			Stream stream = Application.GetResourceStream(uriResource)?.Stream;
			if (stream != null)
			{
				BitmapImage bitmapImage = new BitmapImage();
				bitmapImage.BeginInit();
				bitmapImage.StreamSource = stream;
				bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
				bitmapImage.DecodePixelWidth = 176;
				bitmapImage.EndInit();
				((Freezable)bitmapImage).Freeze();
				AppIconImage.Source = bitmapImage;
				FallbackIcon.Visibility = Visibility.Collapsed;
			}
		}
		catch
		{
		}
	}

	private void TryLoadMascot()
	{
		string[] array = new string[3] { "mascot.png", "mascot.jpg", "mascot.webp" };
		foreach (string text in array)
		{
			try
			{
				Uri uriResource = new Uri("pack://application:,,,/Assets/" + text);
				Stream stream = Application.GetResourceStream(uriResource)?.Stream;
				if (stream == null)
				{
					continue;
				}
				BitmapImage bitmapImage = new BitmapImage();
				bitmapImage.BeginInit();
				bitmapImage.StreamSource = stream;
				bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
				bitmapImage.DecodePixelWidth = 400;
				bitmapImage.EndInit();
				((Freezable)bitmapImage).Freeze();
				MascotImage.Source = bitmapImage;
				return;
			}
			catch
			{
			}
		}
		string path = System.IO.Path.GetDirectoryName(Environment.ProcessPath) ?? AppContext.BaseDirectory;
		string[] array2 = new string[3] { "mascot.png", "mascot.jpg", "mascot.webp" };
		foreach (string path2 in array2)
		{
			string text2 = System.IO.Path.Combine(path, "Assets", path2);
			if (File.Exists(text2))
			{
				try
				{
					BitmapImage bitmapImage2 = new BitmapImage();
					bitmapImage2.BeginInit();
					bitmapImage2.UriSource = new Uri(text2, UriKind.Absolute);
					bitmapImage2.CacheOption = BitmapCacheOption.OnLoad;
					bitmapImage2.DecodePixelWidth = 400;
					bitmapImage2.EndInit();
					MascotImage.Source = bitmapImage2;
					break;
				}
				catch
				{
				}
			}
		}
	}

	private void ShowMascot_Click(object sender, MouseButtonEventArgs e)
	{
		if (MascotImage.Source != null)
		{
			MascotOverlayImage.Source = MascotImage.Source;
			MascotOverlay.Visibility = Visibility.Visible;
			e.Handled = true;
		}
	}

	private void HideMascot_Click(object sender, MouseButtonEventArgs e)
	{
		MascotOverlay.Visibility = Visibility.Collapsed;
		e.Handled = true;
	}

	private void Window_MouseDown(object sender, MouseButtonEventArgs e)
	{
		if (e.ChangedButton == MouseButton.Left && e.LeftButton == MouseButtonState.Pressed)
		{
			try
			{
				DragMove();
			}
			catch
			{
			}
		}
	}

	private void Close_Click(object sender, RoutedEventArgs e)
	{
		Close();
	}

	private void Blog_Click(object sender, RoutedEventArgs e)
	{
		try
		{
			Process.Start(new ProcessStartInfo("https://www.swarchitect.net")
			{
				UseShellExecute = true
			});
		}
		catch
		{
		}
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "10.0.5.0")]
	public void InitializeComponent()
	{
		if (!_contentLoaded)
		{
			_contentLoaded = true;
			Uri resourceLocator = new Uri("/AxCopilot;component/views/aboutwindow.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:
			((AboutWindow)target).MouseDown += Window_MouseDown;
			break;
		case 2:
			((Button)target).Click += Close_Click;
			break;
		case 3:
			DiamondIconCanvas = (Canvas)target;
			break;
		case 4:
			AppIconImage = (Image)target;
			break;
		case 5:
			FallbackIcon = (TextBlock)target;
			break;
		case 6:
			MascotImage = (Image)target;
			break;
		case 7:
			AppNameText = (TextBlock)target;
			break;
		case 8:
			VersionText = (TextBlock)target;
			break;
		case 9:
			MascotOverlay = (Grid)target;
			break;
		case 10:
			((Rectangle)target).MouseDown += HideMascot_Click;
			break;
		case 11:
			MascotOverlayImage = (Image)target;
			break;
		case 12:
			PurposeText = (TextBlock)target;
			break;
		case 13:
			((Grid)target).MouseDown += ShowMascot_Click;
			break;
		case 14:
			CompanyNameText = (TextBlock)target;
			break;
		case 15:
			BlogLinkBtn = (Button)target;
			BlogLinkBtn.Click += Blog_Click;
			break;
		case 16:
			ContributorsGrid = (Grid)target;
			break;
		case 17:
			ContributorsText = (TextBlock)target;
			break;
		case 18:
			BuildInfoText = (TextBlock)target;
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}
