/* global angular, menu_groups, modulos_acciones_list, rol_nombre*/
angular.module("app").controller('AppCtrl', [
    '$scope',
    '$state',
    '$rootScope',
    'AuthService',
    function ($scope, $state, $rootScope, AuthService) {
        var init_folded = false;
        var width = window.innerWidth;
        if (1400 > width) {
            init_folded = true;
        }
        $scope.app = {
            name: "Caronte",
            version: app_version,
            settings: {
                menuFolded: init_folded,
                pagetitle: rol_nombre,
                profileOpened: false
            }
        };
        $scope.menu = {grupos: menu_groups};
        $scope.modulos_acciones_list = modulos_acciones_list;
        for (var i = 0; i < $scope.menu.grupos.length; i++) {
            for (var j = 0; j < $scope.menu.grupos[i].categorias.length; j++) {
                $scope.menu.grupos[i].categorias[j].active = false;
            }
        }
        $scope.open_close_categoria = function (index_i, index_j) {
            var aux = $scope.menu.grupos[index_i].categorias[index_j].active;
            for (var i = 0; i < $scope.menu.grupos.length; i++) {
                for (var j = 0; j < $scope.menu.grupos[i].categorias.length; j++) {
                    $scope.menu.grupos[i].categorias[j].active = false;
                }
            }
            $scope.menu.grupos[index_i].categorias[index_j].active = !aux;
        };
        $scope.logout = function () {
            AuthService.logout().then(function () {
                delete ($rootScope.usuario_acceso);
                $rootScope.intentos_captcha = 0;
                $state.go("login");
            });
        };
        $scope.amounts_of_results_by_page = ['10', '20', '50', '100'];
        $scope.change_by_page = function (amount) {
            $state.go($state.current, {'por_pagina': amount}, {reload: true});
        };
    }
]);

angular.module('app').filter('contains', function () {
    return function (array, needle) {
        var index = -1;
        if (!Array.isArray(array)) {
            return false;
        }
        for (var i = 0; i < array.length; i++) {
            if (parseInt(array[i]) === parseInt(needle)) {
                index = i;
            }
        }
        return index >= 0;
    };
});