var express     = require('express');
var language    = require('../language/EN');
var F           = require('../controllers/Functions');
var config  = require('../config');
var router      = express.Router();
var TPL         = "index";
var obj     = {
  title: config['localhost'].title,
  url: config['localhost'].url
};
module.exports = function(passport){

    router.get('/', F.isAuthenticated, function(req, res) {
        var response = F.infoPage(obj,TPL,language,req);
        F.getNotify(req.user.username, function (data) {
          response.notify = data;
          res.render(TPL, response );
        });
    });

    router.get('/login', F.notIsAuthenticated, function(req, res) {
        var response = F.infoPage(obj,"login",language,req);
        res.render('login', response );
    });

    router.get('/loging', passport.authenticate("loging", { scope: ["profile", "email"] }));
    router.get('/auth/google', passport.authenticate('loging', { scope: 'https://www.googleapis.com/auth/plus.login' }));
    router.get('/auth/google/return', passport.authenticate('loging', {
    	successRedirect: '/',
    	failureRedirect: '/login',
    	failureFlash : true
    }));

    router.post('/login', passport.authenticate('login', {
      successRedirect: '/',
      failureRedirect: '/login',
      failureFlash : true
    }));

    router.get('/logout', function(req, res) {
		if(req.hasOwnProperty("user")){
		    req.logout();
		}
	    res.redirect('/login');
	});

    return router;
};
