/**
 * Created by me664 on 10/23/19.
 */
jQuery(function($){
   // $('.bt_ot_gmap_wrap').each(function(){
        var self=$(this);
        var gmap_el=$('.bt_ot_gmap_wrap').find('.bt_ot_gmap');
        var bt_ot_gmap_input_zoom=$('.bt_ot_gmap_wrap').find('.bt_ot_gmap_input_zoom');
        var bt_ot_gmap_input_lat=$('.bt_ot_gmap_wrap').find('.bt_ot_gmap_input_lat');
        var bt_ot_gmap_input_lng=$('.bt_ot_gmap_wrap').find('.bt_ot_gmap_input_lng');
        var bt_ot_gmap_input_type=$('.bt_ot_gmap_wrap').find('.bt_ot_gmap_input_type');
        var st_token_mapbox =$('.bt_ot_gmap_wrap').find('.st_token_mapbox').val();
        var st_icon_mapbox =$('.bt_ot_gmap_wrap').find('.st_icon_mapbox').val();
        var text_rtl_mapbox =$('.bt_ot_gmap_wrap').find('.text_rtl_mapbox').val();
        if(typeof st_icon_mapbox !== 'underfind' ){
            icon_map = st_icon_mapbox;
        } else {
            icon_map = "https://i.imgur.com/MK4NUzI.png";
        }
        var bt_ot_searchbox=$('.bt_ot_gmap_wrap').find('.bt_ot_searchbox');
        var gmap_obj;
        var current_marker,old_lat=37,old_lng=2,old_zoom=17;
        var    markers=[];
        if(bt_ot_gmap_input_lat.val()){
            old_lat=bt_ot_gmap_input_lat.val();
            old_lat=parseFloat(old_lat);
        }
        if(bt_ot_gmap_input_lng.val()){
            old_lng=bt_ot_gmap_input_lng.val();
            old_lng=parseFloat(old_lng);
        }
        if(bt_ot_gmap_input_zoom.val()){
            old_zoom=bt_ot_gmap_input_zoom.val();
            old_zoom=parseFloat(old_zoom);
        }
        var parent = $('.bt_ot_gmap_wrap'),
        mapEl = $('.google-map-mapbox', parent);

        function stInitAdminGmap() {
            var mapboxLib = (typeof stGetMapGL === 'function') ? stGetMapGL() : window.mapboxgl;
            if (!mapboxLib || !mapboxLib.Map) {
                return;
            }
            if (typeof stSetMapboxAccessToken === 'function') {
                stSetMapboxAccessToken(mapboxLib);
            } else if (!stIsNeshanMap()) {
                mapboxLib.accessToken = st_token_mapbox;
            }
            if (!stIsNeshanMap() && typeof mapboxLib.setRTLTextPlugin !== 'undefined' && text_rtl_mapbox) {
                mapboxLib.setRTLTextPlugin(text_rtl_mapbox);
            }

            var mapOptions = stIsNeshanMap() && typeof stBuildNeshanMapOptions === 'function'
                ? stBuildNeshanMapOptions({
                    container: 'st_mapbox_custom',
                    lat: old_lat,
                    lng: old_lng,
                    zoom: old_zoom
                })
                : {
                container: 'st_mapbox_custom',
                style: 'mapbox://styles/mapbox/streets-v11?optimize=true',
                center: [old_lng, old_lat],
                zoom: old_zoom
            };

            if (!mapOptions) {
                return;
            }

            var map = new mapboxLib.Map(mapOptions);

            map.on('load', function () {
                map.resize();
                if (stIsNeshanMap()) {
                    var el = document.createElement('div');
                    el.className = 'st-neshan-marker';
                    el.style.backgroundImage = 'url(' + icon_map + ')';
                    el.style.backgroundSize = '100%';
                    el.style.width = '40px';
                    el.style.height = '50px';
                    new mapboxLib.Marker(el).setLngLat([old_lng, old_lat]).addTo(map);
                    return;
                }
                map.loadImage(icon_map, function (error, image) {
                    if (error) throw error;
                    map.addImage('custom-marker', image);
                    map.addLayer({
                        id: 'markers',
                        type: 'symbol',
                        source: {
                            type: 'geojson',
                            data: {
                                type: 'FeatureCollection',
                                features: [{
                                    type: 'Feature',
                                    properties: {},
                                    geometry: {
                                        type: 'Point',
                                        coordinates: [old_lng, old_lat]
                                    }
                                }]
                            }
                        },
                        layout: {
                            'icon-image': 'custom-marker'
                        }
                    });
                });
            });

            if (!stIsNeshanMap() && typeof MapboxGeocoder !== 'undefined' && document.getElementById('geocoder')) {
                var geocoder = new MapboxGeocoder({
                    accessToken: mapboxLib.accessToken,
                    mapboxgl: mapboxLib
                });
                document.getElementById('geocoder').appendChild(geocoder.onAdd(map));
                geocoder.on('result', function (result) {
                    new_long = parseFloat(result.result.center[0]);
                    new_lat = parseFloat(result.result.center[1]);
                    bt_ot_gmap_input_lat.val(new_lat);
                    bt_ot_gmap_input_lng.val(new_long);
                });
            }

            map.on('click', function (e) {
                if (!stIsNeshanMap()) {
                    return;
                }
                bt_ot_gmap_input_lat.val(e.lngLat.lat);
                bt_ot_gmap_input_lng.val(e.lngLat.lng);
            });

            $('#st-modal-show-map').on('shown.bs.modal', function () {
                map.resize();
            });
        }

        if (stIsNeshanMap() && typeof stLoadNeshanSdk === 'function') {
            stLoadNeshanSdk().then(stInitAdminGmap);
        } else {
            stInitAdminGmap();
        }
    // });
});
