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 static class SyllabusService
    {
        /// <summary>
        /// 根据id查询列表串联id pid的新的关系列表
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="pid"></param>
        /// <param name="newNodes"></param>
        /// <returns></returns>
        public static HashSet<Tnode> GetNewNode(List<Tnode> nodes, string pid, HashSet<Tnode> newNodes) {
            bool flag = false;
            string spid = "";
            foreach (var node in nodes) {
                if (node.pid == pid) {
                    newNodes.Add(node);
                    spid = node.id;
                    GetNewNode(nodes, spid, newNodes);
                    flag = true;
                }
            }
            return newNodes;
            //if (flag)
            //{
            //    return GetNewNode(nodes, spid, newNodes);
            //}
            //else {
            //    return newNodes;
            //}
        }


        public static List<Tnode> TreeToList(List<SyllabusTree> trees, List<Tnode> nodes)
        {
            int index = 0;
            foreach (SyllabusTree 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,
                    updateTime=x.updateTime
                };
                list.Add(node);
            });
            nodes.AddRange(list);

            foreach (SyllabusTree tree in trees)
            {
                if (null != tree.children && tree.children.Count > 0)
                {
                    TreeToList(tree.children, nodes);
                }
            }
            return nodes;
        }
        public static List<SyllabusTree> ListToTree(List<Tnode> noes)
        {
            List<SyllabusTree> list = noes.ToJsonString().ToObject<List<SyllabusTree>>();
            //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, SyllabusTree> blockDict = new Dictionary<string, SyllabusTree>();
            foreach (var s in res)
            {
                blockDict.TryAdd(s.First().id, s.First());
            }
            return GetChild(list, blockDict);
        }

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