var express = require('express');
var router = express.Router();
var Api = require('../lib/api.controller');

/******************************************************************************/
/*
 * Sensor (co2, temp, hum)
 */
router.route('/sensor/:type').get(function(req,res){
	var tipo = req.params.type;

	if(tipo != 'cost'){
		/*
		 *	Eventos del tipo: co2, temp, hum
		 */

		 Api.getstartChart(tipo, function(result){
			res.send(JSON.stringify( result));
		 });

	}else{
		/*
		 * Eventos del tipo: cost
		 */
 		Api.getstartCostChart(tipo, function(result){
			res.send(JSON.stringify( result));
		});

	}

});
/*
 * Average (am, ah, ad)
 */
router.route('/sensor/:type/avg/:mode').get( function(req, res) {

	var tipo = req.params.type;
  var mode = req.params.mode;

  Api.getstartAvgChart(tipo, mode, function(result){
  	res.send(JSON.stringify( result));
  });

});

/*
 * Last value for Lab
 */
router.route('/sensor/:type/lablast').get(function(req,res){
	var tipo = req.params.type;

		/*
		 *	Eventos del tipo: co2, temp, hum, halleff 
		 *  Location: Lab
		 */
		 Api.getLastValueLab('Lab', tipo, function(result){
			res.send(JSON.stringify( result));
		 });

});

/******************************************************************************/
/************************          POST         *******************************/
/******************************************************************************/

/* POST Set CC algorithm, need tmin and tmax  */
router.post('/setcc/:min/:max', function(req, res, next){

  if(req.params.min != null && req.params.max != null){
    var tmin = req.params.min;
    var tmax = req.params.max;

    Api.getsetDataWebCC(tmin, tmax, function(result){
      console.log("update CC done");
      if(result == 1){
        res.send("CC actualizado");
      }else {
        res.send("KO");
      }
    });
  }else{
      console.log("nothing to do");
  }
});

/* POST Set CCR algorithm, need tobjective  */
router.post('/setccr/:objective', function(req, res, next){

  if(req.params.objective != null){
    var tobj = req.params.objective;

    Api.getsetDataWebCCR(tobj, function(result){
      console.log("update CCR done");
      if(result == 1){
        res.send("CCR actualizado");
      }else {
        res.send("KO");
      }
    });
  }else{
      console.log("nothing to do");
  }
});


module.exports = router;
