/*******************************************************************************
 * Copyright 2014, 2017 gwt-ol3
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.github.tdesjardins.ol3.demo.client.example;

import java.util.ArrayList;
import java.util.List;

import com.github.tdesjardins.ol3.demo.client.utils.DemoUtils;

import ol.Collection;
import ol.Coordinate;
import ol.Feature;
import ol.FeatureOptions;
import ol.GenericFunction;
import ol.Map;
import ol.MapOptions;
import ol.OLFactory;
import ol.View;
import ol.control.Rotate;
import ol.control.ScaleLine;
import ol.geom.Point;
import ol.interaction.KeyboardPan;
import ol.interaction.KeyboardZoom;
import ol.layer.Base;
import ol.layer.LayerOptions;
import ol.layer.Tile;
import ol.layer.VectorLayerOptions;
import ol.source.Cluster;
import ol.source.ClusterOptions;
import ol.source.Osm;
import ol.source.Vector;
import ol.source.VectorOptions;
import ol.source.XyzOptions;
import ol.style.CircleOptions;
import ol.style.Style;
import ol.style.StyleOptions;
import ol.style.TextOptions;

/**
 * Example about using clusters.
 *
 * @author mazlixek
 */
public class ClusterExample implements Example {

    /* (non-Javadoc)
     * @see de.desjardins.ol3.demo.client.example.Example#show() */
    @Override
    public void show(String exampleId) {

        Collection<Feature> features = new Collection<Feature>();
        int e = 450000;
        
        for (int i = 0; i < 20000; ++i) {
            // create a point
            Coordinate coordinate = OLFactory.createCoordinate(2 * e * Math.random() - e, 2 * e * Math.random() - e);
            Point point = new Point(coordinate);

            // create feature
            FeatureOptions featureOptions = OLFactory.createOptions();
            featureOptions.setGeometry(point);
            Feature feature = new Feature(featureOptions);

            features.push(feature);
        }

        // create source
        VectorOptions vectorSourceOptions = OLFactory.createOptions();
        vectorSourceOptions.setFeatures(features);
        Vector vectorSource = new Vector(vectorSourceOptions);

        // create clustering
        ClusterOptions clusterOptions = new ClusterOptions();
        clusterOptions.setDistance(40);
        clusterOptions.setSource(vectorSource);
        Cluster clusterSource = new Cluster(clusterOptions);

        // create vector layer
        VectorLayerOptions vectorLayerOptions = OLFactory.createOptions();
        vectorLayerOptions.setSource(clusterSource);
        vectorLayerOptions.setStyle(new GenericFunction<Feature, Style[]>() {

            @Override
            public Style[] call(Feature cluster) {
                // create style
                List<Style> style = new ArrayList<>();

                Feature[] features = (Feature[])cluster.get("features");
                int clusterSize = features.length;

                StyleOptions styleOptions = new StyleOptions();

                TextOptions textOptions = OLFactory.createTextOptions();
                textOptions.setText(String.valueOf(clusterSize));
                styleOptions.setText(OLFactory.createText(textOptions));

                CircleOptions circleOptions = new CircleOptions();
                circleOptions.setRadius(10);
                circleOptions.setFill(OLFactory.createFill(OLFactory.createColor("#3399CC")));
                styleOptions.setImage(OLFactory.createCircleStyle(circleOptions));
                style.add(new Style(styleOptions));

                return style.toArray(new Style[0]);
            }
        });

        ol.layer.Vector vectorLayer = new ol.layer.Vector(vectorLayerOptions);

        // create a OSM-layer
        XyzOptions osmSourceOptions = OLFactory.createOptions();
        Osm osmSource = new Osm(osmSourceOptions);

        LayerOptions osmLayerOptions = OLFactory.createOptions();
        osmLayerOptions.setSource(osmSource);

        Tile osmLayer = new Tile(osmLayerOptions);

        // create a view
        View view = new View();
        Coordinate centerCoordinate = OLFactory.createCoordinate(0, 0);
        view.setCenter(centerCoordinate);
        view.setZoom(2);

        // create the map
        MapOptions mapOptions = new MapOptions();
        mapOptions.setTarget(exampleId);
        mapOptions.setView(view);
        Collection<Base> lstLayer = new Collection<Base>();
        lstLayer.push(osmLayer);
        lstLayer.push(vectorLayer);
        mapOptions.setLayers(lstLayer);
        Map map = new Map(mapOptions);

        // add some controls
        map.addControl(new ScaleLine());
        DemoUtils.addDefaultControls(map.getControls());

        // add some interactions
        map.addInteraction(new KeyboardPan());
        map.addInteraction(new KeyboardZoom());
        map.addControl(new Rotate());
    }
    
}

@@ -1139,16 +1139,40 @@
 .View;%0D%0A
+import ol.color.Color;%0D%0A
 import o
@@ -1595,32 +1595,57 @@
 ce.XyzOptions;%0D%0A
+import ol.style.Circle;%0D%0A
 import ol.style.
@@ -1707,32 +1707,55 @@
 .StyleOptions;%0D%0A
+import ol.style.Text;%0D%0A
 import ol.style.
@@ -2133,24 +2133,16 @@
 50000;%0D%0A
-        
 %0D%0A      
@@ -2245,32 +2245,20 @@
 inate = 
-OLFactory.create
+new 
 Coordina
@@ -2439,39 +2439,34 @@
 reOptions = 
-OLFactory.creat
+new Featur
 eOptions();%0D
@@ -2696,32 +2696,26 @@
 tions = 
-OLFactory.create
+new Vector
 Options(
@@ -3163,32 +3163,31 @@
 tions = 
-OLFactory.create
+new VectorLayer
 Options(
@@ -3728,32 +3728,20 @@
 tions = 
-OLFactory.create
+new 
 TextOpti
@@ -3852,32 +3852,20 @@
 setText(
-OLFactory.create
+new 
 Text(tex
@@ -3989,24 +3989,92 @@
 adius(10);%0D%0A
+                Color color = Color.getColorFromString(%22#3399CC%22);%0D%0A
             
@@ -4124,40 +4124,13 @@
 ill(
-OLFactory.createColor(%22#3399CC%22)
+color
 ));%0D
@@ -4172,33 +4172,16 @@
 age(
-OLFactory.createCircleSty
+new Circ
 le(c
@@ -4486,32 +4486,23 @@
 tions = 
-OLFactory.create
+new Xyz
 Options(
@@ -4598,32 +4598,25 @@
 tions = 
-OLFactory.create
+new Layer
 Options(
@@ -4819,32 +4819,20 @@
 inate = 
-OLFactory.create
+new 
 Coordina
@@ -5592,17 +5592,13 @@
 %0A    %7D%0D%0A
-    
 %0D%0A%7D%0D%0A

Refactor cluster example