using GxPress.Service.Interface.Media;
using GxPress.Repository.Interface.Category;
using GxPress.Repository.Interface.Media;
using System.Threading.Tasks;
using GxPress.Result.Media;
using AutoMapper;
using System.Linq;
using GxPress.Result.Category;

namespace GxPress.Service.Implement.Media
{
    public class MediaService : IMediaService
    {
        private readonly ICategoryRepository categoryRepository;
        private readonly IMediaRepository mediaRepository;
        private readonly IMapper _mapper;
        public MediaService(ICategoryRepository categoryRepository, IMediaRepository mediaRepository, IMapper _mapper)
        {
            this.categoryRepository = categoryRepository;
            this.mediaRepository = mediaRepository;
            this._mapper = _mapper;
        }
        public async Task<bool> InsertAsync(MediaResult result)
        {

            // if (result.CategoryId > 0)
            // {
            //     result.CategoryName = await categoryRepository.GetCategoryParentAsync(result.CategoryId, result.CategoryName);
            // }
            return await mediaRepository.InsertAsync(result);
        }
        public async Task<MediaResult> GetAsync(int id)
        {
            var result = new MediaResult();
            //获取媒体
            result = await mediaRepository.GetAsync(id);
            //获取类别
            var categoryAll = await categoryRepository.GetAllAsync();
            result.CategoryResults = categoryAll.Where(n => n.ParentId == 0).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
            foreach (var item in result.CategoryResults)
            {
                item.IsChildren = categoryAll.Any(n => n.ParentId == item.Id);
                item.Children = categoryAll.Where(n => n.ParentId == item.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
                if (item.IsChildren)
                {
                    foreach (var chidren in item.Children)
                    {
                        chidren.IsChildren = categoryAll.Any(n => n.ParentId == chidren.Id);
                        chidren.Children = categoryAll.Where(n => n.ParentId == chidren.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
                    }
                }
            }
            return result;
        }

        // public List<CategoryResult> GetAllCategoryResult(IEnumerable<Entity.tede2.Category.Category> categorys, int id, List<CategoryResult> categoryResults)
        // {
        //     var result = new List<CategoryResult>();
        //     result = categorys.Where(n => n.ParentId == id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
        //     foreach (var item in result)
        //     {
        //         item.IsChildren = categorys.Any(n => n.ParentId == item.Id);
        //         item.Children = categorys.Where(n => n.ParentId == item.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
        //         foreach (var children in item.Children)
        //         {
        //             GetAllCategoryResult(categorys,children.Id,categoryResults);
        //         }
        //     }
        //     return result;
        // }
    }
}