using Krypton.Toolkit;
using System.Reflection;

namespace System
{
    public static class KryptonListItemConvertor
    {
        public static KryptonListItem[] ToKryptonListItem<T>(IList<T> list, string display, string value)
        {
            if (list == null || list.Count == 0) return null;

            List<KryptonListItem> l = new List<KryptonListItem>();
            KryptonListItem kryptonListItem;
            Type elementType = typeof(T);

            int i = 0;
            Dictionary<int, object> keyValues = new Dictionary<int, object>();
            foreach (T item in list)
            {
                kryptonListItem = new KryptonListItem();

                foreach (PropertyInfo property in elementType.GetProperties())
                {
                    if (property.Name == display)
                    {
                        kryptonListItem.ShortText = property.GetValue(item, null).ToString();
                        l.Add(kryptonListItem);
                    }
                    if (property.Name == value)
                    {
                        keyValues.Add(i, property.GetValue(item, null));
                    }
                }

                kryptonListItem.Tag = keyValues;

                i++;
            }

            return l.ToArray();
        }

        public static KryptonListItem[] ToKryptonListItem<T>(T[] list, string display, string value)
        {
            if (list == null || list.Length == 0) return null;

            return ToKryptonListItem<T>(list.ToList(), display, value);
        }
    }
}
