var express = require('express');
var auth = require('./auth');
var router = express.Router();
var utils = require('../utils/utils.js');
var compression = require('compression');
var config = require('../profile/config.js');
var common = require('./common.js');

router.use(compression());
router.use(auth.authware({}));


router.use(function(req, res, next){
  res.set({'Cache-Control': 'no-cache no-store', 'Pragma':'no-cache', 'Content-Type': 'application/json;charset=UTF-8'});
  next();
});


router.get('/liveroom/:courseId', function(req, res, next){
  var courseId = req.params['courseId'];
  var name = req.user.username;
  var email = req.user.email;
  var role = 'admin';

  if(!courseId) return next({code:config.errors.paramError, message:'no course id found'});

  var playInfo = utils.generatePlayerUserInfo(name, email, Date.now(), role, courseId);
  var redirect = '/player/'+playInfo;
  res.redirect(302, redirect);
});


/* GET users listing. */
router.get('/', function(req, res) {
  res.send('respond with a resource');
});


router.use(common.apiErrorHandler({}));

module.exports = router;
