using HTEXLib.COMM.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK.Models;
using TEAMModelOS.SDK.Models.Cosmos.Common;


namespace TEAMModelOS.Services.Common
{
    public class AbilityService
    {
        public static List<Tnode> TreeToList(List<AbilityTaskTree> trees, List<Tnode> nodes, long now)
        {
            int index = 0;
            foreach (AbilityTaskTree tr in trees)
            {
                tr.order = index;
                index++;
            }
            trees = trees.OrderBy(x => x.order).ToList();
            List<Tnode> list = new List<Tnode>();
            //var list = trees.ToJsonString().ToObject<List<Tnode>>();
            trees.ForEach(x => {
                List<string> cids = new List<string>();
                if (x.children.IsNotEmpty())
                {
                    x.children.ForEach(y => cids.Add(y.id));
                }
                var node = new Tnode
                {
                    title = x.title,
                    id = x.id,
                    pid = x.pid,
                    order = x.order,
                    rnodes = x.rnodes,
                    cids = cids,
                    creatorId = x.creatorId,
                    creatorName = x.creatorName,
                    updateTime = now
                };
                list.Add(node);
            });
            nodes.AddRange(list);

            foreach (AbilityTaskTree tree in trees)
            {
                if (null != tree.children && tree.children.Count > 0)
                {
                    TreeToList(tree.children, nodes, now);
                }
            }
            return nodes;
        }

        public static List<AbilityTaskTree> ListToTree(List<Tnode> noes)
        {
            List<AbilityTaskTree> list = noes.ToJsonString().ToObject<List<AbilityTaskTree>>();
            //var lookup = list.ToDictionary(n => n.RowKey, n => n);

            var res = from r in list group r by r.id into g select g;
            Dictionary<string, AbilityTaskTree> blockDict = new Dictionary<string, AbilityTaskTree>();
            foreach (var s in res)
            {
                blockDict.TryAdd(s.First().id, s.First());
            }
            return GetChild(list, blockDict);
        }

        private static List<AbilityTaskTree> GetChild(List<AbilityTaskTree> list, Dictionary<string, AbilityTaskTree> dict)
        {
            //  list = list.OrderBy(m => m.Order).ToList();
            List<AbilityTaskTree> trees = new List<AbilityTaskTree>();
            trees = trees.OrderBy(x => x.order).ToList();
            foreach (AbilityTaskTree node in list)
            {
                bool flag = dict.TryGetValue(node.pid, out AbilityTaskTree ability);
                if (flag && ability != null)
                {
                    ability.children.Add(node);
                }
                else
                {
                    trees.Add(node);
                }
            }
            return trees;
        }
    }
}
