var passport = require('passport')
    , GoogleStrategy = require('passport-google-oauth').OAuth2Strategy
    , FacebookStrategy = require('passport-facebook').Strategy;

var config = require('../config/configUtils');

passport.use(new GoogleStrategy({
        authorizationURL: 'https://accounts.google.com/o/oauth2/auth',
        tokenURL: 'https://accounts.google.com/o/oauth2/token',
        clientID: config.getConfigs().GOOGLE_CLIENT_ID,
        clientSecret: config.getConfigs().GOOGLE_CLIENT_SECRET,
        callbackURL: config.getConfigs().GOOGLE_RETURN_URL
    },
    function (accessToken, refreshToken, profile, done) {
        var userInfo = {
            'type': 'google',
            'userid': profile.id,
            'name': profile.displayName,
            'email': profile.emails[0].value,
            'avatar': profile._json.picture
        };
        return done(null, userInfo);
    }
));

passport.use(new FacebookStrategy({
        clientID: config.getConfigs().FACEBOOK_APP_ID,
        clientSecret: config.getConfigs().FACEBOOK_APP_SECRET,
        callbackURL: config.getConfigs().FACEBOOK_RETURN_URL,
        enableProof: false
    },
    function (accessToken, refreshToken, profile, done) {
        console.log('facebook profile: ' + JSON.stringify(profile));
        var userInfo = {};
        return done(null, userInfo);
    }
));

passport.serializeUser(function (user, done) {
    done(null, user);
});

passport.deserializeUser(function (obj, done) {
    done(null, obj);
});

module.exports = function (app, session) {
    app.use(passport.initialize());
    app.get('/auth/google',
        passport.authenticate('google', {
            scope: 'https://www.google.com/m8/feeds https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
        }),
        function (req, res) {
        });

    app.get('/auth/google/return',
        passport.authenticate('google', { failureRedirect: '/login' }),
        function (req, res) {
            if (req.user) {
                session.set(req, 'user', JSON.stringify(req.user), function (err, reply) {
                });
                res.redirect('/user?sid=' + req.cookies['session_id']);
            }
        });
    app.get('/user', function (req, res) {
        var id = req.query.sid;
        console.log('----- received temporary sid: ' + id);
        session.getById(id, 'user', function (err, reply) {
            if (reply) {
                session.deleteById(id, 'session', function (err, reply) {
                });
                session.deleteById(id, 'user', function (err, reply) {
                });
                session.set(req, 'user', reply, function (err, reply) {
                });
            }
        });
        res.redirect('/');
    });

    app.get('/auth/facebook',
        passport.authenticate('facebook', { failureRedirect: '/login' }),
        function (req, res) {
//        if(req.user){
//            session.set(req, 'user', JSON.stringify(req.user), function(err, reply){});
//            res.redirect('/user?sid=' + req.cookies['session_id']);
//        }
        });

    app.get('/auth/facebook/return',
        passport.authenticate('facebook', { failureRedirect: '/login' }),
        function (req, res) {
            // Successful authentication, redirect home.
            res.redirect('/');
        });
};
@@ -1305,16 +1305,224 @@
 Info = %7B
+%0A            'type': 'facebook',%0A            'userid': profile.id,%0A            'name': profile.displayName,%0A            'email': profile.emails%5B0%5D.value,%0A            'avatar': profile.photos%5B0%5D.value%0A        
 %7D;%0A     
@@ -1787,24 +1787,29 @@
 tialize());%0A
+    %0A
     app.get(
@@ -2029,49 +2029,8 @@
   %7D)
-,%0A        function (req, res) %7B%0A        %7D
 );%0A%0A
@@ -2398,24 +2398,558 @@
 %0A        %7D);
+%0A%0A    app.get('/auth/facebook',%0A        passport.authenticate('facebook', %7B failureRedirect: '/login', scope: %5B'email'%5D %7D));%0A%0A    app.get('/auth/facebook/return',%0A        passport.authenticate('facebook', %7B failureRedirect: '/login' %7D),%0A        function (req, res) %7B%0A            if (req.user) %7B%0A                session.set(req, 'user', JSON.stringify(req.user), function (err, reply) %7B%0A                %7D);%0A                res.redirect('/user?sid=' + req.cookies%5B'session_id'%5D);%0A            %7D%0A        %7D);%0A%0A    //common middle procedure
 %0A    app.get
@@ -3498,592 +3498,7 @@
  %7D);
-%0A%0A    app.get('/auth/facebook',%0A        passport.authenticate('facebook', %7B failureRedirect: '/login' %7D),%0A        function (req, res) %7B%0A//        if(req.user)%7B%0A//            session.set(req, 'user', JSON.stringify(req.user), function(err, reply)%7B%7D);%0A//            res.redirect('/user?sid=' + req.cookies%5B'session_id'%5D);%0A//        %7D%0A        %7D);%0A%0A    app.get('/auth/facebook/return',%0A        passport.authenticate('facebook', %7B failureRedirect: '/login' %7D),%0A        function (req, res) %7B%0A            // Successful authentication, redirect home.%0A            res.redirect('/');%0A        %7D);
 %0A%7D;

add passport-facebook oauth2 strategy.