#region COPYRIGHT
//
//     THIS IS GENERATED BY TEMPLATE
//     
//     AUTHOR  :     ROYE
//     DATE       :     2010
//
//     COPYRIGHT (C) 2010, TIANXIAHOTEL TECHNOLOGIES CO., LTD. ALL RIGHTS RESERVED.
//
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;

namespace System.Windows.Forms
{
    public class ListSelection<T> : List<ObjectSelection<T>>
    {
        private IEnumerable _Source;

        public ListSelection(IEnumerable source)
            : this(source, false)
        {
        }

        public ListSelection(IEnumerable source, string usePropertyAsDisplayName)
            : this(source, false, usePropertyAsDisplayName)
        {
        }

        public ListSelection(IEnumerable source, bool showCounts, string usePropertyAsDisplayName)
            : this(source, showCounts)
        {
            DisplayNameProperty = usePropertyAsDisplayName;
        }

        public ListSelection(IEnumerable source, bool showCounts)
            : base()
        {
            _Source = source;
            ShowCounts = showCounts;
            if (_Source is IBindingList)
            {
                ((IBindingList)_Source).ListChanged += new ListChangedEventHandler(ListSelection_ListChanged);
            }
            Populate();
        }

        private void ListSelection_ListChanged(object sender, ListChangedEventArgs e)
        {
            switch (e.ListChangedType)
            {
                case ListChangedType.ItemAdded:
                    Add(CreateSelection((IEnumerator)((IBindingList)_Source)[e.NewIndex]));
                    break;
                case ListChangedType.ItemDeleted:
                    Remove(FindObjectWithItem((T)((IBindingList)_Source)[e.OldIndex]));
                    break;
                case ListChangedType.Reset:
                    Populate();
                    break;
            }
        }

        private ObjectSelection<T> CreateSelection(IEnumerator enumerator)
        {
            return (ObjectSelection<T>)typeof(ObjectSelection<T>).CreateInstance(enumerator.Current, this);
        }

        public ObjectSelection<T> FindObjectWithItem(T item)
        {
            return Find(p => p.Item.Equals(item));
        }

        private void Populate()
        {
            Clear();
            IEnumerator Enumerator = _Source.GetEnumerator();
            if (Enumerator != null)
            {
                while (Enumerator.MoveNext())
                {
                    Add(CreateSelection(Enumerator));
                }
            }
        }

        public void ClearCounts()
        {
            foreach (ObjectSelection<T> Item in this)
            {
                Item.Count = 0;
            }
        }

        public string DisplayNameProperty { get; set; }

        public bool ShowCounts { get; set; }

        public string SelectedNames
        {
            get
            {
                return string.Join(",", this.FindAll(p => p.Selected == true).ConvertAll<string>(p => p.Name).ToArray());
                //string text = string.Empty;
                //foreach (ObjectSelection<T> item in this)
                //{
                //    if (item.Selected)
                //    {
                //        text += (string.IsNullOrEmpty(text) ?
                //            String.Format("{0}", item.Name) :
                //            String.Format(" , {0}", item.Name));
                //    }
                //}
                //return text;
            }
        }
    }
}
