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

var Menu = function() {

    var getMenu = function(callback){
      //models.Menu.findAll({include: [{model: models.Menu, as:'Menuson' ,attributes: ['id', 'nombre', 'url'], required: false}], where:{padre: 0},order: 'orden ASC'}).then(function(menus){
      models.Menu.findAll({order: 'orden ASC'}).then(function(menus){
        var menuitems= [];
		    for (var i = 0; i<menus.length; i++){
  		    var menu = {};
    			menu['id'] = menus[i].id;
    			menu['nombre'] = menus[i].nombre;
    			menu['url'] = menus[i].url;
    			menu['espadre'] = menus[i].espadre;
    			menu['padre'] = menus[i].padre;
    			menu['orden'] = menus[i].orden;
          menu['edit'] = menus[i].edit;
          
    			menuitems.push(menu);
  		  }
  		  callback( menuitems );
      }).catch(function(err){
        console.log(err);
        callback( [] );
      });
    }
    
    var getMenuById = function(mid, callback){
      models.Menu.findAll({where:{ id : mid}}).then(function(menus){
        if(menus.length == 0){
          return callback(0);
        }
        var menu = {};
        for (var i = 0; i<menus.length; i++){
    			menu['id'] = menus[i].id;
    			menu['nombre'] = menus[i].nombre;
    			menu['url'] = menus[i].url;
    			menu['espadre'] = menus[i].espadre;
    			menu['padre'] = menus[i].padre;
    			menu['orden'] = menus[i].orden;
    	    menu['edit'] = menus[i].edit;
    		}
        return callback(menu);
      }).catch(function(){
        return callback(0);
      });
      
    }
    
    var getListMenu = function(callback){
      
      models.Menu.findAll({order: 'padre, espadre, orden ASC'}).then(function(menus){

        var listmenu= [];
        var litempadre = []
    		for (var i = 0; i<menus.length; i++){
    		  var menu = {};
    			menu['id'] = menus[i].id;
    			menu['nombre'] = menus[i].nombre;
    			menu['url'] = menus[i].url;
    			menu['espadre'] = menus[i].espadre;
    			menu['padre'] = menus[i].padre;
    			menu['orden'] = menus[i].orden;
    	    menu['edit'] = menus[i].edit;
    
    			listmenu.push(menu);
    			if (menu.espadre == 1)
    			  litempadre.push(menu);
    		}
    
        callback({Menus: listmenu, itempadre: litempadre});
      }).catch(function(){
    	  // on error load with empty data
    	  callback({Menu: [], itempadre: []});
    	});
      
    }

    var insertItemMenu = function(itemPadre, nameItem, urlItem, callback){
      models.Menu.max('orden', { where: { padre: itemPadre } }).then(function(max){
        
        models.Menu.build({nombre: nameItem, url: urlItem, espadre:0, padre: itemPadre, orden: max+1, edit: 1 }).save().then(function(result){
          callback(result);
        });
        
      });
    }

    var updateMenu = function(mid, itemPadre, nameItem, urlItem, callback){
      models.Menu.update({nombre: nameItem, url: urlItem, padre: itemPadre }, { where: { id : mid } }).then(function(result){
        callback(result);
      });
    }

    var deleteItemMenuById = function(mid, callback){
      models.Menu.destroy({ where: { id: mid }}).then(function(result){
        return callback(result);
      });
    }

    return {
        getMenu: getMenu
        ,getListMenu: getListMenu
        ,insertItemMenu: insertItemMenu
        ,updateMenu: updateMenu
        ,getMenuById: getMenuById
        ,deleteItemMenuById: deleteItemMenuById
    }

}();

module.exports = Menu;
