using Back.Data.Contracts;
using Back.Data.Models;
using Microsoft.EntityFrameworkCore;

namespace Back.Services
{
    public class servPermission
    {
       private readonly IAsyncRepository<Permission> _repoPermission;
        private readonly IAsyncRepository<PermissionPeriod> _repoPermissionPeriod;
        private readonly IAsyncRepository<RolUser> _repoRolUser;
        private readonly IAsyncRepository<PermissionUser> _repoPermissionUser;
        public servPermission(IAsyncRepository<Permission> repoPermission, IAsyncRepository<PermissionPeriod> repoPermissionPeriod
            , IAsyncRepository<RolUser> repoRolUser, IAsyncRepository<PermissionUser> repoPermissionUser)
        {
            _repoPermission = repoPermission;
            _repoPermissionPeriod = repoPermissionPeriod;
            _repoPermissionUser = repoPermissionUser;
            _repoRolUser= repoRolUser;
        }
        public async Task<List<Permission>> GetChildPermission(int PermissionID)
        {
           return await _repoPermission.Get(w => w.ParentID == PermissionID).ToListAsync();
        }
        public async Task<List<Permission>> GetPermissions()
        {
            return await _repoPermission.GetAll().ToListAsync();
        }
        public async Task<RolUser> AddRolUser(RolUser rolUser)
        {
            //SysLog log = new SysLog()
            //{
            //    TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
            //    Datetime = DateTime.Now.ConvertMiladiToShamsi(),
            //    Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddRolUser",
            //    Value = "*" + JsonConvert.SerializeObject(rolUser),
            //    Route = _httpContextAccessor.HttpContext.Request.Path,
            //    Type = "User"
            //};
            //_contextMongodb.InsertItem(log);
            return await _repoRolUser.AddAsync(rolUser);
        }
        public async Task<bool> AddRangePermissionPeriodByCompany(IEnumerable<PermissionPeriod> permissions)
        {
            try
            {
               return await _repoPermissionPeriod.AddRangeAsync(permissions.ToList())/*.Wait()*/;
                //SysLog log = new SysLog()
                //{
                //    TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
                //    Datetime = DateTime.Now.ConvertMiladiToShamsi(),
                //    Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddRangePermissionPeriodByCompany",
                //    Value = JsonConvert.SerializeObject(permissions),
                //    Route = _httpContextAccessor.HttpContext.Request.Path,
                //    Type = "User"
                //};
                //_contextMongodb.InsertItem(log);
              

            } 
            catch (Exception ex)
            {
                //SysLog log = new SysLog()
                //{
                //    TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
                //    Datetime = DateTime.Now.ConvertMiladiToShamsi(),
                //    Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddRangePermissionPeriodByCompany",
                //    Value = ex.Message,
                //    Route = _httpContextAccessor.HttpContext.Request.Path,
                //    Type = "catch"
                //};
                //_contextMongodb.InsertItem(log);
                return false;
                
            }
        }
        public async Task<bool> AddPermissionUser(int RolUserID, int[] PermissionIDs)
        {
            try
            {
                    List<PermissionUser> permissionUsers = new List<PermissionUser>();
                foreach (int permissionID in PermissionIDs)
                    permissionUsers.Add(new PermissionUser() { PermissionID=permissionID,RolUserID= RolUserID });

              return await _repoPermissionUser.AddRangeAsync(permissionUsers);

                //SysLog log = new SysLog()
                //{
                //    TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
                //    Datetime = DateTime.Now.ConvertMiladiToShamsi(),
                //    Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddPermissionUser",
                //    Value = RolUserID+" "+JsonConvert.SerializeObject(PermissionIDs),
                //    Route = _httpContextAccessor.HttpContext.Request.Path,
                //    Type = "User"
                //};
                //_contextMongodb.InsertItem(log);

            }
            catch (Exception ex)
            {
                //SysLog log = new SysLog()
                //{
                //    TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
                //    Datetime = DateTime.Now.ConvertMiladiToShamsi(),
                //    Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddPermissionUser",
                //    Value = ex.Message,
                //    Route = _httpContextAccessor.HttpContext.Request.Path,
                //    Type = "catch"
                //};
                //_contextMongodb.InsertItem(log);
                return false;
                
            }
           
        }
    }
}
