using System;
using System.Threading.Tasks;
using InventoryTraker.Web.Core;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security.DataProtection;

namespace InventoryTraker.Web.Identity
{
	public class ApplicationRoleManager : RoleManager<IdentityRole>
	{
		public const string AdminRoleName = "Admin";

		public ApplicationRoleManager(IRoleStore<IdentityRole, string> store) : base(store)
		{
		}
	}

	public class ApplicationUserManager : UserManager<User>
	{
		public ApplicationUserManager(IUserStore<User> store, IDataProtectionProvider dataProtectionProvider)
			: base(store)
		{
			UserValidator = new UserValidator<User>(this)
			{
				AllowOnlyAlphanumericUserNames = false,
				RequireUniqueEmail = true
			};

			if (dataProtectionProvider != null)
			{
				var dataProtector = dataProtectionProvider.Create("Protector");
				UserTokenProvider = new DataProtectorTokenProvider<User, string>(dataProtector)
				{
					TokenLifespan = TimeSpan.FromHours(1),
				};
			}
		}

		public async Task<IdentityResult> ChangePasswordAsync(User user, string newPassword)
		{
			var resetToken = await GeneratePasswordResetTokenAsync(user.Id);
			return await ResetPasswordAsync(user.Id, resetToken, newPassword);
		}
	}
}