package com.expidev.gcmapp.map;

import android.content.Context;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.clustering.Cluster;
import com.google.maps.android.clustering.ClusterManager;
import com.google.maps.android.clustering.view.DefaultClusterRenderer;

/**
 * Created by matthewfrederick on 2/3/15.
 */
public class MarkerRender extends DefaultClusterRenderer<Marker> {
    private final Context context;

    public MarkerRender(Context context, GoogleMap map, ClusterManager<Marker> clusterManager) {
        super(context.getApplicationContext(), map, clusterManager);

        this.context = context;
    }

    @Override
    protected void onBeforeClusterItemRendered(final Marker item, MarkerOptions markerOptions) {
        super.onBeforeClusterItemRendered(item, markerOptions);
        markerOptions.icon(BitmapDescriptorFactory.fromResource(item.getItemImage()))
                .title(item.getName());
    }

    // TODO: for now disable custom icons for clusters until we figured out nuances of clustering
//    @Override
//    protected void onBeforeClusterRendered(Cluster<Marker> cluster, MarkerOptions markerOptions) {
//        super.onBeforeClusterRendered(cluster, markerOptions);
//        markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.training))
//            .title("" + cluster.getSize() + " training activities");
//    }

    @Override
    protected boolean shouldRenderAsCluster(Cluster<Marker> cluster) {
        return cluster.getSize() > 3;
    }
    
    
}

@@ -529,44 +529,8 @@
 %3E %7B%0A
-    private final Context context;%0A%0A
     
@@ -647,87 +647,30 @@
 text
-.getApplicationContext(), map, clusterManager);%0A%0A        this.context = context
+, map, clusterManager)
 ;%0A  
@@ -986,108 +986,8 @@
  %7D%0A%0A
-    // TODO: for now disable custom icons for clusters until we figured out nuances of clustering%0A//
     
@@ -996,18 +996,16 @@
 verride%0A
-//
     prot
@@ -1095,18 +1095,16 @@
 ions) %7B%0A
-//
         
@@ -1162,169 +1162,939 @@
 s);%0A
-//
+ 
        
- markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.training))%0A//            .title(%22%22 + cluster.getSize() + %22 training activities%22);%0A//
+int churches = 0;%0A        int trainings = 0;%0A        for (final Marker marker : cluster.getItems()) %7B%0A            if(marker instanceof ChurchMarker) %7B%0A                churches++;%0A            %7D else if(marker instanceof TrainingMarker) %7B%0A                trainings++;%0A            %7D%0A        %7D%0A%0A        // generate the title for this cluster%0A        final StringBuilder title = new StringBuilder();%0A        if(churches %3E 0) %7B%0A            title.append(churches).append(%22 Churches%22);%0A        %7D%0A        if(trainings %3E 0) %7B%0A            if(title.length() %3E 0) %7B%0A                title.append(%22 and %22);%0A            %7D%0A            title.append(trainings).append(%22 training activities%22);%0A        %7D%0A        markerOptions.title(title.toString());%0A%0A        // TODO: for now disable custom icons for clusters until we figured out nuances of clustering%0A//        markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.training));%0A
     

update title for clusters