const FbWebStrategy = require("passport-facebook").Strategy;
import {
  createRegisterMobile,
  createVerifyMobile,
  verifyWeb
} from "./StrategyUtils";

import config from "config";
import Axios from "axios";
import _ from "lodash";

let log = require("logfilename")(__filename);

const axios = Axios.create({
  baseURL: "https://graph.facebook.com/",
  timeout: 30e3
});

const profileToUser = profile => ({
  username: `${profile.first_name} ${profile.middle_name &&
    profile.middle_name} ${profile.last_name}`,
  email: profile.email,
  firstName: profile.first_name,
  lastName: profile.last_name,
  picture: profile.picture && profile.picture.data,
  authProvider: {
    name: "facebook",
    authId: profile.id
  }
});

const profileMobileToUser = profile => ({
  username: profile.name,
  email: profile.email,
  firstName: profile.givenName,
  lastName: profile.familyName,
  authProvider: {
    name: "google",
    authId: profile.id
  }
});

export async function verifyMobile(
  models,
  publisherUser,
  profile,
  accessToken
) {
  log.info("verifyMobile ", JSON.stringify(profile, null, 4));
  const getMe = () =>
    axios.get("me", {
      params: {
        fields: "name,email,picture,first_name,last_name",
        access_token: accessToken
      }
    }).then(res => profileMobileToUser(res.data));

  return createVerifyMobile(getMe, models, publisherUser, accessToken);
}

export function registerWeb(passport, models, publisherUser) {
  let authenticationFbConfig = config.authentication.facebook;
  if (authenticationFbConfig && authenticationFbConfig.clientID) {
    log.info("configuring facebook authentication strategy");
    let facebookStrategy = new FbWebStrategy(
      {
        ...authenticationFbConfig,
        profileFields: [
          "id",
          "email",
          "picture",
          "gender",
          "link",
          "locale",
          "name",
          "timezone",
        ],
        enableProof: false
      },
      async function(req, accessToken, refreshToken, profile, done) {
        try {
          log.info("registerWeb ", JSON.stringify(profile, null, 4));
          let res = await verifyWeb(
            models,
            publisherUser,
            profileToUser(profile._json),
            accessToken,
          );
          done(res.err, res.user);
        } catch (err) {
          done(err);
        }
      }
    );
    passport.use("facebook", facebookStrategy);
  }
}

export function registerMobile(passport, models, publisherUser) {
  createRegisterMobile(
    "facebook",
    verifyMobile,
    passport,
    models,
    publisherUser
  );
}

/*
const savePicture = async ({ models, user, fbId, token }) => {
  try {
    const result = await axios.get(`${fbId}/picture`, {
      params: {
        redirect: false,
        type: "large",
        height: 480,
        access_token: token
      }
    });
    log.debug("picture ", result.data);
    const picture = _.get(result.data, "data");

    await models.User.update({ picture }, { where: { id: user.id } });
  } catch (error) {
    log.error("savePicture ", error);
    throw error;
  }
};
*/

@@ -842,17 +842,18 @@
 le.given
-N
+_n
 ame,%0A  l
@@ -879,14 +879,67 @@
 mily
-N
+_n
 ame,%0A
+  picture: profile.picture && profile.picture.data,%0A
   au
@@ -967,14 +967,16 @@
 e: %22
-google
+facebook
 %22,%0A 
@@ -1188,24 +1188,31 @@
 =%3E%0A    axios
+%0A      
 .get(%22me%22, %7B
@@ -1208,24 +1208,26 @@
 get(%22me%22, %7B%0A
+  
       params
@@ -1238,16 +1238,18 @@
         
+  
 fields: 
@@ -1291,32 +1291,34 @@
 _name%22,%0A        
+  
 access_token: ac
@@ -1333,24 +1333,151 @@
 n%0A      
+  
 %7D%0A    
+ 
+ 
 %7D)
+%0A      .then(res =%3E %7B%0A        log.debug(%22verifyMobile me: %22, JSON.stringify(res.data));%0A        return res;%0A      %7D)%0A      
 .then(re
@@ -2110,17 +2110,16 @@
 imezone%22
-,
 %0A       
@@ -2461,17 +2461,16 @@
 essToken
-,
 %0A       

fix profileMobileToUser for fb