
package com.uwetrottmann.shopr.ui;

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.util.Log;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CircleOptions;
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.uwetrottmann.androidutils.Lists;
import com.uwetrottmann.shopr.R;
import com.uwetrottmann.shopr.loaders.ShopLoader;
import com.uwetrottmann.shopr.model.Shop;
import com.uwetrottmann.shopr.ui.ItemListFragment.ShopUpdateEvent;
import com.uwetrottmann.shopr.ui.MainActivity.LocationUpdateEvent;

import de.greenrobot.event.EventBus;

import java.util.List;
import java.util.Map;

public class ShopMapFragment extends SupportMapFragment implements LoaderCallbacks<List<Shop>> {

    private static final int RADIUS_METERS = 2000;
    private static final int ZOOM_LEVEL_INITIAL = 14;
    public static final String TAG = "Shops Map";
    private static final int LAODER_ID = 22;

    public static ShopMapFragment newInstance() {
        return new ShopMapFragment();
    }

    private List<Marker> mShopMarkers;
    private Location mLocation;
    private boolean mIsInitialized;
    private List<Shop> mShops;
    private Map<Integer, Integer> mShopsWithItems;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mIsInitialized = false;

        // enable my location feature
        getMap().setMyLocationEnabled(true);

        getLoaderManager().initLoader(LAODER_ID, null, this);
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault()
                .registerSticky(this, LocationUpdateEvent.class, ShopUpdateEvent.class);
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    private void onInitializeMap() {
        if (!mIsInitialized) {
            Log.d(TAG, "Initializing map.");

            mLocation = ((MainActivity) getActivity()).getLastLocation();
            if (mLocation == null) {
                return;
            }

            LatLng userPosition = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());
            // move camera to current position
            getMap().moveCamera(
                    CameraUpdateFactory.newLatLngZoom(userPosition, ZOOM_LEVEL_INITIAL));
            // draw a circle around it
            getMap().addCircle(new CircleOptions()
                    .center(userPosition)
                    .radius(RADIUS_METERS)
                    .strokeColor(getResources().getColor(R.color.lilac))
                    .strokeWidth(4)
                    .fillColor(getResources().getColor(R.color.lilac_transparent)));

            mIsInitialized = true;
        }
    }

    private void onUpdateShops(List<Shop> shops) {
        // remove existing markers
        if (mShopMarkers != null) {
            for (Marker marker : mShopMarkers) {
                marker.remove();
            }
        }

        List<Marker> shopMarkersNew = Lists.newArrayList();

        for (Shop shop : shops) {
            // determine color and recom. items in this shop
            float color;
            int itemCount;
            if (mShopsWithItems != null && mShopsWithItems.containsKey(shop.id())) {
                itemCount = mShopsWithItems.get(shop.id());
                color = BitmapDescriptorFactory.HUE_VIOLET;
            } else {
                itemCount = 0;
                color = BitmapDescriptorFactory.HUE_AZURE;
            }

            // place marker
            Marker marker = getMap().addMarker(
                    new MarkerOptions()
                            .position(shop.location())
                            .title(shop.name())
                            .snippet(getString(R.string.has_x_recommendations, itemCount))
                            .icon(BitmapDescriptorFactory.defaultMarker(color)));
            shopMarkersNew.add(marker);
        }

        mShopMarkers = shopMarkersNew;
    }

    public void onEvent(LocationUpdateEvent event) {
        onInitializeMap();
    }

    public void onEvent(ShopUpdateEvent event) {
        mShopsWithItems = event.shopMap;
        getLoaderManager().restartLoader(LAODER_ID, null, this);
    }

    @Override
    public Loader<List<Shop>> onCreateLoader(int loaderId, Bundle args) {
        return new ShopLoader(getActivity());
    }

    @Override
    public void onLoadFinished(Loader<List<Shop>> loader, List<Shop> data) {
        onUpdateShops(data);
    }

    @Override
    public void onLoaderReset(Loader<List<Shop>> laoder) {
    }

}

@@ -775,16 +775,68 @@
 l.Shop;%0A
+import com.uwetrottmann.shopr.settings.AppSettings;%0A
 import c
@@ -1483,40 +1483,8 @@
 rs;%0A
-    private Location mLocation;%0A
     
@@ -1519,39 +1519,8 @@
 ed;%0A
-    private List%3CShop%3E mShops;%0A
     
@@ -2323,17 +2323,317 @@
         
-m
+LatLng userPosition;%0A            if (AppSettings.isUsingFakeLocation(getActivity())) %7B%0A                // use fake location (Marienplatz, Munich)%0A                userPosition = new LatLng(48.137314, 11.575253);%0A            %7D else %7B%0A                // actual user position%0A                Location true
 Location
@@ -2697,21 +2697,28 @@
         
-if (m
+    if (true
 Location
@@ -2741,24 +2741,28 @@
             
+    
 return;%0A    
@@ -2761,34 +2761,37 @@
 rn;%0A            
+ 
+   
 %7D
-%0A
 %0A            Lat
@@ -2779,38 +2779,35 @@
   %7D%0A            
-LatLng
+   
  userPosition = 
@@ -2817,17 +2817,20 @@
  LatLng(
-m
+true
 Location
@@ -2845,17 +2845,20 @@
 tude(), 
-m
+true
 Location
@@ -2871,24 +2871,39 @@
 ngitude());%0A
+            %7D%0A%0A
             

Support fake location in map.