﻿namespace Epikem.Macro1.ViewModels
{
	using Catel.Data;
	using Catel.MVVM;
	using Epikem.Macro1.Models;
	using Epikem.Macro1.Services;
	using System;
	using System.Collections.Generic;
	using System.Diagnostics;
	using System.Windows;
	using Tomers.WPF.Themes.Skins;


	/// <summary>
	/// Settings view model.
	/// </summary>
	public class SettingsWindowViewModel : ViewModelBase
	{
		#region Fields
		IMacroService macroService;
		#endregion

		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="SettingsWindowViewModel"/> class.
		/// </summary>
		public SettingsWindowViewModel(IMacroService macroService)
		{
			InitializeSkins();
			this.macroService = macroService;
			this.Settings = macroService.Settings;
			CurrentSkin = this.Settings.CurrentSkin;
		}

		#endregion

		#region Properties
		/// <summary>
		/// Gets the title of the view model.
		/// </summary>
		/// <value>The title.</value>
		public override string Title { get { return "View model title"; } }

		// TODO: Register models with the vmpropmodel codesnippet
		// TODO: Register view model properties with the vmprop or vmpropviewmodeltomodel codesnippets

		/// <summary>
		/// Gets or sets the Settings.
		/// </summary>
		[Model]
		public Settings Settings
		{
			get { return GetValue<Settings>(SettingsProperty); }
			private set { SetValue(SettingsProperty, value); }
		}

		/// <summary>
		/// Register the Settings property so it is known in the class.
		/// </summary>
		public static readonly PropertyData SettingsProperty = RegisterProperty("Settings", typeof(Settings));

		/// <summary>
		/// Gets or sets the FileLocation for file loading..
		/// </summary>
		[ViewModelToModel("Settings")]
		public string FileLocation
		{
			get { return GetValue<string>(FileLocationProperty); }
			set { SetValue(FileLocationProperty, value); }
		}

		/// <summary>
		/// Register the FileLocation property so it is known in the class.
		/// </summary>
		public static readonly PropertyData FileLocationProperty = RegisterProperty("FileLocation", typeof(string), () => 
		{ 
			return Epikem.Macro1.Settings1.Default.FileLocation;
		});

		/// <summary>
		/// Gets or sets the Skins.
		/// </summary>
		public List<Skin> Skins
		{
			get { return GetValue<List<Skin>>(SkinsProperty); }
			set { SetValue(SkinsProperty, value); }
		}

		/// <summary>
		/// Register the Skins property so it is known in the class.
		/// </summary>
		public static readonly PropertyData SkinsProperty = RegisterProperty("Skins", typeof(List<Skin>), new List<Skin>());


		/// <summary>
		/// Gets or sets the CurrentSkin.
		/// </summary>
		[ViewModelToModel("Settings")]
		public Skin CurrentSkin
		{
			get { return GetValue<Skin>(CurrentSkinProperty); }
			set { SetValue(CurrentSkinProperty, value); }
		}

		/// <summary>
		/// Register the CurrentSkin property so it is known in the class.
		/// </summary>
		public static readonly PropertyData CurrentSkinProperty = RegisterProperty("CurrentSkin", typeof(Skin));


		#endregion

		#region Commands
		#endregion

		#region Methods

		protected override System.Threading.Tasks.Task Close()
		{

			return base.Close();
		}

		private void InitializeSkins()
		{
			this.Skins = Skin.Skins;

		}



		private static object OnCoerceSkinValue(DependencyObject d, object baseValue)
		{
			try
			{
				if (baseValue == null)
				{
					return Skin.Null;
				}
				return baseValue;
			}
			catch (Exception ex)
			{
				Debug.WriteLine("Coerce error: " + ex.Message);
				throw;
			}
		}		
		#endregion
	}
}
