var config = require('./config')
  , logging = require('./logging')
  , passport = require('passport')
  , GithubStrategy = require('passport-github').Strategy
  , LocalStrategy = require('passport-local').Strategy
  , User = require('./models').User




/* Assign all the various properties coming from github to the user document */
function ghUserLink(ghProfile, ghAccessToken, user) {
  var ghUser = ghProfile._json
  console.log("!!! GITHUB:", ghUser);

  var github = {
            id: ghUser.id
          , type: ghUser.type
          , login: ghUser.login
          , accessToken: ghAccessToken
          , gravatarId: ghUser.gravatar_id
          , name: ghUser.name
          , email: ghUser.email
          , publicRepoCount: ghUser.public_repo_count
          , publicGistCount: ghUser.public_gist_count
          , followingCount: ghUser.following_count
          , followersCount: ghUser.followers_count
          , company: ghUser.company
          , blog: ghUser.blog
          , location: ghUser.location
          , permission: ghUser.permission
          , createdAt: ghUser.created_at
          // Private data
          , totalPrivateRepoCount: ghUser.total_private_repo_count
          , collaborators: ghUser.collaborators
          , diskUsage: ghUser.disk_usage
          , ownedPrivateRepoCount: ghUser.owned_private_repo_count
          , privateGistCount: ghUser.private_gist_count
          , plan: {
                name: ghUser.plan.name
              , collaborators: ghUser.plan.collaborators
              , space: ghUser.plan.space
              , privateRepos: ghUser.plan.private_repos
          }
    };
    user.set('github', github);
    console.log(user, user.save)

}




var setupGithubAuth = function(app){
  passport.use(new GithubStrategy({
    clientID : config.github.appId
    , clientSecret: config.github.appSecret
    , callbackURL : config.github.myHostname + "/auth/github/callback"
    , passReqToCallback: true
    },
    function(req, accessToken, refreshToken, profile, done){
      if (!req.user){
        return done("Cannot sign up with github - you must link it to account");
      } 
      console.log("![GITHUB_LOGIN", accessToken, refreshToken, profile);
      ghUserLink(profile, accessToken, req.user);
      req.user.save(function(e){
          if(e) throw e;
          done(null, req.user);
        });
      }))
}

var setupPasswordAuth = function(app){
  passport.use(new LocalStrategy(
      function(username, password, done){
        User.authenticate(username, password, function (err, user) {
          if (err) { return done(err); }
          if (!user) {
            return done(null, false, { message: 'Incorrect username.' });
          }
          return done(null, user); 
          })
   }))
}


var registerRoutes = function(app){
  app.get('/register', function(req, res, next){
    return res.render('register.html', {});
  })

  app.post('/register', function(req, res, next){
    console.log(req.body)

    // Quick and dirty validation
    var errors = []
    if (!req.body.invite_code) errors.push("No invite code specified")
    if (!req.body.email) errors.push("Missing email")
    if (!req.body.password) errors.push("Missing password")
    if (errors.length){
      return res.render('register.html', {errors: errors});
    }

    User.registerWithInvite(
      req.body.invite_code, req.body.email, req.body.password
      , function(err, user){
        if (err){
          return res.render('register.html', {
              errors: [err]
            , invite_code: req.body.invite_code
            , email: req.body.email
            , password: req.body.password
          });
        }

        // Registered success:
        req.login(user, function(err, u){
          res.redirect('/');
        })
      });
  });

  app.get('/auth/github',
    passport.authenticate('github'));

  app.get('/auth/github/callback'
      , passport.authenticate('github', { failureRedirect: '/login' })
      , function(req, res){
        res.redirect('/add_repo')
      })

}


var setup = function(app){

  setupPasswordAuth(app);
  setupGithubAuth(app);

  // serialize user on login
  passport.serializeUser(function(user, done) {
    done(null, user.id);
  });

  // deserialize user on logout
  passport.deserializeUser(function(id, done) {
    User.findById(id, function (err, user) {
      done(err, user);
    });
  });

  app.use(passport.initialize())
  app.use(passport.session())

  // Middleware to setup view parameters with user
  app.use(function(req, res, next){
    console.log(req.user);
    if (req.user){
      res.locals.currentUser = {
          user: req.user
        , password: { // Legacy crap
            loginFormFieldName: 'email'
          , passwordFormFieldName: 'password' 
          }
        };
    }
    next();
  })

  registerRoutes(app);
}

var _authenticate = passport.authenticate('local', {
  successRedirect: '/',
  failureRedirect: '/login#fail'
})



module.exports = {
    setup:setup
  , authenticate: _authenticate
};

@@ -416,46 +416,8 @@
 json
-%0A  console.log(%22!!! GITHUB:%22, ghUser);
 %0A%0A  
@@ -1632,41 +1632,8 @@
 ub);
-%0A    console.log(user, user.save)
 %0A%0A%7D%0A
@@ -2073,81 +2073,8 @@
  %7D %0A
-      console.log(%22!%5BGITHUB_LOGIN%22, accessToken, refreshToken, profile);%0A
     
@@ -2816,34 +2816,8 @@
 xt)%7B
-%0A    console.log(req.body)
 %0A%0A  
@@ -4350,16 +4350,16 @@
 th user%0A
+
   app.us
@@ -4390,35 +4390,8 @@
 t)%7B%0A
-    console.log(req.user);%0A
     

remove debug