Source: managers/SubDetalhamentoManager.js

/**
 * @ngdoc managers
 * @name SubDetalhamentoManager
 * @module s4c.managers.SubDetalhamentoManager
 *
 * @description
 * `SubDetalhamentoManager` Componente para comunicação entre uma página html qualquer e o controller da diretiva
 * 
 * 
 */
(function () {
    'use strict';

    function SubDetalhamentoManager(MainState, MapaService) {
        return {
            'ativo': false,
            'top': 10,
            'right': 10,
            'titulo': 'Sub item',
            'data': {},
            'camerasProximas': [],
            'camerasMarcadas': new L.LayerGroup([]),
            'subItens': new L.LayerGroup([]),
            'zindex': 30,
            /**
             * @method fechar
             */
            'fechar': function () {
                this.ativo = false;

                var SubDetalhamentoManager = MainState.getManager('SubDetalhamentoManager');

                MapaService.limparSubItem();
                SubDetalhamentoManager.resetarSubDetalhamento();
            },
            /**
             * @method marcarCamerasProximas
             * @param {*} cameras 
             */
            'marcarCamerasProximas': function (cameras) {

                var SubDetalhamentoManager = MainState.getManager('SubDetalhamentoManager');

                MapaService.removerLayers(SubDetalhamentoManager.camerasMarcadas.getLayers());
                SubDetalhamentoManager.camerasMarcadas.clearLayers();

                _.each(cameras, function (camera) {
                    var y = JSON.parse(camera.geojson);
                    var x = JSON.parse(SubDetalhamentoManager.data.geojson);

                    MapaService.adicionarLinha(x.coordinates.reverse(), y.coordinates.reverse());
                });
            },
            /**
             * @method abrirPoi
             * @param {*} id 
             * @param {*} obj 
             * @param {*} tipo 
             */
            'abrirPoi': function (id, obj, tipo) {
                this.ativo = true;

                var DetalhamentoManager = MainState.getManager('DetalhamentoManager');
                var subdetalhamento = MainState.getDirective('subdetalhamento');

                this.top = DetalhamentoManager.top + 4;
                this.right = DetalhamentoManager.right + 4;

                subdetalhamento.buscarPoi(id, obj, tipo);
            },
            /**
             * @method abrirViatura
             * @param {*} camada 
             */
            'abrirViatura': function (camada) {
                this.ativo = true;

                var DetalhamentoManager = MainState.getManager('DetalhamentoManager');
                var subdetalhamento = MainState.getDirective('subdetalhamento');
                this.top = $scope.DetalhamentoManager.top + 4;
                this.right = $scope.DetalhamentoManager.right + 4;

                subdetalhamento.carregarViatura(camada);
            },
            /**
             * @method abrirIncidente
             * @param {*} id 
             * @param {*} obj 
             * @param {*} marker 
             */
            'abrirIncidente': function (id, obj, marker) {
                this.ativo = true;

                var subdetalhamento = MainState.getDirective('subdetalhamento');
                var MSIManager = MainState.getManager('MSIManager');

                subdetalhamento.buscarIncidente(id, obj, marker);
                MSIManager.gotoIncidente(id);
            },
            /**
             * @method resetarSubDetalhamento
             */
            'resetarSubDetalhamento': function () {
                var SubDetalhamentoManager = MainState.getManager('SubDetalhamentoManager');

                MapaService.removerLayers(SubDetalhamentoManager.subItens.getLayers());
                MapaService.removerLayers(SubDetalhamentoManager.camerasMarcadas.getLayers());
                SubDetalhamentoManager.subItens.clearLayers();
                SubDetalhamentoManager.camerasMarcadas.clearLayers();
            },
            /**
             * @method voarParaSubItem
             * @param {*} data 
             * @param {*} obj 
             */
            'voarParaSubItem': function (data, obj) {
                var SubDetalhamentoManager = MainState.getManager('SubDetalhamentoManager');
                MapaService.removerLayers(SubDetalhamentoManager.camerasMarcadas.getLayers());
                SubDetalhamentoManager.camerasMarcadas.clearLayers();

                var coords;

                if (typeof obj.geojson === 'string') {
                    coords = JSON.parse(obj.geojson);
                    coords = turf.point([coords.coordinates[0], coords.coordinates[1]]).geometry;
                } else {
                    coords = obj.geojson;
                    coords = turf.point([obj.geojson.coordinates[0], obj.geojson.coordinates[1]]).geometry;
                }
                var coords2;
                if (data.viatura) {
                    coords2 = data.geojson;
                    if (coords2 != null && coords2.geometry != null && coords2.geometry.coordinates != null) {
                        coords2 = turf.point([data.geo_point.lon, data.geo_point.lat]).geometry;
                    } else if (coords2 != null && coords2.coordinates != null) {
                        coords2 = turf.point([data.geojson.coordinates[0], data.geojson.coordinates[1]]).geometry;
                    }
                } else {
                    coords2 = JSON.parse(data.geojson);
                }


                var distancia = turf.distance(
                    turf.point([coords.coordinates[1], coords.coordinates[0]]),
                    turf.point([coords2.coordinates[1], coords2.coordinates[0]]),
                    'miles');

                MapaService.adicionarSubItem(coords, coords2, (distancia * 1609).toFixed(0) + ' m', SubDetalhamentoManager.subItens.getLayers());
            }
        };
    }

    SubDetalhamentoManager.$inject = ['MainState', 'MapaService'];

    angular.module('s4c.managers')
        .service('SubDetalhamentoManager', SubDetalhamentoManager);
})();