(function () {
    'use strict';

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

    toolBar.$inject = ['ferramentaMedirDistancia', 'ferramentaContinuarItinerario', 'ferramentaApagarTrecho', 'ferramentaAjustarTrecho', 'ferramentaInicioFimColeta',
        'ferramentaPacmans'];

    function toolBar(ferramentaMedirDistancia, ferramentaContinuarItinerario, ferramentaApagarTrecho, ferramentaAjustarTrecho, ferramentaInicioFimColeta,
        ferramentaPacmans) {

        var directive = {
            restrict: 'E',
            replace: true,
            templateUrl: 'angular/EditorRotas/app/scripts/components/tool-bar/tool-bar.directive.html',
            link: link
        };

        function link(scope, element, attr, ctrl) {
            angular.extend(scope.vm, {
                mostraValorScope: mostraValorScope,
                clearToolbar: clearToolbar
            });

            scope.elementTool = element;
            scope.toggleTool = function ($event) {
                let trigger = $event.target;
                element.find('.tool.active').addClass('open');
                if (element.find('.tool.active').children().hasClass(trigger.className)) return
                element.find('.tool.active').removeClass('open');
                element.find('.tool.active').removeClass('active');
                if ($(trigger).parent().hasClass('disabled')) { return; }
                $(trigger).parent().addClass('active');
                $(trigger).parent().addClass('open');
                $('#mapa').on('click', () => { element.find('.tool.active').removeClass('open'); });
                scope.Borracha = false;

                switch (trigger.className) {
                    case 'icon-pen':
                        DesablitaFerramentas();
                        ferramentaMedirDistancia.Desabilita();
                        ferramentaContinuarItinerario.Habilita();
                        break;
                    case 'icon-erase':
                        DesablitaFerramentas();
                        ferramentaApagarTrecho.Habilita();
                        break;
                    case 'icon-ruler':
                        DesablitaFerramentas();
                        ferramentaContinuarItinerario.Desabilita();
                        ferramentaMedirDistancia.Habilita();
                        break;
                    case 'icon-track':
                        DesablitaFerramentas();
                        ferramentaInicioFimColeta.Habilita();
                        ferramentaInicioFimColeta.SetCenterInicioFim(true);
                        break;
                    case 'icon-bezier':
                        DesablitaFerramentas();
                        ferramentaAjustarTrecho.Habilita('Selecionar');
                        break;
                    case 'icon-pointer':
                        DesablitaFerramentas();
                        scope.setCursor($(trigger).data('setcursor'));
                        break;
                    default: break;
                }
            };

            function clearToolbar($event) {
                // scope.Borracha = false;
                element.find('.tool.active').addClass('open');
                element.find('.tool.active').removeClass('open');
                element.find('.tool.active').removeClass('active');
                $('#mapa').on('click', () => { element.find('.tool.active').removeClass('open'); });
            }

            function mostraValorScope() {
                if (!scope.vm.informacoesRotaAtual.RotaTemp) {
                    scope.vm.habilitarHorarioColeta(false);
                    ferramentaPacmans.habilitaPacmans(false);
                }
                else {
                    scope.vm.habilitarHorarioColeta(true);
                    ferramentaPacmans.habilitaPacmans(true);
                }
            }

            scope.closeTool = function ($event) {
                $event.stopPropagation();
                let trigger = $event.target;
                $(trigger).closest('.tool').removeClass('open');
            };

            scope.toggleToolItem = function (trigger) {
                $(trigger).siblings().removeClass('active');
                $(trigger).siblings().removeClass('open');
                $(trigger).addClass('active');
                $(trigger).addClass('open');
            };

            scope.setCursor = function (cursor) {
                $('.map-canvas').attr('data-cursor', cursor)
            };

            scope.callOtherActionExample = function () {

            };

            $(document).on('click', '.tool-content .default-list li', function () {
                var trigger = $(this);
                scope.toggleToolItem(trigger);
            });

            function DesablitaFerramentas() {
                if (scope.vm.permissions.indexOf('medirDistancia') !== -1) ferramentaMedirDistancia.Desabilita();
                if (scope.vm.permissions.indexOf('continuarItinerario') !== -1) ferramentaContinuarItinerario.Desabilita();
                if (scope.vm.permissions.indexOf('inicioFimColeta') !== -1) ferramentaInicioFimColeta.Desabilita();
                if (scope.vm.permissions.indexOf('apagarTrecho') !== -1) ferramentaApagarTrecho.Desabilita();
            }
            scope.setCursor("cursor-pointer");
        }

        return directive;

    }
})();
