﻿/*
 * Copyright 2014 Dominick Baier, Brock Allen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Data.Entity;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace Sahra.MutualFund.IdentityServer.WebHost.AspId
{
    public class User : IdentityUser
    {
        public virtual string DisplayName { get; set; }
        public virtual bool IsAdmin { get; set; }
        public virtual bool IsCustomizedAccess { get; set; }
        public virtual DateTime? CreateDate { get; set; }
        public virtual int? Status { get; set; }
        public string Hometown { get; set; }

    }

    public class Role : IdentityRole
    {


    }

    public class Context : IdentityDbContext<User, Role, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
    {
        public Context(string connString)
            : base(connString)
        {
        }

        public Context()
            : base("name=AspId")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.HasDefaultSchema("sec");
            base.OnModelCreating(modelBuilder);

        }

        public class UserStore : UserStore<User, Role, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
        {
            public UserStore(Context ctx)
                : base(ctx)
            {
            }
        }

        public class UserManager : UserManager<User, string>
        {
            public UserManager(UserStore store)
                : base(store)
            {
                this.RegisterTwoFactorProvider("sms", new Sms());

            }

        }

        public class Sms : Microsoft.AspNet.Identity.PhoneNumberTokenProvider<User, string>
        {
            public override Task<bool> ValidateAsync(string purpose, string token, UserManager<User, string> manager,
                User user)
            {
                // just hard coding to validate any 2fa token
                return Task.FromResult(true);
                //return base.ValidateAsync(purpose, token, manager, user);
            }
        }

        public class RoleStore : RoleStore<Role>
        {
            public RoleStore(Context ctx)
                : base(ctx)
            {
            }
        }

        public class RoleManager : RoleManager<Role>
        {
            public RoleManager(RoleStore store)
                : base(store)
            {
            }
        }


    }
}