package hu.supercluster.overpasser.app.activity.container;

import android.content.Context;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.annotations.UiThread;

import java.util.HashMap;
import java.util.Map;

import hu.supercluster.overpasser.R;
import hu.supercluster.overpasser.adapter.OverpassQueryResult;
import hu.supercluster.overpasser.adapter.OverpassQueryResult.Element;
import hu.supercluster.overpasser.app.view.PoiInfoWindowAdapter;

@EBean
public class MapPoiHandler {
    private MapFragment fragment;
    private Map<Long, Element> poiMap;

    @RootContext
    Context context;

    @Bean
    MapOverpassAdapter overApiAdapter;

    @Bean
    PoiInfoWindowAdapter poiInfoWindowAdapter;

    public MapPoiHandler() {
        poiMap = new HashMap<>();
    }

    public void setFragment(MapFragment fragment) {
        this.fragment = fragment;
    }

    public void reset() {
        poiMap.clear();
    }

    @Background
    void fetchPois(LatLngBounds bounds) {
        OverpassQueryResult result = overApiAdapter.search(bounds);

        for (Element poi : result.elements) {
            if (!alreadyStored(poi)) {
                fixTitle(poi);
                storePoi(poi);
                showPoi(poi);
            }
        }
    }

    private boolean alreadyStored(Element poi) {
        return poiMap.containsKey(poi.id);
    }

    private void fixTitle(Element poi) {
        Element.Tags info = poi.tags;

        if (info.name == null) {
            info.name = context.getResources().getString(R.string.poi_category_public_toilet);
        }
    }

    private void storePoi(Element poi) {
        poiMap.put(poi.id, poi);
    }

    @UiThread
    void showPoi(Element poi) {
        MarkerOptions options = new MarkerOptions()
            .position(new LatLng(poi.lat, poi.lon))
        ;

        Marker marker = fragment.getGoogleMap().addMarker(options);
        poiInfoWindowAdapter.addMarkerInfo(marker, poi);
    }
}

@@ -1445,16 +1445,50 @@
 unds);%0A%0A
+        if (result != null) %7B%0A    
         
@@ -1533,24 +1533,28 @@
             
+    
 if (!already
@@ -1584,16 +1584,20 @@
         
+    
 fixTitle
@@ -1619,16 +1619,20 @@
         
+ 
+   
 storePoi
@@ -1654,16 +1654,20 @@
         
+    
 showPoi(
@@ -1664,32 +1664,50 @@
   showPoi(poi);%0A
+                %7D%0A
             %7D%0A  

Add null check