using System.Collections.Generic;
using System.Linq;
using JXCMS.CMS.Movie.Entity;
using JXCMS.Core.Extensions;

namespace JXCMS.CMS.Movie.Utils
{
    public static class Classify
    {
        public static List<ClassifyEntity> FindAllChildren(List<ClassifyEntity> classifyEntities, int baseId, bool includeSelf = true)
        {
            var returnValue = new List<ClassifyEntity>();
            if (includeSelf)
            {
                returnValue.AddRange(classifyEntities.Where(x => x.Id == baseId));
            }
            var classifyEntityList = classifyEntities.Where(x => x.ParentId == baseId).ToList();
            returnValue.AddRange(classifyEntityList);
            foreach (var classifyEntity in classifyEntityList)
            {
                returnValue.AddRange(FindAllChildren(classifyEntities, classifyEntity.Id, false));
            }
            return returnValue;
        }

        public static List<ClassifyEntity> FindAllNotChildren(List<ClassifyEntity> classifyEntities, int baseId)
        {
            var children = FindAllChildren(classifyEntities, baseId, true);
            return classifyEntities.Where(x => !children.Contains(x)).ToList();
        }

        public static List<ClassifyEntity> Structured(List<ClassifyEntity> classifyEntities, int baseId = 0, int hierarchy = 0)
        {
            var returnValue = new List<ClassifyEntity>();
            var classifyEntityList = classifyEntities.Where(x => x.ParentId == baseId).ToList();
            foreach (var classifyEntity in classifyEntityList)
            {
                classifyEntity.Name = classifyEntity.Name.InsertMultipleString("-", hierarchy * 2);
                returnValue.Add(classifyEntity);
                returnValue.AddRange(Structured(classifyEntities, classifyEntity.Id, hierarchy + 1));
            }

            return returnValue;
        }
    }
}