package fr.free.nrw.commons.nearby;

import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.services.android.telemetry.MapboxTelemetry;

import java.lang.reflect.Type;
import java.util.List;

import fr.free.nrw.commons.R;
import fr.free.nrw.commons.utils.UriDeserializer;

public class NearbyMapFragment extends android.support.v4.app.Fragment {
    private MapView mapView;
    private Gson gson;
    private List<Place> placeList;
    private List<NearbyBaseMarker> baseMarkerOptionses;
    private fr.free.nrw.commons.location.LatLng curLatLng;

    public NearbyMapFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = this.getArguments();
        gson = new GsonBuilder()
                .registerTypeAdapter(Uri.class, new UriDeserializer())
                .create();
        if (bundle != null) {
            String gsonPlaceList = bundle.getString("PlaceList");
            String gsonLatLng = bundle.getString("CurLatLng");
            Type listType = new TypeToken<List<Place>>() {}.getType();
            placeList = gson.fromJson(gsonPlaceList, listType);
            Type curLatLngType = new TypeToken<fr.free.nrw.commons.location.LatLng>() {}.getType();
            curLatLng = gson.fromJson(gsonLatLng, curLatLngType);
            baseMarkerOptionses = NearbyController
                    .loadAttractionsFromLocationToBaseMarkerOptions(curLatLng, placeList);

        }
        Mapbox.getInstance(getActivity(),
                getString(R.string.mapbox_commons_app_token));
        MapboxTelemetry.getInstance().setTelemetryEnabled(false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        MapboxMapOptions options = new MapboxMapOptions()
                .styleUrl(Style.OUTDOORS)
                .camera(new CameraPosition.Builder()
                        .target(new LatLng(curLatLng.latitude, curLatLng.longitude))
                        .zoom(11)
                        .build());

        // create map
        mapView = new MapView(getActivity(), options);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(MapboxMap mapboxMap) {
                mapboxMap.addMarkers(baseMarkerOptionses);

                mapboxMap.setOnMarkerClickListener(new MapboxMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(@NonNull Marker marker) {
                        if (marker instanceof NearbyMarker) {
                            NearbyMarker nearbyMarker = (NearbyMarker) marker;
                            Place place = nearbyMarker.getNearbyBaseMarker().getPlace();
                            NearbyInfoDialog.showYourself(getActivity(), place);
                        }
                        return false;
                    }
                });
            }
        });
        if (PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("theme",true)) {
            mapView.setStyleUrl(getResources().getString(R.string.map_theme_dark));
        } else {
            mapView.setStyleUrl(getResources().getString(R.string.map_theme_light));
        }

        setHasOptionsMenu(false);

        return mapView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onStart() {
        mapView.onStart();
        super.onStart();
    }

    @Override
    public void onPause() {
        mapView.onPause();
        super.onPause();
    }

    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    public void onStop() {
        mapView.onStop();
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        mapView.onDestroy();
        super.onDestroyView();
    }
}

@@ -2561,32 +2561,248 @@
 InstanceState) %7B
+%0A%0A        if (curLatLng != null) %7B%0A            setupMapView(savedInstanceState);%0A        %7D%0A%0A        setHasOptionsMenu(false);%0A%0A        return mapView;%0A    %7D%0A%0A    private void setupMapView(Bundle savedInstanceState) %7B
 %0A        MapboxM
@@ -4351,68 +4351,8 @@
    %7D
-%0A%0A        setHasOptionsMenu(false);%0A%0A        return mapView;
 %0A   
@@ -4549,24 +4549,59 @@
 onStart() %7B%0A
+        if (mapView != null) %7B%0A    
         mapV
@@ -4611,24 +4611,34 @@
 .onStart();%0A
+        %7D%0A
         supe
@@ -4695,24 +4695,59 @@
 onPause() %7B%0A
+        if (mapView != null) %7B%0A    
         mapV
@@ -4757,24 +4757,34 @@
 .onPause();%0A
+        %7D%0A
         supe
@@ -4842,24 +4842,59 @@
 nResume() %7B%0A
+        if (mapView != null) %7B%0A    
         mapV
@@ -4905,24 +4905,34 @@
 onResume();%0A
+        %7D%0A
         supe
@@ -4989,24 +4989,59 @@
  onStop() %7B%0A
+        if (mapView != null) %7B%0A    
         mapV
@@ -5050,24 +5050,34 @@
 w.onStop();%0A
+        %7D%0A
         supe
@@ -5139,24 +5139,59 @@
 royView() %7B%0A
+        if (mapView != null) %7B%0A    
         mapV
@@ -5203,24 +5203,34 @@
 nDestroy();%0A
+        %7D%0A
         supe

Fix Crash issue when GPS location is not available