using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace BO.AppServer.Business.Security.Extensions
{
    internal static class UserManagerExt
    {
        public static async Task<bool> HasUserRoleAny(this UserManager<IdentityUser> userMan, string userName, IEnumerable<string> roles)
        {
            if (userMan == null)
                throw new ArgumentNullException(nameof(userMan));

            if (string.IsNullOrEmpty(userName))
                throw new ArgumentNullException(nameof(userName));

            if (roles==null)
                throw new ArgumentNullException(nameof(roles));

            var user = await userMan.FindByNameAsync(userName);
            if (user == null)
                throw new ArgumentOutOfRangeException(nameof(user), $"User '{userName}' not found.");

            var ownedRoles = await userMan.GetRolesAsync(user);

            return roles.Any(x => ownedRoles.Contains(x));

        }
    }
}
