package io.github.plastix.forage.ui.map;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.List;

import butterknife.BindView;
import io.github.plastix.forage.ForageApplication;
import io.github.plastix.forage.R;
import io.github.plastix.forage.data.local.model.Cache;
import io.github.plastix.forage.ui.base.PresenterFragment;

/**
 * Fragment that is responsible for the geocache map.
 */
public class MapFragment extends PresenterFragment<MapPresenter, MapFragView> implements MapFragView, OnMapReadyCallback {

    @BindView(R.id.map_mapview)
    MapView map;

    private GoogleMap googleMap;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        injectDependencies();
        super.onCreate(savedInstanceState);
    }

    private void injectDependencies() {
        ForageApplication.getComponent(getContext())
                .plus(new MapModule(this))
                .injectTo(this);
    }

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

    @Override
    public void populateMap(List<Cache> caches) {
        if (googleMap != null) {
            for (Cache cache : caches) {
                MarkerOptions markerOptions = new MarkerOptions()
                        .position(new LatLng(cache.location.latitude, cache.location.longitude))
                        .title(cache.name);
                googleMap.addMarker(markerOptions);
            }
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);

        this.googleMap = googleMap;
        presenter.getGeocaches();
    }

    @Override
    public void onDestroyView() {
        map.onDestroy();
        this.googleMap = null;
        super.onDestroyView();
    }

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

    @Override
    public void onResume() {
        super.onResume();
        map.onResume();
        map.getMapAsync(this);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        map.onLowMemory();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        map.onSaveInstanceState(outState);
    }

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_map;
    }

}

@@ -378,17 +378,92 @@
 rker
-Options;%0A
+;%0Aimport com.google.android.gms.maps.model.MarkerOptions;%0A%0Aimport java.util.HashMap;
 %0Aimp
@@ -481,16 +481,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -734,16 +734,84 @@
 ragment;
+%0Aimport io.github.plastix.forage.ui.cachedetail.CacheDetailActivity;
 %0A%0A/**%0A *
@@ -862,16 +862,59 @@
 he map.%0A
+ * TODO Switch from MapView to MapFragment%0A
  */%0Apubl
@@ -1029,16 +1029,53 @@
 Callback
+, GoogleMap.OnInfoWindowClickListener
  %7B%0A%0A    
@@ -1152,16 +1152,57 @@
 ogleMap;
+%0A    private Map%3CMarker, String%3E markers;
 %0A%0A    @O
@@ -1339,32 +1339,72 @@
 InstanceState);%0A
+        this.markers = new HashMap%3C%3E();%0A
     %7D%0A%0A    priva
@@ -2124,24 +2124,40 @@
             
+ Marker marker =
  googleMap.a
@@ -2197,19 +2197,273 @@
     
-%7D%0A        %7D
+    markers.put(marker, cache.cacheCode);%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onInfoWindowClick(Marker marker) %7B%0A        String cacheCode = markers.get(marker);%0A%0A        startActivity(CacheDetailActivity.newIntent(getContext(), cacheCode));
 %0A   
@@ -2707,16 +2707,70 @@
 d(true);
+%0A        googleMap.setOnInfoWindowClickListener(this);
 %0A%0A      
@@ -2935,16 +2935,134 @@
 oogleMap
+.setOnInfoWindowClickListener(null);%0A        this.googleMap = null;%0A        this.markers.clear();%0A        this.markers
  = null;

Add click support to Map screen