using System;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Practices.Common;
using System.ComponentModel.Design;
using Microsoft.Practices.RecipeFramework.Extensions.Dialogs;
using Microsoft.Practices.ComponentModel;

namespace Microsoft.Practices.RecipeFramework.Extensions.Editors
{
	/// <summary>
	/// Picks a Windows user account.
	/// </summary>
    [ServiceDependency(typeof(IUIService))]
    public class WindowsUserNamePickerEditor : UITypeEditor
    {
		/// <summary>
		/// Gets the editor style used by the <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)"></see> method.
		/// </summary>
		/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
		/// <returns>
		/// A <see cref="T:System.Drawing.Design.UITypeEditorEditStyle"></see> value that indicates the style of editor used by the <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)"></see> method. If the <see cref="T:System.Drawing.Design.UITypeEditor"></see> does not support this method, then <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> will return <see cref="F:System.Drawing.Design.UITypeEditorEditStyle.None"></see>.
		/// </returns>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

		/// <summary>
		/// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> method.
		/// </summary>
		/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
		/// <param name="provider">An <see cref="T:System.IServiceProvider"></see> that this editor can use to obtain services.</param>
		/// <param name="value">The object to edit.</param>
		/// <returns>
		/// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
		/// </returns>
        public override object EditValue(ITypeDescriptorContext context, 
            IServiceProvider provider, object value)
        {
			IUIService svc = (IUIService)provider.GetService(typeof(IUIService));

			using (DsObjectPickerDialog userNamePicker = new DsObjectPickerDialog())
			{
				userNamePicker.InitialScopes = DsObjectPickerInitialScopes.DefaultFilterUsers;
				if (userNamePicker.ShowDialog(svc.GetDialogOwnerWindow()) == DialogResult.OK && 
					userNamePicker.SelectedObjects.Count > 0)
				{
					return userNamePicker.SelectedObjects[0].Upn;
				}
				else
				{
					return null;
				}
			}
        }
	}
}
