/* 
 * Copyright (c) 2015 by Rafael Angel Aznar Aparici (rafaaznar at gmail dot com)
 * 
 * openAUSIAS: The stunning micro-library that helps you to develop easily 
 *             AJAX web applications by using Java and jQuery
 * openAUSIAS is distributed under the MIT License (MIT)
 * Sources at https://github.com/rafaelaznar/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 */


'use strict';
/* Controllers */

moduloUsuariocursoxusuario.controller('UsuariocursoxusuarioPListController', ['$scope', '$routeParams', 'serverService', '$location', 'redirectService', 'sharedSpaceService', 'checkSessionStorageService',
    function ($scope, $routeParams, serverService, $location, redirectService, sharedSpaceService, checkSessionStorageService) {
        checkSessionStorageService.isSessionSessionStoraged();

        $scope.visibles = {};
        $scope.visibles.id = true;
        $scope.visibles.titulo = true;
        $scope.visibles.descripcion = true;
        $scope.visibles.nota = true;

        $scope.ob = "usuariocursoxusuario";
        $scope.op = "plist";
        $scope.title = "Listado de usuariocursoxusuario";
        $scope.icon = "fa-file-text-o";
        $scope.neighbourhood = 2;
        
        

        

        if (!$routeParams.page) {
            $routeParams.page = 1;
        }

        if (!$routeParams.rpp) {
            $routeParams.rpp = 999;
        }

        $scope.numpage = $routeParams.page;
        $scope.rpp = $routeParams.rpp;

        $scope.predicate = 'id';
        $scope.reverse = false;
        $scope.orderCliente = function (predicate) {
            $scope.predicate = predicate;
            $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
        };

        $scope.getListaCursos = function () {
            serverService.getDataFromPromise(serverService.promise_getListaCursos()).then(function (data) {
                redirectService.checkAndRedirect(data);
                if (data.status != 200) {
                    $scope.statusListaCursos = "Error en la recepción de datos del servidor";
                } else {
                    $scope.listaCursos = data.message;
                    for (var i = 0; i < $scope.listaCursos.length; i++) {
                        if (i > 0) {
                            if ($scope.listaCursos[i].obj_curso.id == $scope.listaCursos[i - 1].obj_curso.id) {
                                $scope.listaCursos[i - 1].alProfesores.push($scope.listaCursos[i].alProfesores[0]);
                                $scope.listaCursos.splice(i, 1);
                                i = i - 1;
                            }
                        }
                    }

                }
            });
        };



        //$scope.rppPad = serverService.getNrppBar($scope.ob, $scope.op, $scope.numpage, $scope.rpp);

//        $scope.order = $routeParams.order;
//        $scope.ordervalue = $routeParams.value;
//
//        $scope.filter = $routeParams.filter;
//        $scope.filteroperator = $routeParams.filteroperator;
//        $scope.filtervalue = $routeParams.filtervalue;
//
//        $scope.systemfilter = $routeParams.systemfilter;
//        $scope.systemfilteroperator = $routeParams.systemfilteroperator;
//        $scope.systemfiltervalue = $routeParams.systemfiltervalue;


        $scope.order = "";
        $scope.ordervalue = "";
        $scope.filter = "id";
        $scope.filteroperator = "like";
        $scope.filtervalue = "";
        $scope.systemfilter = "";
        $scope.systemfilteroperator = "";
        $scope.systemfiltervalue = "";
        $scope.params = "";
        $scope.paramsWithoutOrder = "";
        $scope.paramsWithoutFilter = "";
        $scope.paramsWithoutSystemFilter = "";




        if ($routeParams.order && $routeParams.ordervalue) {
            $scope.order = $routeParams.order;
            $scope.ordervalue = $routeParams.ordervalue;
            $scope.orderParams = "&order=" + $routeParams.order + "&ordervalue=" + $routeParams.ordervalue;
            $scope.paramsWithoutFilter += $scope.orderParams;
            $scope.paramsWithoutSystemFilter += $scope.orderParams;
        } else {
            $scope.orderParams = "";
        }

        if ($routeParams.filter && $routeParams.filteroperator && $routeParams.filtervalue) {
            $scope.filter = $routeParams.filter;
            $scope.filteroperator = $routeParams.filteroperator;
            $scope.filtervalue = $routeParams.filtervalue;
            $scope.filterParams = "&filter=" + $routeParams.filter + "&filteroperator=" + $routeParams.filteroperator + "&filtervalue=" + $routeParams.filtervalue;
            $scope.paramsWithoutOrder += $scope.filterParams;
            $scope.paramsWithoutSystemFilter += $scope.filterParams;
        } else {
            $scope.filterParams = "";
        }

        if ($routeParams.systemfilter && $routeParams.systemfilteroperator && $routeParams.systemfiltervalue) {
            $scope.systemFilterParams = "&systemfilter=" + $routeParams.systemfilter + "&systemfilteroperator=" + $routeParams.systemfilteroperator + "&systemfiltervalue=" + $routeParams.systemfiltervalue;
            $scope.paramsWithoutOrder += $scope.systemFilterParams;
            $scope.paramsWithoutFilter += $scope.systemFilterParams;
        } else {
            $scope.systemFilterParams = "";
        }

        $scope.params = ($scope.orderParams + $scope.filterParams + $scope.systemFilterParams);
        //$scope.paramsWithoutOrder = $scope.paramsWithoutOrder.replace('&', '?');
        //$scope.paramsWithoutFilter = $scope.paramsWithoutFilter.replace('&', '?');
        //$scope.paramsWithoutSystemFilter = $scope.paramsWithoutSystemFilter.replace('&', '?');
        $scope.params = $scope.params.replace('&', '?');

        $scope.getCursosPage = function () {
            serverService.getDataFromPromise(serverService.promise_getSomeUsuariocursoXUsuario($scope.ob, $scope.rpp, $scope.numpage, $scope.filterParams, $scope.orderParams, $scope.systemFilterParams)).then(function (data) {
                redirectService.checkAndRedirect(data);
                if (data.status != 200) {
                    $scope.status = "Error en la recepción de datos del servidor";
                } else {
                    $scope.pages = data.message.pages.message;
                    if (parseInt($scope.numpage) > parseInt($scope.pages))
                        $scope.numpage = $scope.pages;

                    $scope.page = data.message.page.message;
                    $scope.registers = data.message.registers.message;
                    $scope.status = "";
                }
            });
        }
         $scope.getCursosPage();



//        $scope.pages = serverService.getPages($scope.ob, $scope.rpp, null, null, null, null, null, null).then(function (datos5) {
//            $scope.pages = data['data'];
//            if (parseInt($scope.page) > parseInt($scope.pages))
//                $scope.page = $scope.pages;
//            //$location.path( "#/clientes/" +$scope.pages + "/" + $scope.pages);
//        });


//        $scope.$watch('pages', function () {
//            $scope.$broadcast('myApp.construirBotoneraPaginas');
//        }, true)
//

        $scope.getRangeArray = function (lowEnd, highEnd) {
            var rangeArray = [];
            for (var i = lowEnd; i <= highEnd; i++) {
                rangeArray.push(i);
            }
            return rangeArray;
        };
        $scope.evaluateMin = function (lowEnd, highEnd) {
            return Math.min(lowEnd, highEnd);
        };
        $scope.evaluateMax = function (lowEnd, highEnd) {
            return Math.max(lowEnd, highEnd);
        };

        $scope.dofilter = function () {
            if ($scope.filter != "" && $scope.filteroperator != "" && $scope.filtervalue != "") {
                //console.log('#/' + $scope.ob + '/' + $scope.op + '/' + $scope.numpage + '/' + $scope.rpp + '?filter=' + $scope.filter + '&filteroperator=' + $scope.filteroperator + '&filtervalue=' + $scope.filtervalue + $scope.paramsWithoutFilter);
                if ($routeParams.order && $routeParams.ordervalue) {
                    if ($routeParams.systemfilter && $routeParams.systemfilteroperator) {
                        $location.path($scope.ob + '/' + $scope.op + '/' + $scope.numpage + '/' + $scope.rpp).search('filter', $scope.filter).search('filteroperator', $scope.filteroperator).search('filtervalue', $scope.filtervalue).search('order', $routeParams.order).search('ordervalue', $routeParams.ordervalue).search('systemfilter', $routeParams.systemfilter).search('systemfilteroperator', $routeParams.systemfilteroperator).search('systemfiltervalue', $routeParams.systemfiltervalue);
                    } else {
                        $location.path($scope.ob + '/' + $scope.op + '/' + $scope.numpage + '/' + $scope.rpp).search('filter', $scope.filter).search('filteroperator', $scope.filteroperator).search('filtervalue', $scope.filtervalue).search('order', $routeParams.order).search('ordervalue', $routeParams.ordervalue);
                    }
                } else {
                    $location.path($scope.ob + '/' + $scope.op + '/' + $scope.numpage + '/' + $scope.rpp).search('filter', $scope.filter).search('filteroperator', $scope.filteroperator).search('filtervalue', $scope.filtervalue);
                }
            }
            return false;
        };
        //$scope.$on('myApp.construirBotoneraPaginas', function () {
        //    $scope.botoneraPaginas = serverService.getPaginationBar($scope.ob, $scope.op, $scope.page, $scope.pages, 2, $scope.rpp);
        //})
//
//        $scope.prettyFieldNames = serverService.getPrettyFieldNames($scope.ob).then(function (datos4) {
//            datos4['data'].push('acciones');
//            $scope.prettyFieldNames = datos4['data'];
//        });
//
//        $scope.clientes = serverService.getPage($scope.ob, $scope.page, null, null, $scope.rpp, null, null, null, null, null, null).then(function (datos3) {
//            $scope.clientes = datos3['list'];
//
//        });
//
//        $scope.fieldNames = serverService.getFieldNames($scope.ob).then(function (datos6) {
//            $scope.fieldNames = datos6['data'];
//            $scope.selectedFilterFieldName = null;
//        });
//
//
//        $scope.$watch('numPagina', function () {
//            $scope.$broadcast('myApp.construirPagina');
//        }, true)
//
//        $scope.$on('myApp.construirPagina', function () {
//
//            $scope.clientes = serverService.getPage($scope.ob, $scope.page, null, null, $scope.rpp, null, null, null, null, null, null).then(function (datos3) {
//                $scope.clientes = datos3['list'];
//
//            });
//
//        })
//
//        $scope.filtrar = function () {
//            alert("f")
//
//
//        };

// $scope.$watch('filteroperator', function () {
//           console.log($scope.filter);
//           console.log($scope.filteroperator);
//           console.log($scope.filtervalue);
//        }, true)


        $scope.newObj = function (args) {
            $scope.objEdit = {};
            $scope.position = undefined;
            switch (args.strClass) {
                case 'curso':
                    $scope.objEdit.id = 0;
                    break;
                case 'inscribirse':
                    $scope.mostrarPass={};
                    if (sharedSpaceService.getFase() == 0) {
                        serverService.getDataFromPromise(serverService.promise_getOne('curso', args.id)).then(function (data) {
                            $scope.objEdit = data.message;
                            $scope.objEdit.password;
                            
                            $scope.mostrarPass.mostrar=true;
                            $scope.mostrarPass.idCurso=args.id;
                        });
                    } else {
                        $scope.objEdit = sharedSpaceService.getObject();
                        sharedSpaceService.setFase(0);
                    }
                    break;
            }
            ;
        };

        $scope.edit = function (args) {
            $scope.objEdit = {};
            switch (args.strClass) {
                case 'curso':
                    $scope.position = $scope.page.alUsuariocursos.indexOf(args.levelOne);
                    if (sharedSpaceService.getFase() == 0) {
                        serverService.getDataFromPromise(serverService.promise_getOne(args.strClass, args.id)).then(function (data) {
                            $scope.objEdit = data.message;
                            //date conversion
                        });
                    } else {
                        $scope.objEdit = sharedSpaceService.getObject();
                        sharedSpaceService.setFase(0);
                    }
                    break;
            }
            ;
        };

        $scope.save = function (type) {
            switch (type) {
                case 'curso':
                    serverService.getDataFromPromise(serverService.promise_setCurso('curso', {json: JSON.stringify(serverService.array_identificarArray($scope.objEdit))})).then(function (data) {
                        redirectService.checkAndRedirect(data);
                        if (data.status = 200) {
                            $scope.result = data;
                            if ($scope.position !== undefined) {
                                $scope.page.alUsuariocursos[$scope.position].obj_curso = $scope.objEdit;
                                $scope.position = undefined;
                            } else {
                                $scope.objFinal = {};
                                $scope.objFinal.id = $scope.result.message.idUsuarioCurso;
                                $scope.objFinal.obj_curso = {id: $scope.result.message.idCurso};
                                $scope.objFinal.obj_curso.titulo = $scope.objEdit.titulo;
                                $scope.objFinal.obj_curso.descripcion = $scope.objEdit.descripcion;
                                $scope.page.alUsuariocursos.push($scope.objFinal);
                            }
                        }
                    });
                    break;
                case 'inscribirse':
                    serverService.getDataFromPromise(serverService.promise_setUsuariocurso('usuariocurso', {json: JSON.stringify(serverService.array_identificarArray($scope.objEdit))})).then(function (data) {
                        redirectService.checkAndRedirect(data);
                        if (data.status = 200) {
                            $scope.getCursosPage();
                            $scope.mostrarContraseña.mostrar=undefined;
                            $scope.mostrarContraseña.idCurso=undefined;
                        }
                    });
                    break;
            }
            ;

        };

        $scope.remove = function (type, id, idUsuarioCurso) {
            switch (type) {
                case 'curso':
                    serverService.getDataFromPromise(serverService.promise_removeCurso(type, id, idUsuarioCurso)).then(function (data) {
                        redirectService.checkAndRedirect(data);
                        if (data.status = 200) {
                            $scope.result = data;
                            $scope.page.alUsuariocursos.splice($scope.position, 1);
                            $scope.position = undefined;
                        }
                    });
                    break;
            }
        };

    }]);