﻿using IdentityKey.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IdentityKey.Properties;

namespace IdentityKey.Identity
{
    // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser> store)
            : base(store)
        {
        }

        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
            
            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = IdentityConfig.Default.AllowOnlyAlphanumericUserNames,
                RequireUniqueEmail = true
            };

            manager.UserValidator = new BlackListValidator<ApplicationUser>();

            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = IdentityConfig.Default.RequiredLength,
                RequireNonLetterOrDigit = IdentityConfig.Default.RequireNonLetterOrDigit,
                RequireDigit = IdentityConfig.Default.RequireDigit,
                RequireLowercase = IdentityConfig.Default.RequireLowercase,
                RequireUppercase = IdentityConfig.Default.RequireUppercase,
            };


            // Configure user lockout defaults
            manager.UserLockoutEnabledByDefault = IdentityConfig.Default.UserLockoutEnabledByDefault;
            manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(IdentityConfig.Default.DefaultAccountLockoutMinute);
            manager.MaxFailedAccessAttemptsBeforeLockout = IdentityConfig.Default.MaxFailedAccessAttemptsBeforeLockout;


            // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
            // You can write your own provider and plug it in here.
            manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
            {
                MessageFormat = "Your security code is {0}"
            });

            manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
            {
                Subject = "Security Code",
                BodyFormat = "Your security code is {0}"
            });

            manager.EmailService = new EmailService();
            manager.SmsService = new SmsService();

            var dataProtectionProvider = options.DataProtectionProvider;
            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider =
                    new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
            }
            return manager;
        }
    }
}