using System;
using System.Collections;
using System.Drawing.Design;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace CodeSmith.CustomProperties
{
    public class NameValueCollectionEditor : UITypeEditor
    {
        private IWindowsFormsEditorService editorService = null;
        private NameValueCollectionEditorUI _nameValueCollectionEditorUI = null;

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (editorService != null)
                {
                    if (_nameValueCollectionEditorUI == null)
                        _nameValueCollectionEditorUI = new NameValueCollectionEditorUI();
                    _nameValueCollectionEditorUI.Start(editorService, value);
                    editorService.ShowDialog(_nameValueCollectionEditorUI);
                    if (_nameValueCollectionEditorUI.DialogResult == DialogResult.OK)
                    {
                        value = _nameValueCollectionEditorUI.NameValueCollection;
                    }
                    _nameValueCollectionEditorUI.End();
                }
            }

            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }
}
