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

namespace Shride.Models
{
  public class RoleInitializer
  {
    public static async Task EnsurePopulated(IServiceProvider service)
    {
      var roleManager = service.GetRequiredService<RoleManager<IdentityRole>>();

      await CreateRoles(roleManager);
    }

      private static async Task CreateRoles(RoleManager<IdentityRole> rMgr)
      {
          if(!await rMgr.RoleExistsAsync("Adult"))
          {
            await rMgr.CreateAsync(new IdentityRole("Adult"));
          }

          if (!await rMgr.RoleExistsAsync("Child"))
          {
            await rMgr.CreateAsync(new IdentityRole("Child"));
          }

          if (!await rMgr.RoleExistsAsync("Business"))
          {
            await rMgr.CreateAsync(new IdentityRole("Business"));
          }

          if (!await rMgr.RoleExistsAsync("Admin"))
          {
            await rMgr.CreateAsync(new IdentityRole("Admin"));
          }
      }
  }
}
