package com.churches.by;

import android.content.Intent;
import android.location.Address;
import android.os.Handler;
import android.os.ResultReceiver;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;

import com.churches.by.data.DataProvider;
import com.churches.by.data.model.Church;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.vla3089.functional.Receiver;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;

import java.util.List;

@EActivity(R.layout.activity_churches_map)
@OptionsMenu(R.menu.menu_churches_map)
public class ChurchesMap extends ActionBarActivity {

    private static final int userIcon = R.drawable.yellow_point;
    private static final int foodIcon = R.drawable.red_point;
    private static final int drinkIcon = R.drawable.blue_point;
    private static final int shopIcon = R.drawable.green_point;
    private static final int otherIcon = R.drawable.purple_point;

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    public ChurchesMap() {
    }

    @AfterViews
    protected void setupMap() {
        if(mMap==null){
            FragmentManager fm = getSupportFragmentManager();
            SupportMapFragment mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);

            if (mapFragment != null) {
                mMap = mapFragment.getMap();
            }
        }

        if (mMap != null) {
            mMap.setMyLocationEnabled(true);
            displayChurches();
        }
    }

    @OptionsItem(R.id.action_settings)
    protected void startIntentService() {
        Intent intent = new Intent(this, FetchAddressIntentService.class);
        intent.putExtra(FetchAddressIntentService.REQUEST_ADDRESS, "г. Минск, пл. Свободы 9");
        startService(intent);
    }


    private void displayChurches() {
        DataProvider.instance().requestChurches(new Receiver<List<Church>>() {
            @Override
            public void receive(List<Church> value) {
                for (Church church : value) {
                    Address address = church.address();
                    LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());

                    mMap.addMarker(new MarkerOptions()
                    .position(latLng)
                    .icon(BitmapDescriptorFactory.fromResource(otherIcon))
                    .title(church.name()));
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

}

@@ -239,16 +239,41 @@
 ctivity;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -396,16 +396,71 @@
 gleMap;%0A
+import com.google.android.gms.maps.OnMapReadyCallback;%0A
 import c
@@ -1093,16 +1093,46 @@
 ctivity 
+implements OnMapReadyCallback 
 %7B%0A%0A    p
@@ -1582,88 +1582,35 @@
     
-@AfterViews%0A    protected void setupMap() %7B%0A        if(mMap==null)%7B%0A            
+    android.support.v4.app.
 Frag
@@ -1651,28 +1651,24 @@
 tManager();%0A
-    
         Supp
@@ -1753,28 +1753,24 @@
 );%0A%0A        
- 
-   
 if (mapFragm
@@ -1799,19 +1799,8 @@
     
-     mMap =
  map
@@ -1818,138 +1818,18 @@
 tMap
-();%0A            %7D%0A        %7D%0A%0A        if (mMap != null) %7B%0A            mMap.setMyLocationEnabled(true);%0A            displayChurches(
+Async(this
 );%0A 
@@ -2865,10 +2865,1328 @@
 %0A    %7D%0A%0A
+    private void updatePlaces()%7B%0A        Location lastLoc = CAppliation.locationManager().lastKnownLocation();%0A%0A        if (lastLoc == null) %7B%0A            return;%0A        %7D%0A%0A        double lat = lastLoc.getLatitude();%0A        double lng = lastLoc.getLongitude();%0A%0A        LatLng lastLatLng = new LatLng(lat, lng);%0A%0A        if(userMarker!=null) %7B%0A            userMarker.remove();%0A        %7D%0A%0A        Log.d(%22churches_map%22, %22setting user location:%22 + lastLatLng.toString());%0A%0A        userMarker = mMap.addMarker(new MarkerOptions()%0A                .position(lastLatLng)%0A                .title(%22You are here%22)%0A                .icon(BitmapDescriptorFactory.fromResource(userIcon))%0A                .snippet(%22Your last recorded location%22));%0A%0A        mMap.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 3000, null);%0A    %7D%0A%0A    @Override%0A    public void onMapReady(GoogleMap googleMap) %7B%0A        mMap = googleMap;%0A        updatePlaces();%0A        displayChurches();%0A%0A        LatLng sydney = new LatLng(-33.867, 151.206);%0A%0A        mMap.setMyLocationEnabled(true);%0A        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));%0A%0A        mMap.addMarker(new MarkerOptions()%0A                .title(%22Sydney%22)%0A                .snippet(%22The most populous city in Australia.%22)%0A                .position(sydney));%0A    %7D%0A
 %7D%0A

Remove annoyed sdk plugin and modify ChurchesMap a bit