(function () {
    'use strict';

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

    toolBar.$inject = ['$timeout', '$location', '__env'];

    /* @ngInject */
    function toolBar($timeout, $location, __env) {
        var directive = {
            restrict: 'E',
            replace: true,
            templateUrl: __env.appPath + 'scripts/components/tool-bar-index/tool-bar-index.directive.html',
            scope: false,
            link: link
        };

        function link(scope, element, attr, ctrl) {
            scope.toggleTool = function ($event) {
                var 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', function () {
                    element.find('.tool.active').removeClass('open');
                });
            };

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

            scope.toggleToolItem = function (trigger) {
                $(trigger).siblings().removeClass('active', 'open');
                $(trigger).addClass('active', '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);
            });

            scope.setCursor("cursor-pointer");
        }

        return directive;

    }


})();
