using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Houdar.Core.Util.Extension
{
    /// 集合扩展
    public static class CollectionExtension
    {
        /// 
        /// 确保某一类型的元素必须位于开始位置
        /// 
        /// 
        /// 
        /// 想要位于首位的元素的类型
        /// 调整后的集合
        /// 
        /// 本方法适用于要确认的元素的类型在集合中唯一的情况。
        /// 
        public static void EnsureFirstPosition(this IEnumerable source, Type checkedItem)
        {
            if (source == null)
            {
                return ;
            }
            var sourceArray = new List(source);
            var itemIndex = sourceArray.FindIndex(m => m.GetType() == checkedItem);
            if (itemIndex <= 0) return ;
            var changeItem = sourceArray[itemIndex];
            sourceArray.RemoveAt(itemIndex);
            sourceArray.Insert(0, changeItem);
            source= sourceArray;
        }
        /// 
        /// 确保某一类型的元素必须位于末尾位置
        /// 
        /// 
        /// 
        /// 想要位于首位的元素的类型
        /// 调整后的集合
        /// 
        /// 本方法适用于要确认的元素的类型在集合中唯一的情况。
        /// 
        public static void EnsureLastPosition(this IEnumerable source, Type checkedItem)
        {
            if (source == null)
            {
                return ;
            }
            var sourceArray = new List(source);
            var itemIndex = sourceArray.FindIndex(m => m.GetType() == checkedItem);
            if (itemIndex == sourceArray.Count-1) return ;
            var changeItem = sourceArray[itemIndex];
            sourceArray.RemoveAt(itemIndex);
            sourceArray.Insert(sourceArray.Count-1, changeItem);
            source = sourceArray;
        }
        /// 
        /// 集合转换
        /// 
        /// 对象类型
        /// 转换后的对象
        /// 对象
        /// 转换表达式
        /// 
        public static IEnumerable ConvertAll(this IEnumerable items,
            Converter transformation)
        {
            if (items == null) return null;
            var arr = items as T[];
            if (arr != null)
            {
                return Array.ConvertAll(arr, transformation);
            }
            var list = items as List;
            if (list != null)
            {
                return list.ConvertAll(transformation);
            }
            return items.Select(_ => transformation(_));
        }
        /// 
        ///     Func转Predicate
        /// 
        /// 
        /// 
        /// 
        public static Predicate ToPredicate(this Func source)
        {
            var result = new Predicate(source);
            return result;
        }
        /// 
        /// IEnumerable对象的轮询操作
        /// 
        /// 
        /// 
        /// 
        public static void ForEach(this IEnumerable items, Action action)
        {
            if (items == null)
            {
                return;
            }
            var arr = items as T[];
            if (arr != null)
            {
                Array.ForEach(arr, action);
                return;
            }
            var list = items as List;
            if (list != null)
            {
                list.ForEach(action);
                return;
            }
            foreach (var item in items)
            {
                action(item);
            }
        }
        /// 
        /// IEnumerable是否为空
        /// 
        /// 
        /// 
        public static bool IsNullOrEmpty(this IEnumerable targer)
        {
            return targer == null || targer.GetEnumerator().MoveNext() == false;
        }
        
        /// 
        ///     ICollection转换成数组T[]
        /// 
        public static T[] ToArray(this ICollection collection, int index = 0)
        {
            if (collection.IsNullOrEmpty()) return null;
            var arr = new T[collection.Count];
            collection.CopyTo(arr, index);
            return arr;
        }
        /// 把一个列表组合成为一个字符串,默认逗号分隔
        /// 
        /// 组合分隔符,默认逗号
        /// 
        public static string Join(this IEnumerable value, string separator = ",")
        {
            if (value == null) return null;
            var sb = new StringBuilder();
            foreach (var item in value)
            {
                sb.Separate(separator).Append(item + "");
            }
            return sb.ToString();
        }
        /// 把一个列表组合成为一个字符串,默认逗号分隔
        /// 
        /// 组合分隔符,默认逗号
        /// 把对象转为字符串的委托
        /// 
        public static string Join(this IEnumerable value, string separator = ",", Func func = null)
        {
            if (value == null) return null;
            var sb = new StringBuilder();
            if (func == null) func = obj => obj + "";
            foreach (var item in value)
            {
                sb.Separate(separator).Append(func(item));
            }
            return sb.ToString();
        }
        /// 
        /// 在考虑依赖关系的情况下,使用托普法进行排序
        /// 
        /// 要排序的类型.
        /// 要排序的数据
        /// 依赖项查询
        /// 
        public static List SortByDependencies(this IEnumerable source, Func> getDependencies)
        {
            var sorted = new List();
            var visited = new Dictionary();
            foreach (var item in source)
            {
                SortByDependenciesVisit(item, getDependencies, sorted, visited);
            }
            return sorted;
        }
       
        private static void SortByDependenciesVisit(T item, Func> getDependencies, List sorted, Dictionary visited)
        {
            bool inProcess;
            var alreadyVisited = visited.TryGetValue(item, out inProcess);
            if (alreadyVisited)
            {
                if (inProcess)
                {
                    throw new ArgumentException("Cyclic dependency found!");
                }
            }
            else
            {
                visited[item] = true;
                var dependencies = getDependencies(item);
                if (dependencies != null)
                {
                    foreach (var dependency in dependencies)
                    {
                        SortByDependenciesVisit(dependency, getDependencies, sorted, visited);
                    }
                }
                visited[item] = false;
                sorted.Add(item);
            }
        }
    }
}