using FoodShop.Data;
using FoodShop.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace FoodShop.Services
{
    public class SeedUserList
    {
        public string Name { get; set; }
        public string Role { get; set; }

        public SeedUserList(string name, string role)
        {
            Name = name;
            Role = role;
        }
    }

    public class SeedData
    {
        public static void InitializeIdentity(IServiceProvider serviceProvider)
        {
            string userPWD = "pwdadmin";
            //using var context = new ApplicationDbContext(serviceProvider.GetRequiredService<DbContextOptions<ApplicationDbContext>>());
            var context = new ApplicationDbContext(serviceProvider.GetRequiredService<DbContextOptions<ApplicationDbContext>>());

            string[] roleNames = { "Admin", "PowerUser", "User", "Guest", "Member" };

            var roleStore = new RoleStore<IdentityRole>(context);

            foreach (var roleName in roleNames)
            {
                if (!context.Roles.Any(r => r.Name == roleName))
                {
                    var role = new IdentityRole
                    {
                        Name = roleName,
                        //NormalizedName = roleName
                        NormalizedName = roleName.ToUpper()
                    };
                    roleStore.CreateAsync(role).Wait();
                }
            }

            SeedUserList[] userNames = {
                new SeedUserList("admin@local.net", "Admin".ToUpper()),
                new SeedUserList("poweruser@local.net", "PowerUser".ToUpper()),
                new SeedUserList("user@local.net", "User".ToUpper()),
                new SeedUserList("guest@local.net", "Guest".ToUpper())
            };

            var userStore = new UserStore<ApplicationUser>(context);

            foreach (var seeduser in userNames)
            {
                var testuser = userStore.FindByNameAsync(seeduser.Name).Result;

                if (testuser == null)
                {
                    var user = new ApplicationUser();
                    var password = new PasswordHasher<ApplicationUser>();

                    user.UserName = seeduser.Name;
                    user.Email = user.UserName;
                    user.NormalizedUserName = user.UserName.ToUpper();
                    user.NormalizedEmail = user.UserName.ToUpper();
                    user.PasswordHash = password.HashPassword(user, userPWD);
                    user.Name = user.UserName;
                    user.Address = "";
                    user.City = "";
                    user.State = "IT";
                    user.Country = "Italia";
                    user.PostalCode = "";
                    user.FiscalCode = "";
                    user.EmailConfirmed = true;
                    user.ConsensoViewEmail = false;
                    user.ConsensoCookie = true;
                    user.HasRoles = true;
                    user.EntryDate = DateTime.Now;

                    userStore.CreateAsync(user).Wait();
                    userStore.AddToRoleAsync(user, seeduser.Role).Wait();
                }
            }

        }
    }
}
