using Infragistics.Win;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Common
{
    public static class ExtendCls
    {
        public static ValueList ListToValuelist<T>(this List<T> items){

            ValueList vlistScale = new ValueList();
            vlistScale.ValueListItems.Add("", "全部");
            vlistScale.ValueListItems.Add("0", "无效");
            vlistScale.ValueListItems.Add("1", "未使用");
            vlistScale.ValueListItems.Add("2", "使用中");
            vlistScale.ValueListItems.Add("3", "已完成");

            return vlistScale;
        }

        #region List转dataTable
        public static DataTable ListToDataTable<T>(this List<T> items)
        {
            DataTable dataTable = new DataTable();
            try
            {
                PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
                foreach (PropertyInfo prop in Props)
                {
                    dataTable.Columns.Add(prop.Name);
                }
                foreach (T obj in items)
                {
                    var values = new object[Props.Length];
                    for (int i = 0; i < Props.Length; i++)
                    {
                        values[i] = Props[i].GetValue(obj, null);
                    }
                    dataTable.Rows.Add(values);
                }
                return dataTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show("公共方法ListToDataTable报错！错误内容：\r\n" + ex.Message);
                return dataTable;
            }
            
        }
        #endregion

        #region dataTable转List
        public static List<T> TableToDataList<T>(this DataTable table)
        {
            if (table == null)
            {
                return null;
            }

            List<DataRow> rows = new List<DataRow>();
            foreach (DataRow row in table.Rows)
            {
                rows.Add(row);
            }

            return ConvertTo<T>(rows);
        }

        private static List<T> ConvertTo<T>(List<DataRow> rows)
        {
            List<T> list = null;

            if (rows != null)
            {
                list = new List<T>();

                foreach (DataRow row in rows)
                {
                    T item = row.CreateItem<T>();//CreateItem<T>(row);
                    list.Add(item);
                }
            }

            return list;
        }

        public static T CreateItem<T>(this DataRow row)
        {
            T obj = default(T);
            try
            {
                if (row != null)
                {
                    obj = Activator.CreateInstance<T>();

                    foreach (DataColumn column in row.Table.Columns)
                    {
                        PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);

                        object value = row[column.ColumnName];
                        if (value.ToString() != "")
                        {
                            if (prop.PropertyType.FullName == typeof(string).FullName)
                            {
                                prop.SetValue(obj, value.ToString(), null);
                            }
                            else if (prop.PropertyType.FullName == typeof(DateTime?).FullName)
                            {
                                prop.SetValue(obj, (DateTime?)Convert.ToDateTime(value.ToString()), null);
                            }
                            else if (prop.PropertyType.FullName == typeof(int?).FullName)
                            {
                                prop.SetValue(obj, (int?)Convert.ToInt32(value.ToString()), null);
                            }
                            else if (prop.PropertyType.FullName == typeof(long?).FullName)
                            {
                                prop.SetValue(obj, (long?)Convert.ToInt32(value.ToString()), null);
                            }
                            else if (prop.PropertyType.FullName == typeof(double?).FullName)
                            {
                                prop.SetValue(obj, (double?)Convert.ToDouble(value.ToString()), null);
                            }
                            else if (prop.PropertyType.FullName == typeof(bool).FullName)
                            {
                                prop.SetValue(obj, Convert.ToBoolean(value.ToString()), null);
                            }
                            else if (prop.PropertyType.FullName == typeof(decimal?).FullName)
                            {
                                prop.SetValue(obj, Convert.ToDecimal(value.ToString()), null);
                            }
                            //prop.SetValue(obj, value, null);
                        }
                    }

                }
            }
            catch (Exception ex)
            {  //You can log something here     
               //throw;    
            } 
            return obj;
        }
        #endregion
    }
}
