using GxPress.Repository.Interface.SystemRoleMenus;
using GxPress.Service.Interface.Role;
using GxPress.Repository.Interface.SystemRole;
using GxPress.Repository.Interface.Menus;
using System.Threading.Tasks;
using GxPress.Request.Role;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using AutoMapper;
using GxPress.Request.Menu;
using GxPress.Result.SystemRole;

namespace GxPress.Service.Implement.Role
{
    public class RoleService : IRoleService
    {
        private readonly ISystemRoleMenusRepository systemRoleMenusRepository;
        private readonly IMenusRepository menusRepository;

        private readonly ISystemRoleRepository systemRoleRepository;

        private readonly IMapper _mapper;
        public RoleService(ISystemRoleMenusRepository systemRoleMenusRepository, IMenusRepository menusRepository, ISystemRoleRepository systemRoleRepository, IMapper _mapper)
        {
            this.systemRoleRepository = systemRoleRepository;
            this.systemRoleMenusRepository = systemRoleMenusRepository;
            this.menusRepository = menusRepository;
            this._mapper = _mapper;
        }
        /// <summary>
        /// 添加权限菜单关联
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public async Task<bool> InsertSystemRoleMenusAsync(SystemRoleMenusInRequest models)
        {
            return await systemRoleMenusRepository.InsertAsync(models);
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public async Task<bool> InsertSystemRoleAsync(RoleInRequest request)
        {
            try
            {
                using (var transactionScope = new TransactionScope())
                {
                    var role = new Entity.SystemRole.SystemRole();
                    role.Name = request.Name;
                    var roleId = await systemRoleRepository.InsertAsync(role);
                    var models = new SystemRoleMenusInRequest();
                    models.RoleId = roleId;
                    models.SystemRoleMenuInRequest = request.SystemRoleMenuInRequest;
                    await systemRoleMenusRepository.InsertAsync(models);
                    transactionScope.Complete();
                }
            }
            catch (System.Exception ex)
            {
                throw new Common.Exceptions.BusinessException(ex.Message);
            }
            return true;
        }

        public async Task<List<SystemRoleResult>> GetSystemRoleAllAsync()
        {
            var result = new List<SystemRoleResult>();
            var systemRoles = await systemRoleRepository.GetSystemRolesAllAsync();
            foreach (var systemRole in systemRoles)
            {
                var models = await systemRoleMenusRepository.GetAllAsync(systemRole.Id);
                var item = await menusRepository.GetAllMenusAsync(models.Select(n => n.MenuId).ToList());
                var systemRoleResult = new SystemRoleResult();
                systemRoleResult.SystemRole = systemRole;
                var model = item.Where(n => n.ParentId == 0).Select(n => _mapper.Map<MenuInResult>(n)).ToList();
                foreach (var request in model)
                {
                    request.Children = item.Where(n => n.ParentId == request.Id).Select(n => _mapper.Map<MenuInResult>(n)).ToList();
                }
                systemRoleResult.MenuInResult = model;
                result.Add(systemRoleResult);
            }
            return result;

        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateSystemRoleAsync(RoleInRequest request)
        {
            try
            {
                using (var transactionScope = new TransactionScope())
                {
                    var role = await systemRoleRepository.GetRoleAsync(request.Id);
                    role.Name = request.Name;
                    await systemRoleRepository.UpdateAsync(role);
                    var models = new SystemRoleMenusInRequest();
                    models.RoleId = request.Id;
                    models.SystemRoleMenuInRequest = request.SystemRoleMenuInRequest;
                    await systemRoleMenusRepository.InsertAsync(models);
                    transactionScope.Complete();
                }
            }
            catch (System.Exception ex)
            {
                throw new Common.Exceptions.BusinessException(ex.Message);
            }
            return true;
        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteSystemRoleAsync(int id)
        {
            await systemRoleMenusRepository.DeleteAsync(id);
            return await systemRoleRepository.DeleteAsync(id);
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public async Task<bool> InsertMenusAsync(Entity.Menus.Menus model)
        {
            return await menusRepository.InsertAsync(model) > 0;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public async Task<bool> InsertMenusAsync(List<MenuInRequest> models)
        {
            try
            {
                using (var transactionScope = new TransactionScope())
                {
                    foreach (var item in models)
                    {
                        var menuEntity = new Entity.Menus.Menus()
                        {
                            Icon = item.Icon,
                            Name = item.Name,
                            ParentId = item.ParentId,
                            Path = item.Path,
                            Title = item.Title
                        };
                        var id = await menusRepository.InsertAsync(menuEntity);
                        if (item.Children == null)
                            continue;
                        foreach (var menu in item.Children)
                        {
                            menuEntity = new Entity.Menus.Menus()
                            {
                                Icon = menu.Icon,
                                Name = menu.Name,
                                ParentId = id,
                                Path = menu.Path,
                                Title = menu.Title
                            };
                            await menusRepository.InsertAsync(menuEntity);
                        }
                    }
                    transactionScope.Complete();
                }
            }
            catch (System.Exception ex)
            {
                throw new Common.Exceptions.BusinessException(ex.Message);
            }
            return true;
        }
        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateMenusAsync(Entity.Menus.Menus model)
        {
            return await menusRepository.UpdateAsync(model);
        }
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteMenusAsync(int id)
        {
            return await menusRepository.DeleteAsync(id);
        }
        /// <summary>
        /// 根据权限ID获取菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<List<MenuInResult>> GetMenusAllAsync(int roleId)
        {
            //获取
            var models = await systemRoleMenusRepository.GetAllAsync(roleId);
            var item = await menusRepository.GetAllAsync(models.Select(n => n.MenuId).ToList());
            var result = new List<MenuInResult>();
            result = item.Select(n=>_mapper.Map<MenuInResult>(n)).ToList();
           
            return result;
        }

        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Entity.Menus.Menus>> GetMenusAllAsync()
        {
            return await menusRepository.GetAllAsync();
        }
    }
}