using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Web.Security;
using Microsoft.Web.WebPages.OAuth;
using Qusion.Framework.Configuration;
using Qusion.WebApp.Areas.Qusion.Models.Account;
using WebMatrix.WebData;

namespace Qusion.WebApp
{
    public static class AuthConfig
    {
        public static void RegisterAuth()
        {
            // To let users of this site log in using their accounts from other sites such as Microsoft, Facebook, and Twitter,
            // you must update this site. For more information visit http://go.microsoft.com/fwlink/?LinkID=252166

            //OAuthWebSecurity.RegisterMicrosoftClient(
            //    clientId: "",
            //    clientSecret: "");

            //OAuthWebSecurity.RegisterTwitterClient(
            //    consumerKey: "",
            //    consumerSecret: "");

            //OAuthWebSecurity.RegisterFacebookClient(
            //    appId: "",
            //    appSecret: "");

            OAuthWebSecurity.RegisterGoogleClient();
        }

        public static void RegisterMembership()
        {

            Database.SetInitializer<UserContext>(null);
            try
            {
                using (var context = new UserContext())
                {
                    if (!context.Database.Exists())
                    {
                        // Create the SimpleMembership database without Entity Framework migration schema
                        ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                    }
                }
             
                WebSecurity.InitializeDatabaseConnection(ConnectionStringProvider.GetConnectionStringName(), "qusion_User", "UserId", "UserName",
                                                         autoCreateTables: true);

                if (!Roles.RoleExists("Administrator"))
                    Roles.CreateRole("Administrator");

                if (!Roles.RoleExists("Agent"))
                    Roles.CreateRole("Agent");

                if (!Roles.RoleExists("Guest"))
                    Roles.CreateRole("Guest");
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                    "The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588",
                    ex);
            }
        }
    }
}