var models = require('../models');

var Chart = function(){
    
    var getListCharts = function(callback){
        models.Chart.findAll({ include: [{model: models.SensorDetails}]}).then(function(charts){

            var listchars = [];
            for (var i = 0; i<charts.length; i++){
                var chart = {};
    			chart['id'] = charts[i].id;
    			chart['nombre'] = charts[i].nombre;
		    	chart['idtipo'] = charts[i].SensorDetail.tipo;
		    	chart['tipo'] = charts[i].SensorDetail.evento;
		    	chart['formato'] = charts[i].formato;
		    	chart['botones'] = charts[i].botones;
		    	
		    	listchars.push(chart);
            }

            return callback( listchars );
        }).catch(function(resu){
            console.log("error getting list charts");
            return callback( [] );
        });
    }
    
    var getChartById = function(cid, callback){
        models.Chart.findAll({include: [{model: models.SensorDetails}], where:{id: cid}}).then(function(charts){
            var chart = {};
            for(var i=0; i<charts.length; i++){
                chart['id'] = charts[i].id;
    			chart['nombre'] = charts[i].nombre;
		    	chart['idtipo'] = charts[i].SensorDetail.tipo;
		    	chart['tipo'] = charts[i].SensorDetail.evento;
		    	chart['formato'] = charts[i].formato;
		    	chart['botones'] = charts[i].botones;
            }
            
            return callback(chart);
        }).catch(function(resu){
            console.log(resu);
            console.log("error getting chart by id");
            return callback( [] );
        });
    }
    
    var insertChart = function(cname, ctipo, cformato, cbtns, callback){
        console.log(cname+" "+ctipo+" "+cformato+" "+cbtns);
        models.Chart.build({nombre: cname, tipo: ctipo, formato: cformato, botones: cbtns}).save().then(function(result){
            callback(result);
        });  
        
    }
    
    var insertChartToPage = function(pid, cid, callback){
        
        models.PageChart.build({idPage: pid, idChart: cid}).save().then(function(result){
            callback(result);
        });
        
    }
    
    var updateChart = function(cid, cname, ctipo, cformato, cbtns, callback){
        
        models.Chart.update({nombre: cname, tipo: ctipo, formato: cformato, botones: cbtns}, { where: { id : cid } }).then(function (result) {
            return callback(result);
        });
    }
    
    var deleteChartFromPage = function (pid, cid, callback){
        models.PageChart.destroy({ where: { idPage: pid, idChart: cid }}).then(function(result){
            return callback(result);
        });
    }
    
    var deleteChartById = function(cid, callback){
        
        models.Chart.destroy({ where: { id: cid }}).then(function(result){
            models.PageChart.destroy({where: { idChart: cid }}).then(function(resul){
                return callback(resul);
            }).catch(function(){
                return callback(result);
            });
        });
        
    }

    // definir si un chart tiene botones laterales o no, si no tiene botones solo real time.
    return {
        getListCharts: getListCharts
        ,getChartById: getChartById
        ,insertChart: insertChart
        ,updateChart: updateChart
        ,insertChartToPage: insertChartToPage
        ,deleteChartFromPage: deleteChartFromPage
        ,deleteChartById: deleteChartById
    }
    
}();

module.exports = Chart;