using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Media;
using Microsoft.Win32;
using Wpf.Ui.Controls;

namespace Crysome.Server.View;

public class LanguageWindow : FluentWindow, IComponentConnector
{
	private static readonly string SavedLangPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "language.dat");

	internal TitleBar TitleBarControl;

	internal TextBlock SelectLabel;

	internal ComboBox LanguageComboBox;

	internal Button ContinueButton;

	private bool _contentLoaded;

	public string SelectedCultureTag { get; private set; } = "en";

	public LanguageWindow()
	{
		InitializeComponent();
		ApplyBackdrop();
		PreSelectSavedLanguage();
	}

	private void PreSelectSavedLanguage()
	{
		string text = LoadSavedLanguage();
		foreach (ComboBoxItem item in (IEnumerable)LanguageComboBox.Items)
		{
			if (item.Tag as string == text)
			{
				LanguageComboBox.SelectedItem = item;
				return;
			}
		}
		LanguageComboBox.SelectedIndex = 0;
	}

	private static string LoadSavedLanguage()
	{
		try
		{
			if (File.Exists(SavedLangPath))
			{
				string text = File.ReadAllText(SavedLangPath)?.Trim();
				if (!string.IsNullOrEmpty(text))
				{
					return text;
				}
			}
		}
		catch
		{
		}
		return "en";
	}

	private void SaveLanguage(string tag)
	{
		try
		{
			File.WriteAllText(SavedLangPath, tag);
		}
		catch
		{
		}
	}

	private void ContinueButton_Click(object sender, RoutedEventArgs e)
	{
		if (LanguageComboBox.SelectedItem is ComboBoxItem { Tag: string tag })
		{
			SelectedCultureTag = tag;
			SaveLanguage(tag);
		}
		((Window)this).DialogResult = true;
		((Window)this).Close();
	}

	private void ApplyBackdrop()
	{
		if (IsWindows11OrNewer())
		{
			((FluentWindow)this).ExtendsContentIntoTitleBar = true;
			((FluentWindow)this).WindowBackdropType = (WindowBackdropType)2;
		}
		else
		{
			((Control)this).Background = new SolidColorBrush(Color.FromRgb(32, 32, 32));
		}
	}

	private static bool IsWindows11OrNewer()
	{
		try
		{
			using RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
			if (registryKey?.GetValue("CurrentBuild") is string s && int.TryParse(s, out var result))
			{
				return result >= 22000;
			}
		}
		catch
		{
		}
		return false;
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "9.0.14.0")]
	public void InitializeComponent()
	{
		if (!_contentLoaded)
		{
			_contentLoaded = true;
			Uri resourceLocator = new Uri("/Crysome.Server;component/view/languagewindow.xaml", UriKind.Relative);
			Application.LoadComponent(this, resourceLocator);
		}
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "9.0.14.0")]
	[EditorBrowsable(EditorBrowsableState.Never)]
	void IComponentConnector.Connect(int connectionId, object target)
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Expected O, but got Unknown
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Expected O, but got Unknown
		switch (connectionId)
		{
		case 1:
			TitleBarControl = (TitleBar)target;
			break;
		case 2:
			SelectLabel = (TextBlock)target;
			break;
		case 3:
			LanguageComboBox = (ComboBox)target;
			break;
		case 4:
			ContinueButton = (Button)target;
			((ButtonBase)(object)ContinueButton).Click += ContinueButton_Click;
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}
