app.controller("localizacaoController", function ($scope, $modal, $http, $rootScope, localizacaoFactory, notificationFactory) {
    $scope.edificios = [];
    $scope.pisos = [];
    $scope.salas = [];

    
    // Edificios
    
    $scope.carregarEdificios = function (cadastro) {
        var getSuccessCallback = function (data, status) {

            
            $scope.edificios = data;
            if (cadastro != null)
                $scope.seleccionarActual(cadastro);

            $scope.devolveLocalizacao(cadastro, $scope.selectedEdificio, $scope.selectedPiso, $scope.selectedSala);
        };

        

        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        localizacaoFactory.getEdificios().success(getSuccessCallback).error(errorCallback);
    };

  

    $scope.inserirEdificio = function (item) {

        if (item == null)
            return;

        var successCallback = function (data, status, headers, config) {
            
            notificationFactory.success(data.Msg);
            
            $scope.edificios.push(data.Conteudo);
            //$scope.carregarEdificios();
           
        };


        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data.Msg);
        };

        localizacaoFactory.addEdificio(item).success(successCallback).error(errorCallback);

    };


    $scope.apagarEdificio = function (elem) {
        if (elem == null)
            return;



        var successCallback = function (data, status, headers, config) {
            notificationFactory.success(data);
            $scope.selectedEdificio = null;
            $scope.selectedPiso = null;
            $scope.selectedSala = null;
            $scope.carregarEdificios();
        };


        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        localizacaoFactory.deleteEdificio(elem).success(successCallback).error(errorCallback);
    };

    $scope.updateEdificio = function (elem,cadastro) {
        if (elem == null)
            return;



        var successCallback = function (data, status, headers, config) {
            notificationFactory.success(data);

            if (cadastro != null)
                $scope.atribuirLoc(cadastro);

        };


        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        localizacaoFactory.updateEdificio(elem).success(successCallback).error(errorCallback);
    };

    $scope.selEdificio = function (cadastro,item) {
        $scope.selectedEdificio = item;
        $scope.devolveLocalizacao(cadastro, $scope.selectedEdificio, $scope.selectedPiso, $scope.selectedSala);
        
    };

    $scope.edificioEditRow = function (row,cadastro) {

        if (row == null) {
            row = {
                PK_Edificio: 0,
                Descricao: 'Edificio'
            };
            row.op = "Novo";
        }
        else
            row.op = "Editar";

        var modalInstance = $modal.open(
        {

            templateUrl: 'views/modalEditarEdificio.html?v='+new Date(),
            controller: 'modalController',
            resolve: {

                items: function () { return row; },

                //  isNew: false
            }
        });

        modalInstance.result.then(function (item) {
            if (item.op == "Editar") {
                $scope.selectedEdificio = item;
                $scope.updateEdificio(item);
            }
            else
                $scope.inserirEdificio(item);

        }, function () {

        });

    };

    $scope.confirmarApagarEdificio = function (row) {

        if (row == null)
            return;

        var modalInstance = $modal.open(
        {

            templateUrl: 'views/modalConfirmarApagar.html?v=' + new Date(),
            controller: 'modalController',
            resolve: {

                items: function () { return row; },

                //  isNew: false
            }
        });

        modalInstance.result.then(function (item) {

            $scope.apagarEdificio(item);
        }, function () {

        });

    };

    //Pisos
    $scope.inserirPiso = function (item) {

        if (item == null)
            return;
        var successCallback = function (data, status, headers, config) {
            notificationFactory.success(data.Msg);

            $scope.selectedEdificio.LstPiso.push(data.Conteudo);
            //$scope.carregarEdificios();
            
            
        };


        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        localizacaoFactory.addPiso(item).success(successCallback).error(errorCallback);

    };


    $scope.apagarPiso = function (elem) {
        if (elem == null)
            return;
        var successCallback = function (data, status, headers, config) {
            notificationFactory.success(data);

            
            $scope.selectedPiso.LstSala = [];
            var arr = $scope.selectedEdificio.LstPiso;
            
            if (arr == null)
                return;
            for (i = 0; i < arr.length; i++) {
                if (arr[i] == elem) {
                    arr.splice(i, 1);
                    break;

                }
            }
            $scope.selectedSala = null;
            $scope.selectedPiso = null;
            
        };


        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        localizacaoFactory.deletePiso(elem).success(successCallback).error(errorCallback);
    };

    $scope.updatePiso = function (elem) {
        
        if (elem == null)
            return;
        var successCallback = function (data, status, headers, config) {
            notificationFactory.success(data);

        };

        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        localizacaoFactory.updatePiso(elem).success(successCallback).error(errorCallback);
    };

    $scope.selPiso = function (cadastro,item) {
        $scope.selectedPiso = item;
       
        $scope.devolveLocalizacao(cadastro, $scope.selectedEdificio, $scope.selectedPiso, $scope.selectedSala);
    };

    $scope.pisoEditRow = function (row) {

        if ($scope.selectedEdificio == null) {
            notificationFactory.error("Seleccione o Edificio");
            return;
        }
            

        if (row == null) {
           

            row = {
                PK_Piso: 0,
                FK_Edificio:$scope.selectedEdificio.PK_Edificio,
                Descricao: 'Piso'
            };
            row.op = "Novo";
        }
        else
        {
            if ($scope.selectedPiso == null)
            {
                notificationFactory.error("Seleccione o Piso");
                return;
            }
                
            row.op = "Editar";
        }
            

        var modalInstance = $modal.open(
        {

            templateUrl: 'views/modalEditarPiso.html?v='+new Date(),
            controller: 'modalController',
            resolve: {

                items: function () { return row; },

                //  isNew: false
            }
        });

        modalInstance.result.then(function (item) {
            
            if (item.op == "Editar") {
                
                $scope.selectedPiso = item;
                $scope.updatePiso(item);
            }
            else
                $scope.inserirPiso(item);
            
        }, function () {

        });

    };

    $scope.confirmarApagarPiso = function (row) {

        if (row == null)
            return;

        var modalInstance = $modal.open(
        {

            templateUrl: 'views/modalConfirmarApagar.html?v=' + new Date(),
            controller: 'modalController',
            resolve: {

                items: function () { return row; },

                //  isNew: false
            }
        });

        modalInstance.result.then(function (item) {

            $scope.apagarPiso(item);
        }, function () {

        });

    };



    //Sala
  
    $scope.selSala = function (cadastro,item) {
        $scope.selectedSala = item;
        $scope.devolveLocalizacao(cadastro, $scope.selectedEdificio, $scope.selectedPiso, $scope.selectedSala);
    };

    $scope.salaEditRow = function (row) {

        if ($scope.selectedEdificio == null || $scope.selectedPiso == null) {
            notificationFactory.error("Seleccione a Piso");
            return;
        }

        if (row == null) {
            row = {
                PK_Sala: 0,
                FK_Piso: $scope.selectedPiso.PK_Piso,
                Descricao: 'Sala'
            };
            row.op = "Nova";
        }
        else
        {
            if ($scope.selectedSala == null)
            {
                notificationFactory.error("Seleccione a Sala");
                return;
            }
            row.op = "Editar";
        }
            

        var modalInstance = $modal.open(
        {

            templateUrl: 'views/modalEditarSala.html?v='+new Date(),
            controller: 'modalController',
            resolve: {

                items: function () { return row; },

                //  isNew: false
            }
        });

        modalInstance.result.then(function (item) {
            
            if (item.op == "Editar") {
                
                $scope.selectedSala = item;
                $scope.updateSala(item);
            }
            else
                $scope.inserirSala(item);
            
        }, function () {

        });

    };

    $scope.confirmarApagarSala = function (row) {

        if (row == null)
            return;

        var modalInstance = $modal.open(
        {

            templateUrl: 'views/modalConfirmarApagar.html?v=' + new Date(),
            controller: 'modalController',
            resolve: {

                items: function () { return row; },

                //  isNew: false
            }
        });

        modalInstance.result.then(function (item) {

            $scope.apagarSala(item);
        }, function () {

        });

    };

    $scope.inserirSala = function (item) {

        if (item == null)
            return;

        var successCallback = function (data, status, headers, config) {
            notificationFactory.success(data.Msg);

            $scope.selectedPiso.LstSala.push(data.Conteudo);
            //$scope.carregarEdificios();
        };


        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        localizacaoFactory.addSala(item).success(successCallback).error(errorCallback);

    };


    $scope.apagarSala = function (elem) {
        if (elem == null)
            return;



        var successCallback = function (data, status, headers, config) {
            notificationFactory.success(data);
            
            //$scope.carregarSalas();
            var arr = $scope.selectedPiso.LstSala;
            if (arr == null)
                return;
            for (i = 0; i < arr.length; i++) {
                if (arr[i] == elem) {
                    arr.splice(i, 1);
                    break;

                }
            }
            $scope.selectedSala = null;
            
        };


        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        localizacaoFactory.deleteSala(elem).success(successCallback).error(errorCallback);
    };

    $scope.updateSala = function (elem) {
        if (elem == null)
            return;



        var successCallback = function (data, status, headers, config) {
            notificationFactory.success(data);

        };


        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data);
        };

        localizacaoFactory.updateSala(elem).success(successCallback).error(errorCallback);
    };

    $scope.selEdificioPK = function (PK_Edificio) {
        
        
        //$scope.selectedEdificio = null;
        if ($scope.edificios == null)
            return;
        //console.log($scope.edificios.length);
        for (i = 0; i < $scope.edificios.length; i++) {
            //console.log($scope.edificios[i].PK_Edificio + " " + PK_Edificio);
            if ($scope.edificios[i].PK_Edificio == PK_Edificio) {
                
                $scope.selectedEdificio = $scope.edificios[i];
                //console.log($scope.selectedEdificio);
                i = $scope.edificios.length;
            }
            
        }
    };

    $scope.selPisoPK = function (PK_Piso) {
       
        //$scope.selectedPiso = null;
        if ($scope.selectedEdificio == null)
            return;

        for (j = 0; j < $scope.selectedEdificio.LstPiso.length; j++) {
            //console.log($scope.selectedEdificio.LstPiso[j].PK_Piso+" "+PK_Piso);
            if ($scope.selectedEdificio.LstPiso[j].PK_Piso == PK_Piso) {
                
                $scope.selectedPiso = $scope.selectedEdificio.LstPiso[j];
                j = $scope.selectedEdificio.LstPiso.length;
            }
        }
    };

    $scope.selSalaPK = function (PK_Sala) {
        
        //$scope.selectedSala = null;
        if ($scope.selectedPiso == null)
            return;

        for (k = 0; k < $scope.selectedPiso.LstSala.length; k++) {
            if ($scope.selectedPiso.LstSala[k].PK_Sala == PK_Sala) {
                $scope.selectedSala = $scope.selectedPiso.LstSala[k];
                k = $scope.selectedPiso.LstSala.length;
            }
        }
    };
    
    $scope.seleccionarActual = function (item) {
        
        if (item == null)
            return;
        
        $scope.selEdificioPK(item.FK_Edificio);
       
        $scope.selPisoPK(item.FK_Piso);
        
        $scope.selSalaPK(item.FK_Sala);
        
    };

    $scope.devolveLocalizacao = function (item,edificio,piso,sala) {
        
        if (item == null)
            return "";
        var edificioDescricao = "";
        var pisoDescricao = "";
        var salaDescricao = "";

        if (edificio == null) {
            
            item.FK_Edificio = 0;
        }
        else {
            edificioDescricao = edificio.Descricao;
            item.FK_Edificio = edificio.PK_Edificio;
        }

       
        if (piso == null) {
            item.FK_Piso = 0;
        }
        else {
            item.FK_Piso = piso.PK_Piso;
            pisoDescricao = " - " + piso.Descricao;
        }

        if (sala == null) {
            item.FK_Sala = 0;
        }
        else {
            salaDescricao = " - " + sala.Descricao;
            item.FK_Sala = sala.PK_Sala;
        }

        if (edificio!=null && piso!=null && edificio.PK_Edificio != piso.FK_Edificio)
        {
            $scope.selectedPiso = null;
            $scope.selectedSala = null;
            
            edificioDescricao = edificio.Descricao;
            pisoDescricao = "";
            salaDescricao = "";
            item.FK_Piso = 0;
            item.FK_Sala = 0;
            
        }
        
        if (edificio != null && piso != null && sala != null &&
            edificio.PK_Edificio != piso.FK_Edificio &&
            piso.PK_Piso != sala.FK_Piso)
        {
            $scope.selectedPiso = null;
            $scope.selectedSala = null;
            edificioDescricao = edificio.Descricao;
            pisoDescricao = "";
            salaDescricao = "";
            item.FK_Piso = 0;
            item.FK_Sala = 0;
        }
        if (piso!=null && sala!=null && piso.PK_Piso != sala.FK_Piso)
        {
            $scope.selectedSala = null;
            edificioDescricao = edificio.Descricao;
            pisoDescricao = pisoDescricao;
            salaDescricao = "";
            
            item.FK_Sala = 0;
        }
        $scope.caminhoLocalizacao = edificioDescricao + "" + pisoDescricao + "" + salaDescricao;
        item.Localizacao = $scope.caminhoLocalizacao;

        
    };

    $scope.showEdificios = true;
    $scope.showPisos = false;
    $scope.showSalas = false;
    $scope.tabActiva = [true, false, false];

    $scope.mostrarTab = function (n) {
        if (n == 0)
        {
            $scope.showEdificios = true;
            $scope.showPisos = false;
            $scope.showSalas = false;
            $scope.tabActiva[0] = true;
            $scope.tabActiva[1] = false;
            $scope.tabActiva[2] = false;
        }
        if (n == 1) {
            $scope.showEdificios = false;
            $scope.showPisos = true;
            $scope.showSalas = false;
            $scope.tabActiva[0] = false;
            $scope.tabActiva[1] = true;
            $scope.tabActiva[2] = false;
        }
        if (n == 2) {
            $scope.showEdificios = false;
            $scope.showPisos = false;
            $scope.showSalas = true;
            $scope.tabActiva[0] = false;
            $scope.tabActiva[1] = false;
            $scope.tabActiva[2] = true;
        }
    };

   
    $scope.mostrarInfoPisos = function (cadastro,edificio) {
        
        //$scope.selectedPiso = null;
        
        $scope.selEdificio(edificio);
        
        $scope.showEdificios = false;
        $scope.showPisos = true;
        $scope.showSalas = false;
        $scope.tabActiva[0] = false;
        $scope.tabActiva[1] = true;
        $scope.tabActiva[2] = false;
        

       
    };

    $scope.mostrarInfoSalas = function (cadastro,piso) {
        
        //$scope.selSala(null);
        $scope.selPiso(piso);
        $scope.showEdificios = false;
        
        $scope.showPisos = false;
        $scope.showSalas = true;
        $scope.tabActiva[0] = false;
        $scope.tabActiva[1] = false;
        $scope.tabActiva[2] = true;


    };

    
   
});