using Hotline.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Dependency;
using Hotline.Caching.Interfaces;

namespace Hotline.Permissions
{
    public interface IPermissionManager
    {
        IReadOnlyList<string> RolesToPermissions(List<string> roles);
    }

    public class PermissionManager : IPermissionManager, ISingletonDependency
    {
        private readonly IServiceScopeFactory _serviceScopeFactory;

        public PermissionManager(IServiceScopeFactory serviceScopeFactory)
        {
            _serviceScopeFactory = serviceScopeFactory;

            //_systemauthorityRepository = factory.CreateScope().ServiceProvider.GetRequiredService<ISystemAuthorityRepository>();
        }


        public IReadOnlyList<string> RolesToPermissions(List<string> roles)
        {
            using var scope = _serviceScopeFactory.CreateScope();
            var cacheManager = scope.ServiceProvider.GetService<IRolePermissionsCacheManager>();
            return roles.SelectMany(d => cacheManager.GetPermissions(d)).ToList();

            //var list = _systemauthorityRepository.GetPermission(roles);

            //return list.Select(x => Enum.Parse(typeof(EPermission), x).ToString()).ToList();
        }
    }
}
