using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls;

namespace AgApp
{
	public partial class PageIndex : UserControl
	{
		public static ChosenLocalizationManager localizationManager = ChosenLocalizationManager.None;
		public static ChosenTheme theme = ChosenTheme.OfficeBlack;

		public PageIndex()
		{
			InitializeComponent();
			this.Loaded += new RoutedEventHandler(Page_Loaded);
		}

		void Page_Loaded(object sender, RoutedEventArgs e)
		{
			this.themeVista.IsChecked = (theme == ChosenTheme.Vista);
			this.themeSummer.IsChecked = (theme == ChosenTheme.Summer);
			this.themeOfficeBlack.IsChecked = (theme == ChosenTheme.OfficeBlack);
			this.managerNone.IsChecked = (localizationManager == ChosenLocalizationManager.None);
			this.managerResourceDef.IsChecked = (localizationManager == ChosenLocalizationManager.ResourceDef);
			this.managerResourceEN.IsChecked = (localizationManager == ChosenLocalizationManager.ResourceEN);
			this.managerResourceDE.IsChecked = (localizationManager == ChosenLocalizationManager.ResourceDE);
			this.managerResourceJP.IsChecked = (localizationManager == ChosenLocalizationManager.ResourceJP);
			this.managerGetStringOverride.IsChecked = (localizationManager == ChosenLocalizationManager.GetStringOverride);
			this.InitializeLocalizationManager();
		}

		private void managerGroup_Click(object sender, RoutedEventArgs e)
		{
			this.InitializeLocalizationManager();
		}

		private void cultureGroup_Click(object sender, RoutedEventArgs e)
		{
			this.InitializeLocalizationManager();
		}

		private void InitializeLocalizationManager()
		{
			Thread.CurrentThread.CurrentUICulture = this.GetUICulture();
			LocalizationManager.Manager = this.GetLocalizationManager();
			this.openTestPage.Content = LocalizationManager.GetString("DemoBtnOpenTestPage")
				?? "Open test page";
		}

		private void openTestPage_Click(object sender, RoutedEventArgs e)
		{
			PageIndex.theme = this.GetThemeType();
			PageIndex.localizationManager = this.GetLocalizationManagerType();
			NavigationHelper.LoadPageTestCombo();
		}

		internal LocalizationManager GetLocalizationManager()
		{
			LocalizationManager locMan = null;
			switch (this.GetLocalizationManagerType())
			{
				case PageIndex.ChosenLocalizationManager.ResourceDef:
					locMan = new LocalizationManager()
					{
						ResourceManager = AgApp.Properties.Resources.ResourceManager,
						Culture = null
					};
					break;
				case PageIndex.ChosenLocalizationManager.ResourceDE:
					locMan = new LocalizationManager()
					{
						ResourceManager = AgApp.Properties.Resources.ResourceManager,
						Culture = new CultureInfo("de")
					};
					break;
				case PageIndex.ChosenLocalizationManager.ResourceEN:
					locMan = new LocalizationManager()
					{
						ResourceManager = AgApp.Properties.Resources.ResourceManager,
						Culture = new CultureInfo("en")
					};
					break;
				case PageIndex.ChosenLocalizationManager.ResourceJP:
					locMan = new LocalizationManager()
					{
						ResourceManager = AgApp.Properties.Resources.ResourceManager,
						Culture = new CultureInfo("ja")
					};
					break;
				case PageIndex.ChosenLocalizationManager.GetStringOverride:
					locMan = new MyLocalizationManager();
					break;
			}
			return locMan;
		}

		internal CultureInfo GetUICulture()
		{
			return (this.cultureEN.IsChecked == true) ? new CultureInfo("en")
				: (this.cultureDE.IsChecked == true) ? new CultureInfo("de")
				: (this.cultureJp.IsChecked == true) ? new CultureInfo("ja")
				: Thread.CurrentThread.CurrentCulture;
		}

		internal ChosenTheme GetThemeType()
		{
			return (this.themeVista.IsChecked == true) ? ChosenTheme.Vista
				: (this.themeSummer.IsChecked == true) ? ChosenTheme.Summer
				: (this.themeOfficeBlack.IsChecked == true) ? ChosenTheme.OfficeBlack
				: ChosenTheme.BuiltIn;
		}

		internal ChosenLocalizationManager GetLocalizationManagerType()
		{
			return (this.managerResourceEN.IsChecked == true) ? ChosenLocalizationManager.ResourceEN
				: (this.managerResourceDE.IsChecked == true) ? ChosenLocalizationManager.ResourceDE
				: (this.managerResourceDef.IsChecked == true) ? ChosenLocalizationManager.ResourceDef
				: (this.managerResourceJP.IsChecked == true) ? ChosenLocalizationManager.ResourceJP
				: (this.managerGetStringOverride.IsChecked == true) ? ChosenLocalizationManager.GetStringOverride
				: ChosenLocalizationManager.None;
		}

		public enum ChosenLocalizationManager
		{
			None, ResourceDef, ResourceEN, ResourceDE, ResourceJP, GetStringOverride
		}

		public enum ChosenTheme
		{
			BuiltIn, Vista, Summer, OfficeBlack
		}
	}
}
