using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace CodeSmith.CustomProperties
{
    public partial class NameValueCollectionEditorUI : Form
    {
        private NameValueCollection _nameValueCollection = new NameValueCollection();

        public NameValueCollection NameValueCollection
        {
            get { return _nameValueCollection; }
        }

        public void Start(IWindowsFormsEditorService editorService, object value)
        {
            nameValueGridView.Rows.Clear();

            if (value is NameValueCollection)
            {
                _nameValueCollection = (NameValueCollection)value;
                foreach (NameValueEntry entry in _nameValueCollection)
                {
                    nameValueGridView.Rows.Add(entry.Key, entry.Value);
                }                
            }
            else
            {
                _nameValueCollection.Clear();               
            }
        }

        public void End()
        {

        }

        public NameValueCollectionEditorUI()
        {
            InitializeComponent();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            _nameValueCollection.Clear();
            foreach (DataGridViewRow row in nameValueGridView.Rows)
            {
                if (row.IsNewRow)
                    continue;

                _nameValueCollection.Add(
                    row.Cells["nameColumn"].Value.ToString(),
                    row.Cells["valueColumn"].Value.ToString());
            }
        }

    }
}