var models = require("../models/models.js");
var sp = require('../scripts/sp');
var request = require('request');


// POST sportpager/crear
exports.ack_firebase = function(req, res) {
	console.log("POST sportpager ack_firebase: ---> ");
	console.log("ack: " + JSON.stringify(req.body));

	var sportpagers = models.Sportpagers;

	dataQuery = {tipo: "enviado"};
	dataWhere = {where: {firebase_id: req.body.firebase_id, rowId: req.body.rowId}};

	sportpagers 
	.update(dataQuery, dataWhere)
	.then( function(affectedRows){
	console.log("ack_firebase Sportpager ACK. rows: " + affectedRows);
	});


	res.send("OK");
}

// POST sportpager/eliminar
exports.eliminar = function(req, res) {

	var d = new Date();
	var n = d.getTime(); 

	console.log("POST sportpager eliminar: ---> ");
	console.log("sp: " + JSON.stringify(req.body));
	console.log("exports.eliminar Ahora: ---> " + n); 

	dataQuery = {
		rowId: req.body.rowId,
		firebase_id: req.body.firebase_id
	};

	var sportpagers = models.Sportpagers;

	dataQuery = {tipo: "eliminado"};
	dataWhere = {where: {firebase_id: req.body.firebase_id, rowId: req.body.rowId}};

	sportpagers 
	.update(dataQuery, dataWhere)
	.then( function(affectedRows){
	console.log("ack_firebase Sportpager eliminar. rows: " + affectedRows);
	});


	res.send("OK");
}

// POST sportpager/crear
exports.crear = function(req, res) {

	var d = new Date();
	var n = d.getTime(); 

	console.log("POST sportpager crear: ---> ");
	console.log("sp: " + JSON.stringify(req.body));
	console.log("exports.crear Ahora: ---> " + n); 

	dataQuery = {
		tipo: req.body.tipo,
		fechaPronostico: req.body.fechaPronostico,
		fechaPronosticoString: req.body.fechaPronosticoString,
		fechaAviso: req.body.fechaAviso,
		fechaAvisoString: req.body.fechaAvisoString,
		tempMin: req.body.tempMin,
		tempMax: req.body.tempMax,
		probLluviaMin: req.body.probLluviaMin,
		probLluviaMax: req.body.probLluviaMax,
		velVientoMin: req.body.velVientoMin,
		velVientoMax: req.body.velVientoMax,
		dirViento: req.body.dirViento,
		sinNubes: req.body.sinNubes,
		ParcialmenteNublado: req.body.ParcialmenteNublado,
		Nublado: req.body.Nublado,
		latitud: req.body.latitud,
		longitud: req.body.longitud,
		lugar: req.body.lugar,
		rowId: req.body.rowId,
		firebase_id: req.body.firebase_id
	};

	var sportpagers = models.Sportpagers.build(dataQuery);

	sportpagers 
	.save()
	.then(function(){
	 	console.log("sportpager almacenado");
	 	//sp.init();
	}).catch(function(error){
		console.log("sportpager error en save: " + error);
		//next(error)
	}); 

	res.send("OK");
}
