(function () {
    'use strict';

    angular
        .module('editorRotas')
        .directive('mainFooter', mainFooter);

    mainFooter.$inject = ['$location', '$window', '__env', '$routeParams', 'ngNotify', 'Observable'];

    function mainFooter($location, $window, __env, $routeParams, ngNotify, Observable) {
        var directive = {
            restrict: 'EA',
            replace: true,
            scope: false,
            templateUrl: __env.appPath + 'scripts/components/main-footer/main-footer.directive.html',
            link: link,
        };

        return directive;

        function link(scope, element, attr, ctrl) {
            angular.extend(scope.vm, {
                habilitarBotoes: habilitarBotoes,
                navegar: navegar,
                excluirRotaRodape: excluirRotaRodape,
            });

            scope.vm.RotaObservable = new Observable();

            scope.vm.viewHasChanged = false;

            scope.vm.isValid = false;

            function changeState(state) {
                switch (state) {
                    case 'disable':
                        scope.vm.viewHasChanged = false;
                        break;
                    case 'enable':
                        scope.vm.viewHasChanged = true;
                        break;
                }

                if (!scope.$$phase)
                    scope.$apply();
            }

            $window.onbeforeunload = function (event) {
                if (scope.vm.viewHasChanged) {
                    let message = "You have not saved your changes.";
                    let mEvent = event || window.event;

                    if (mEvent) {
                        mEvent.returnValue = message;
                    }
                    return message;
                }
            };

            scope.vm.RotaObservable.subscribe(changeState);

            function excluirRotaRodape() {
                let rota = {
                    Codigo: $routeParams.CodigoRota,
                    RotasTemp: $routeParams.rt,
                    Status: 1
                };

                scope.vm.abrirModalExcluirRota(rota);
            }



            scope.etapa = { url: "" };
            let path = $location.path();

            function navegar($event) {
                var trigger = $event.currentTarget;
                var etapa = $(trigger).data('etapa');
                if ($(trigger).hasClass('locked')) return false;

                if ($routeParams.rt === "false" && scope.vm.informacoesRotaAtual.Etapa === "I" && etapa != 3)
                    ngNotify.set('Essa ação não é permitida. Para editar, clone a rota.', { type: 'warn', sticky: true, button: true });
                else
                    window.location.href = urlGlobal + "EditorRotas/Etapa" + etapa + "/?CodigoSetor=" + $routeParams.CodigoSetor + "&CodigoRota=" + $routeParams.CodigoRota + "&rt=" + $routeParams.rt;
            }

            function habilitarBotoes(estado) {
                scope.nav2 = true;
                scope.nav3 = true;
                switch (estado) {
                    case "T": scope.nav2 = false; scope.nav3 = false; break; //Etapa 1
                    case "S":
                    case "G": scope.nav3 = false; break; //Etapa 2
                    case "I": break; //Etapa 4
                    default: scope.nav2 = false; scope.nav3 = false; break;
                }
            }

            if ($routeParams.rt === "false" && path[18] !== "3") return;

            scope.vm.permiteExcluir = true;
            switch (path[18]) {
                case "1": scope.etapa.url = "etapa1.html"; break;
                case "2": scope.etapa.url = "etapa2.html"; break;
                case "3":
                    scope.etapa.url = "etapa3.html";
                    if ($routeParams.rt == "false") {
                        scope.vm.permiteClonar = true;
                        scope.vm.permiteExcluir = false;
                    }
                    break;
                default: break;
            }
        }
    }
})();
