package com.churches.by;

import android.location.Address;
import android.location.Location;
import android.os.Bundle;
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.CameraUpdateFactory;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.vla3089.functional.Receiver;

import org.androidannotations.annotations.EActivity;
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 int userIcon, foodIcon, drinkIcon, shopIcon, otherIcon;

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userIcon = R.drawable.yellow_point;
        foodIcon = R.drawable.red_point;
        drinkIcon = R.drawable.blue_point;
        shopIcon = R.drawable.green_point;
        otherIcon = R.drawable.purple_point;

        if(mMap==null){
            FragmentManager fm = getSupportFragmentManager();
            SupportMapFragment mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);

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

        if (mMap != null) {
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            updatePlaces();
            displayChurches();
        }
    }

    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();
    }

    private void updatePlaces(){
        Location lastLoc = CAppliation.locationManager().lastKnownLocation();

        double lat = lastLoc.getLatitude();
        double lng = lastLoc.getLongitude();

        LatLng lastLatLng = new LatLng(lat, lng);

        if(userMarker!=null) {
            userMarker.remove();
        }

        userMarker = mMap.addMarker(new MarkerOptions()
                .position(lastLatLng)
                .title("You are here")
                .icon(BitmapDescriptorFactory.fromResource(userIcon))
                .snippet("Your last recorded location"));

        mMap.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 3000, null);
    }
}

@@ -105,17 +105,39 @@
 oid.
-os.Bundle
+support.v4.app.FragmentActivity
 ;%0Aim
@@ -733,16 +733,70 @@
 eiver;%0A%0A
+import org.androidannotations.annotations.AfterViews;%0A
 import o
@@ -1072,315 +1072,157 @@
 ate 
-int userIcon, foodIcon, drinkIcon, shopIcon, otherIcon;%0A%0A    private GoogleMap mMap; // Might be null if Google Play services APK is not available.%0A    private Marker userMarker;%0A%0A    @Override%0A    protected void onCreate(Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A        user
+static final int userIcon = R.drawable.yellow_point;%0A    private static final int foodIcon = R.drawable.red_point;%0A    private static final int drink
 Icon
@@ -1227,38 +1227,36 @@
 on = R.drawable.
-yellow
+blue
 _point;%0A        
@@ -1255,16 +1255,37 @@
     
-    food
+private static final int shop
 Icon
@@ -1290,35 +1290,37 @@
 on = R.drawable.
+g
 re
-d
+en
 _point;%0A        
@@ -1319,17 +1319,38 @@
     
-    drink
+private static final int other
 Icon
@@ -1355,35 +1355,37 @@
 on = R.drawable.
-blu
+purpl
 e_point;%0A       
@@ -1381,96 +1381,182 @@
 nt;%0A
+%0A
     
-    shopIcon = R.drawable.green_point;
+private GoogleMap mMap; // Might be null if Google Play services APK is not available.%0A%0A    public ChurchesMap() %7B
 %0A    
+%7D%0A%0A
     
-otherIcon = R.drawable.purple_point;%0A
+@AfterViews%0A    protected void setupMap() %7B
 %0A   
@@ -1941,29 +1941,46 @@
         
-updatePlaces(
+mMap.setMyLocationEnabled(true
 );%0A     
@@ -2758,685 +2758,6 @@
  %7D%0A%0A
-    private void updatePlaces()%7B%0A        Location lastLoc = CAppliation.locationManager().lastKnownLocation();%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        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
 %7D%0A

fix obtaining map instance