﻿using System.Windows;

namespace Epikem.Macro1
{
	using System.Windows;
	using System.Data;
	using System.Linq;
	using Catel.Windows;
	using Catel.IoC;
	using Catel.Services;
	using Epikem.Macro1.ViewModels;
	using Epikem.Macro1.Views;
	using Epikem.Macro1.Services;
	using System;
	using System.Collections.Generic;

	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : Application
	{
		/// <summary>
		/// Raises the <see cref="E:System.Windows.Application.Startup"/> event.
		/// </summary>
		/// <param name="e">A <see cref="T:System.Windows.StartupEventArgs"/> that contains the event data.</param>
		protected override void OnStartup(StartupEventArgs e)
		{

//#if DEBUG
//			Catel.Logging.LogManager.RegisterDebugListener();
//#endif

			//StyleHelper.CreateStyleForwardersForDefaultStyles();
			
			// TODO: Using a custom IoC container like Unity? Register it here:
			// Catel.IoC.ServiceLocator.Instance.RegisterExternalContainer(MyUnityContainer);

			var serviceLocator = ServiceLocator.Default;
			serviceLocator.RegisterType<IMacroService, MacroService>();
			
			//serviceLocator.RegisterType<IFamilyService, FamilyService>();

			var uiVisualizerService = serviceLocator.ResolveType<IUIVisualizerService>();
			//uiVisualizerService.Register(typeof(InputTestWindowViewModel), typeof(KeyPressViewModel), typeof(MacrosViewModel), typeof(MainWindowViewModel));
			uiVisualizerService.Register<MacroWindowViewModel, MacroWindow>();
			uiVisualizerService.Register<ActionViewModel, ActionView>();
			uiVisualizerService.Register(typeof(MainWindowViewModel), typeof(MainWindow));
			uiVisualizerService.Register<KeyPressWindowViewModel, KeyPressWindow>();
			uiVisualizerService.Register<MousePressWindowViewModel, MousePressWindow>();

			base.OnStartup(e);
		}


	}

	public class ThemeResourceDictionary : ResourceDictionary
	{
	}


	public class ThemeSelector : DependencyObject
	{

		public static readonly DependencyProperty CurrentThemeDictionaryProperty =
		 DependencyProperty.RegisterAttached("CurrentThemeDictionary", typeof(Uri),
		 typeof(ThemeSelector),
		 new UIPropertyMetadata(null, CurrentThemeDictionaryChanged));

		public static Uri GetCurrentThemeDictionary(DependencyObject obj)
		{
			return (Uri)obj.GetValue(CurrentThemeDictionaryProperty);
		}

		public static void SetCurrentThemeDictionary(DependencyObject obj, Uri value)
		{
			obj.SetValue(CurrentThemeDictionaryProperty, value);
		}

		private static void CurrentThemeDictionaryChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			if (obj is FrameworkElement) // works only on FrameworkElement objects
			{
				ApplyTheme(obj as FrameworkElement, GetCurrentThemeDictionary(obj));
			}
		}

		private static void ApplyTheme(FrameworkElement targetElement, Uri dictionaryUri)
		{
			if (targetElement == null) return;

			try
			{
				ThemeResourceDictionary themeDictionary = null;
				if (dictionaryUri != null)
				{
					themeDictionary = new ThemeResourceDictionary();
					themeDictionary.Source = dictionaryUri;

					// add the new dictionary to the collection of merged dictionaries of the target object
					targetElement.Resources.MergedDictionaries.Insert(0, themeDictionary);
				}

				// find if the target element already has a theme applied
				List<ThemeResourceDictionary> existingDictionaries =
						(from dictionary in targetElement.Resources.MergedDictionaries.OfType<ThemeResourceDictionary>()
						 select dictionary).ToList();

				// remove the existing dictionaries
				foreach (ThemeResourceDictionary thDictionary in existingDictionaries)
				{
					if (themeDictionary == thDictionary) continue;  // don't remove the newly added dictionary
					targetElement.Resources.MergedDictionaries.Remove(thDictionary);
				}
			}
			finally { }
		}

		public void ChangeToExpressionDarkTheme()
		{
			ThemeSelector.SetCurrentThemeDictionary(this, new Uri("pack://Application:,,,/SahaWpfTheme20132;component/Themes/ExpressionDark.xaml", UriKind.Relative));
		}
		public void ChangeToExpressionLightTheme()
		{
			ThemeSelector.SetCurrentThemeDictionary(this, new Uri("pack://Application:,,,/SahaWpfTheme20132;component/Themes/ExpressionLight.xaml", UriKind.Relative));
		}
	}
}