using System.ComponentModel;
using Exam.Infrastructure.Data.Entity;
using Exam.Infrastructure.Extensions;
using Exam.Share.ViewResponses.Sourceware;
using Hotline.Application.Exam.Constants.Messages;
using Hotline.Application.Exam.Extensions;
using Hotline.Application.Exam.Interface.Sourcewares;
using Hotline.Application.Exam.QueryExtensions.Sourcewares;
using Hotline.Exams.Sourcewares;
using Hotline.Repository.SqlSugar;
using Hotline.Repository.SqlSugar.Exam.Interfaces.Sourcewares;
using Hotline.Repository.SqlSugar.Exam.Service;
using Hotline.Share.Dtos.Sourcewares;
using Hotline.Share.Requests.Sourceware;
using MapsterMapper;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;

namespace Hotline.Application.Exam.Service.Sourcewares
{
    /// <summary>
    /// 课件类型服务
    /// </summary>
    [Description("课件类型服务")]
    public class SourcewareCategoryService : ApiService<ExamSourcewareCategory, AddSourcewareCategoryDto,UpdateSourcewareCategoryDto,HotlineDbContext>,ISourcewareCategoryService, IScopeDependency
    {
        private readonly ISourcewareCategoryRepository _repository;
        private readonly IMapper _mapper;
        public SourcewareCategoryService(ISourcewareCategoryRepository repository,IMapper mapper) : base(repository,mapper)
        {
            _repository = repository;
            _mapper = mapper;
        }

        public async Task<SourcewareCategoryDto> GetAsync(EntityQueryRequest entityQueryRequest)
        {
            var entity = await _repository.GetAsync(entityQueryRequest.Id);

            return _mapper.Map<SourcewareCategoryDto>(entity);
        }

        public async Task<(int, List<SourcewareCategoryViewResponse>)> GetListAsync(SourcewareCategoryRequest queryRequest)
        {
            var expression = queryRequest.GetExpression();
            var query = _repository.Queryable().Where(expression);

            var result = await query.Select(m => new SourcewareCategoryViewResponse { 
                				Name = m.Name,
								ParentId = m.ParentId,
								Id = m.Id,
				            }).ToListAsync();

            var total = await query.CountAsync();

            return (total,result);
        }

        public async Task<PageViewResponse<SourcewareCategoryViewResponse>> GetPagedListAsync(SourcewareCategoryRequest queryRequest)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取树状
        /// </summary>
        /// <param name="sourcewareCategoryDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<SourcewareCategoryViewResponse>> GetTreeAsync(SourcewareCategoryRequest sourcewareCategoryRequest)
        {
            var expression = sourcewareCategoryRequest.GetExpression();

            var sourcewareCategorys = await _repository.GetTreeAsync(expression);

            var sourcewareCategoryViewResponses = sourcewareCategorys.Select(m => new SourcewareCategoryViewResponse
            {
                Id = m.Id,
                Name = m.Name,
                ParentId = m.ParentId,
                SortIndex = m.SortIndex,
                LastModificationTime = m.LastModificationTime,
                Children = _mapper.Map<List<SourcewareCategoryViewResponse>>(m.Children)
            }).ToList();


            return sourcewareCategoryViewResponses;
        }

        /// <summary>
        /// 删除课件分类
        /// </summary>
        /// <param name="entityQueryRequest"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override Task DeleteAsync(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
        {
            //TODO 如果分类拥有下级分类怎么不能删除

            if (_repository.CheckHasChild(entityQueryRequest))
            {
                throw new UserFriendlyException(BusinessExamErrorMessage.ServiceError, string.Format(BusinessExamErrorMessage.HasChild,typeof(ExamSourcewareCategory).GetDescription()));
            }

            return base.DeleteAsync(entityQueryRequest, cancellationToken);
        }
    }
}


