var models  = require('../models');
//var Chart = require("./chart.controller");

var Page = function() {
    
    var getListPages = function(callback){
      models.Page.findAll().then(function(pages){
        var listpages = [];
        for (var i = 0; i<pages.length; i++){
    		  var page = {};
    			page['id'] = pages[i].id;
    			page['titulo'] = pages[i].titulo;
    			page['url'] = pages[i].url;
    			page['tipo'] = pages[i].tipo;
    
    			listpages.push(page);
    		}

        callback(listpages);
      }).catch(function(){
        console.log("error getting list page");
        callback( [] );
      });
        
    }
    
    var getPageByUrl = function(url, callback){
      models.Page.findAll({include: [{model: models.PageChart, include: [{model: models.Chart, include:[{model: models.SensorDetails}]}] }], where:{ url : url}, order: 'id ASC'}).then(function(result){
        if(result.length == 0){
          return callback([]);
        }
        var page = {}
        for (var i = 0; i<result.length; i++){
          page['id'] = result[i].id;
          page['titulo'] = result[i].titulo;
          page['url'] = result[i].url;
          page['contenido'] = result[i].contenido;
          page['plantilla'] = result[i].plantilla;
          page['tipo'] = result[i].tipo;
          page['charts'] = [];
          
          if(result[i].PageCharts.length > 0){
            for(var j=0; j < result[i].PageCharts.length; j++){
              page['charts'].push(result[i].PageCharts[j].Chart);
            }
          }
          
        }
        return callback(page);
      }).catch(function(){
        return callback(0);
      });
    }
    
    var getPageById = function(pid, callback){
      models.Page.findAll({include: [{model: models.PageChart, include: [{model: models.Chart/*, where:{ idChart: models.PageChart.idChart}*/}] }], where:{ id : pid}, order: 'id ASC'}).then(function(result){

        if(result.length == 0){
          return callback([]);
        }
        var page = {}
        for (var i = 0; i<result.length; i++){
          page['id'] = result[i].id;
          page['titulo'] = result[i].titulo;
          page['url'] = result[i].url;
          page['contenido'] = result[i].contenido;
          page['plantilla'] = result[i].plantilla;
          page['charts'] = [];
          
          if(result[i].PageCharts.length > 0){
            for(var j=0; j < result[i].PageCharts.length; j++){
              page['charts'].push(result[i].PageCharts[j].Chart);
            }
          }
          
        }
        return callback(page);
      }).catch(function(resu){
        console.log(resu);
        return callback(0);
      });
    }
    
    var insertPage = function(ptitulo, purl, pcontenido, ptipo, ptemplate, callback){
      
      models.Page.build({titulo: ptitulo, url: purl, contenido: pcontenido, tipo: ptipo, plantilla: ptemplate}).save().then(function(result){
        callback(result);
      });  
    }
    
    var updatePage = function(pid, ptitulo, purl, pcontenido, callback){
      
      models.Page.update({ titulo: ptitulo, url: purl, contenido: pcontenido }, { where: { id : pid } }).then(function (result) {
        return callback(result);
      });
      
    }
    
    var deletePageById = function(pid, callback){
      models.Page.destroy({ where: { id: pid }}).then(function(result){
        
        models.PageChart.destroy({where: { idPage: pid }}).then(function(resul){
          return callback(resul);
        }).catch(function(){
          return callback(result);
        });
      });
    }
    
    return {
        getListPages: getListPages
        ,insertPage: insertPage
        ,getPageByUrl: getPageByUrl
        ,getPageById: getPageById
        ,updatePage: updatePage
        ,deletePageById: deletePageById
    }
    
}();

module.exports = Page;
