﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.Google;
using Microsoft.Owin.Security.OAuth;
using Owin;
using MessageHub.Web.Providers;
using MessageHub.Web.Models;
using Microsoft.AspNet.Identity.Owin;

namespace MessageHub.Web
{
    public partial class Startup
    {
        // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
        public static void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context and user manager to use a single instance per request
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

            // Enable the application to use a cookie to store information for the signed in user
            // and to use a cookie to temporarily store information about a user logging in with a third party login provider
            // Configure the sign in cookie
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
					OnValidateIdentity = SecurityStampValidator
						.OnValidateIdentity<ApplicationUserManager, ApplicationUser, long>(
							validateInterval: TimeSpan.FromMinutes(30),
							regenerateIdentityCallback: (manager, user) =>
								user.GenerateUserIdentityAsync(manager),
							getUserIdCallback: (id) => GetValue(id))
                }
            });

            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            // Uncomment the following lines to enable logging in with third party login providers
            //app.UseMicrosoftAccountAuthentication(
            //    clientId: "",
            //    clientSecret: "");

            //app.UseTwitterAuthentication(
            //   consumerKey: "",
            //   consumerSecret: "");

            app.UseFacebookAuthentication(
               appId: "1585663128340191",
               appSecret: "f3a15f5148d7bfa56f55f3f6bc8e9e1f");

            app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions
            {
                ClientId = "526850737677-ke2hgbmh6nq3dm1l3prj8f7hv1j66bq8.apps.googleusercontent.com",
                ClientSecret = "rwxakzkpbx-vb1eGRfaPTbXq"
            });
        }

	    public static long GetValue(System.Security.Claims.ClaimsIdentity id)
	    {
			var idVal = id.GetUserId();
		    return 1;
	    }
    }
}
