module.exports = function(io) {
  var app = require('express');
  var router = app.Router();
  var Api = require('../lib/api.controller');
  var Page = require('../lib/page.controller');
  var path      = require("path");
  var gconfig = require(path.join(__dirname, '..', 'config', 'global.json'))['global'];

  // start listen with socket.io
  io.on('connection', function(socket){
    console.log('User connected to use case');
    var interval = null;

    socket.on('charts', function(msg){
      var charts = msg;
      if(charts.length > 0){
        // Emit updated data every X seconds
        interval = setInterval(function () {
            for(var i=0; i< charts.length; i++){
              if(charts[i] != 'cost'){
                Api.getrealTimeData(charts[i], function(result){
                  if(result.data.val.length > 0){
                    // Emit data for charts
                    socket.emit("chartData", result );
                  }
                });
              }else{
                // cost realtime
                Api.getrealTimeCostData(charts[i], function(result){
                  // Emit data for charts
                  socket.emit("chartData", result );
                });
              }
            }
        }, gconfig.nSec*1000);
      }
    });
    
    socket.on('generalcharts', function(msg){
      var charts = msg;
      if(charts.length > 0){
        // Emit updated data every X seconds
        interval = setInterval(function () {
            for(var i=0; i< charts.length; i++){
              Api.getLastValueLab('Lab', charts[i], function(result){
                if(result.data.val.length > 0){
                  // Emit data for charts
                  socket.emit("chartGData", result );
                }
              });
            }
        }, gconfig.nSec*1000);
      }
    });
    

    socket.on("disconnect", function () {
        clearInterval(interval);
        console.log('User disconnect to use case');
    });
  });

  router.get('/:type', function(req, res, next) {
    // Dynamic routing
    var type = req.params.type;
    Api.getDataWeb(function(result){
      var sesion = false;
      if (req.session.user) {
        sesion = true;
      }
      Page.getPageByUrl('/use-cases/'+type, function(presult){
        if(presult == 0){
          res.render('error',{
            message: '404 Page not found',
            error: {} 
          });
        }else{
          if(presult.tipo == 2){
            // Page with charts
            var listcharts = [];
            for(var i=0; i< presult.charts.length; i++){
              var chart = {};
              chart['id'] = presult.charts[i].SensorDetail.evento;
              chart['formato'] = presult.charts[i].formato;
              chart['btns'] = presult.charts[i].botones;
              
              listcharts.push(chart);
            }
            res.render(presult.plantilla, { title: presult.titulo, graphs: listcharts, dataweb: result, logged: sesion });    
          }else{
            // Page with text/photo content
            res.render(presult.plantilla, {Page: presult});
          }
          
        }
      });
    });
  });

  router.get('/:type/info', function(req, res, next) {
    var tipo = req.params.type;
    
    Page.getPageByUrl('/use-cases/'+tipo+'/info', function(result){
      if (result == 0){
        res.render('error',{
          message: '404 Page not found',
          error: {} 
        });
      }else{
        res.render(result.plantilla, {Page: result});
      }
    });
  });

  return router;
}
