(function () {
    'use strict';

    angular
        .module('contestacaoApp')
        .factory('ObjetoMapaReferencia', ObjetoMapaReferencia);

    ObjetoMapaReferencia.$inject = ['InfoWindowReferencia', 'mapsSingletonService'];

    /* @ngInject */
    function ObjetoMapaReferencia(InfoWindowReferencia, mapsSingletonService) {

        function ObjetoMapaReferencia(data, codigoRef) {
            this.codigoReferencia = codigoRef;
            this.lat = data.latitude;
            this.lng = data.longitude;
            this.raio = data.raio;
            this.area = data.area;
            this.codigoTipo = data.codigoTipo;
            this.tipoReferencia = data.tipoReferencia;
            this.mapa = null;
            this.infoWindow = new InfoWindowReferencia(data, this.codigoReferencia);
        }

        ObjetoMapaReferencia.prototype.inicializar = function (mapa) {
            this.mapa = mapsSingletonService.getMap();

            if (this.area && this.area.length) {
                this.plotarPoligono();
            } else this.plotarMarker();

            this.infoWindow.inicializar();
        };

        ObjetoMapaReferencia.prototype.plotarPoligono = function () {
            this.area.forEach(area => {
                const path = area.coordenadas.map(x => [x.latitude / 1000000, x.longitude / 1000000]);
                const options = new InlogMaps.PolygonOptions(path, 1, false);
                options.fillColor = '#3e94ff';
                options.fillOpacity = 0.4;
                options.color = '#1c3d6b';
                options.object = this.codigoReferencia;

                this.mapa.drawPolygon('referencia', options, selecionarObjetoArea.bind(this));
            });
        };

        ObjetoMapaReferencia.prototype.plotarMarker = async function () {
            const position = [this.lat / 1000000, this.lng / 1000000];

            const circleOptions = new InlogMaps.CircleOptions(position, this.raio);
            circleOptions.weight = 4;
            circleOptions.addToMap = false;
            circleOptions.fillOpacity = 0.4;
            circleOptions.fillColor = '#3e94ff';
            circleOptions.color = '#1c3d6b';
            circleOptions.opacity = 0.8;
            circleOptions.object = this.codigoReferencia;

            this.mapa.drawCircle('referencia', circleOptions);

            const iconUrl = `${__env.relativeUrl}Setores/SetoresSource/ImagemTipoReferencia?codigoTipoReferencia=` +
                `${this.codigoTipo}&tipoReferencia=${this.tipoReferencia}`;
            const iconSize = await mapsSingletonService.getIconSize(iconUrl);

            const markerOptions = new InlogMaps.MarkerOptions(position);
            markerOptions.icon = new InlogMaps.MarkerIcon(iconUrl, iconSize);
            markerOptions.object = this.codigoReferencia
            this.mapa.drawMarker('referencia', markerOptions, selecionarObjetoMarker.bind(this));
        };

        ObjetoMapaReferencia.prototype.exibirObjetoMapa = function () {
            this.mapa.toggleCircles(true, 'referencia', (data) => data == this.codigoReferencia);
            this.mapa.togglePolygons(true, 'referencia', (data) => data == this.codigoReferencia);
            this.mapa.toggleMarkers(true, 'referencia', (data) => data == this.codigoReferencia);
        };

        ObjetoMapaReferencia.prototype.ocultarObjetoMapa = function () {
            this.mapa.toggleCircles(false, 'referencia', (data) => data == this.codigoReferencia);
            this.mapa.togglePolygons(false, 'referencia', (data) => data == this.codigoReferencia);
            this.mapa.toggleMarkers(false, 'referencia', (data) => data == this.codigoReferencia);

            this.mapa.closePopup('referencia');
        };

        function selecionarObjetoMarker() {
            this.infoWindow.exibirInfoWindowMarker();
        }

        function selecionarObjetoArea(object, event) {
            this.infoWindow.exibirInfoWindowPolygon(event);
        }

        return ObjetoMapaReferencia;
    }
})();
