package tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.ViewGroup;
import android.widget.ImageView;

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;
import com.google.maps.android.ui.IconGenerator;

public class MyClusterManagerRenderer extends DefaultClusterRenderer<ClusterMarker> {

    private final IconGenerator iconGenerator;
    private final ImageView imageView;
    private final int markerWith;
    private final int markerHeight;

    public MyClusterManagerRenderer(Context context, GoogleMap map, ClusterManager<ClusterMarker> clusterManager) {
        super(context, map, clusterManager);
        //this.iconGenerator = iconGenerator;
        //this.imageView = imageView;
        //this.markerWith = markerWith;
        //this.markerHeight = markerHeight;

        iconGenerator =  new IconGenerator(context.getApplicationContext());
        imageView = new ImageView(context.getApplicationContext());
        markerWith = (int) 40; //TODO брать из ресурсов
        markerHeight = (int) 40; //TODO брать из ресурсов
        imageView.setLayoutParams(new ViewGroup.LayoutParams(markerWith, markerHeight));
        int pagging = (int) 5;
        imageView.setPadding(pagging,pagging,pagging,pagging);
        iconGenerator.setContentView(imageView);
    }

    @Override
    protected void onBeforeClusterItemRendered(ClusterMarker item, MarkerOptions markerOptions) {
        //super.onBeforeClusterItemRendered(item, markerOptions);
        imageView.setImageResource(item.getIconPicture());
        Bitmap icon = iconGenerator.makeIcon();
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon))
            .title(item.getTitle());
    }

    @Override
    protected boolean shouldRenderAsCluster(Cluster<ClusterMarker> cluster) {
        //return super.shouldRenderAsCluster(cluster);
        return false; //Не объединаять точки в одну точку если они наплывают друг на друга.
    }
}
