package com.google.maps.android.clustering.view;

import android.content.Context;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.clustering.Cluster;
import com.google.maps.android.clustering.ClusterItem;
import com.google.maps.android.ui.BubbleIconFactory;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class DefaultView<T extends ClusterItem> implements ClusterView<T> {
    private final GoogleMap mMap;
    private final BubbleIconFactory mBubbleIconFactory;
    private final Collection<Marker> mMarkers = new HashSet<Marker>();

    public DefaultView(Context context, GoogleMap map) {
        mMap = map;
        mBubbleIconFactory = new BubbleIconFactory(context);
        mBubbleIconFactory.setStyle(BubbleIconFactory.Style.BLUE);
    }

    private Marker onCreateCluster(Cluster<T> cluster) {
        if (cluster.getSize() == 1) {
            for (ClusterItem item : cluster.getItems()) {
                return mMap.addMarker(item.getMarkerOptions());
            }
        }

        BitmapDescriptor icon = getBitmapDescriptor(cluster);
        return mMap.addMarker(new MarkerOptions().
                icon(icon).
                title(getTitle(cluster)).
                position(cluster.getPosition()));
    }

    private String getTitle(Cluster cluster) {
        return "Items: " + cluster.getSize();
    }

    private BitmapDescriptor getBitmapDescriptor(Cluster cluster) {
        return BitmapDescriptorFactory.fromBitmap(mBubbleIconFactory.makeIcon(cluster.getSize() + ""));
    }

    @Override
    public void onClustersChanged(Set<? extends Cluster<T>> clusters) {
        for (Marker m : mMarkers) {
            m.remove();
        }
        mMarkers.clear();
        for (Cluster c : clusters) {
            mMarkers.add(onCreateCluster(c));
        }
    }
}
@@ -74,16 +74,74 @@
 Context;
+%0Aimport android.util.Log;%0Aimport android.util.SparseArray;
 %0A%0Aimport
@@ -884,16 +884,160 @@
 rker%3E();
+%0A    private SparseArray%3CBitmapDescriptor%3E mIcons = new SparseArray%3CBitmapDescriptor%3E();%0A    private Set%3C? extends Cluster%3CT%3E%3E mCurrentClusters;
 %0A%0A    pu
@@ -1254,22 +1254,20 @@
 private 
-Marker
+void
  onCreat
@@ -1331,12 +1331,11 @@
 e() 
-== 1
+%3C 4
 ) %7B%0A
@@ -1400,38 +1400,47 @@
                 
-return
+Marker marker =
  mMap.addMarker(
@@ -1469,33 +1469,91 @@
 ));%0A            
-%7D
+    mMarkers.add(marker);%0A            %7D%0A            return;
 %0A        %7D%0A%0A    
@@ -1618,22 +1618,31 @@
         
-return
+Marker marker =
  mMap.ad
@@ -1786,24 +1786,54 @@
 sition()));%0A
+        mMarkers.add(marker);%0A
     %7D%0A%0A    p
@@ -2014,189 +2014,1032 @@
 urn 
-BitmapDescriptorFactory.fromBitmap(mBubbleIconFactory.makeIcon(cluster.getSize() + %22%22));%0A    %7D%0A%0A    @Override%0A    public void onClustersChanged(Set%3C? extends Cluster%3CT%3E%3E clusters) %7B
+getIcon(getBucket(cluster.getSize()));%0A    %7D%0A%0A    private BitmapDescriptor getIcon(int bucket) %7B%0A        BitmapDescriptor descriptor = mIcons.get(bucket);%0A        if (descriptor == null) %7B%0A            descriptor = BitmapDescriptorFactory.fromBitmap(mBubbleIconFactory.makeIcon(bucket + (bucket %3E= 10 ? %22+%22 : %22%22)));%0A            mIcons.put(bucket, descriptor);%0A        %7D%0A        return descriptor;%0A    %7D%0A%0A    private int getBucket(int size) %7B%0A        if (size %3C 10) %7B%0A            return size;%0A        %7D%0A        if (size %3C 20) %7B%0A            return 10;%0A        %7D%0A        if (size %3C 40) %7B%0A            return 20;%0A        %7D%0A        if (size %3C 100) %7B%0A            return 40;%0A        %7D%0A        if (size %3C 200) %7B%0A            return 100;%0A        %7D%0A        if (size %3C 500) %7B%0A            return 200;%0A        %7D%0A        return 500;%0A    %7D%0A%0A    @Override%0A    public void onClustersChanged(Set%3C? extends Cluster%3CT%3E%3E clusters) %7B%0A        if (clusters.equals(mCurrentClusters)) %7B%0A            return;%0A        %7D%0A        mCurrentClusters = clusters;
 %0A   
@@ -3180,29 +3180,16 @@
         
-mMarkers.add(
 onCreate
@@ -3198,17 +3198,16 @@
 uster(c)
-)
 ;%0A      

use buckets for icon text. Don't show the cluster if there are fewer than 4 points. Only refresh the view if the clusters change.