package com.github.davidmoten.grumpy.wms.demo;

import static com.github.davidmoten.grumpy.core.Position.position;
import static com.github.davidmoten.grumpy.wms.RendererUtil.draw;
import static com.github.davidmoten.grumpy.wms.RendererUtil.getCircle;
import static com.github.davidmoten.grumpy.wms.RendererUtil.toPath;
import static com.github.davidmoten.grumpy.wms.WmsUtil.getProjector;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.GeneralPath;
import java.util.Date;
import java.util.List;

import com.github.davidmoten.grumpy.core.Position;
import com.github.davidmoten.grumpy.projection.Projector;
import com.github.davidmoten.grumpy.util.Bounds;
import com.github.davidmoten.grumpy.wms.Layer;
import com.github.davidmoten.grumpy.wms.LayerFeatures;
import com.github.davidmoten.grumpy.wms.RendererUtil;
import com.github.davidmoten.grumpy.wms.WmsRequest;
import com.github.davidmoten.grumpy.wms.WmsUtil;
import com.github.davidmoten.grumpy.wms.reduction.BoundsSampler;
import com.github.davidmoten.grumpy.wms.reduction.BoundsSamplerMaxSize;
import com.github.davidmoten.grumpy.wms.reduction.ReducingValueRenderer;
import com.github.davidmoten.grumpy.wms.reduction.ValueRenderer;
import com.google.common.base.Function;

public class FiddleLayer implements Layer {

    private final LayerFeatures features;

    public FiddleLayer() {
        features = LayerFeatures.builder().name("Fiddle").crs("EPSG:4326").crs("EPSG:3857").build();
    }

    @Override
    public void render(Graphics2D g, WmsRequest request) {
        Position centre = position(-35, 149);
        Projector projector = getProjector(request);

        int radiusKm = 500;
        List<Position> positions = getCircle(centre, radiusKm, 360);
        List<GeneralPath> paths = toPath(projector, positions);
        g.setColor(Color.BLUE);
        draw(g, paths);

        Bounds geoBounds = WmsUtil.toBounds(request);
        Function<Position, Boolean> function = createValueFunction(centre, radiusKm);
        ValueRenderer<Boolean> regionRenderer = createValueRenderer();
        BoundsSampler sampler = new BoundsSamplerMaxSize(radiusKm / 4);
        ReducingValueRenderer.renderRegion(g, function, projector, geoBounds, sampler,
                regionRenderer);
    }

    private ValueRenderer<Boolean> createValueRenderer() {
        return new ValueRenderer<Boolean>() {
            @Override
            public void render(Graphics2D g, Projector projector, Bounds geoBounds, Boolean t) {
                if (t) {
                    List<Position> positions = WmsUtil.getBorder(geoBounds);
                    List<GeneralPath> shapes = RendererUtil.toPath(projector, positions);
                    g.setColor(Color.GRAY);
                    RendererUtil.fill(g, shapes);
                }
            }
        };
    }

    private Function<Position, Boolean> createValueFunction(final Position centre,
            final double radiusKm) {
        return new Function<Position, Boolean>() {
            @Override
            public Boolean apply(Position p) {
                return centre.getDistanceToKm(p) <= radiusKm;
            }
        };
    }

    @Override
    public String getInfo(Date time, WmsRequest request, Point point, String mimeType) {
        return null;
    }

    @Override
    public LayerFeatures getFeatures() {
        return features;
    }

}

@@ -1692,200 +1692,12 @@
 m = 
-500;%0A        List%3CPosition%3E positions = getCircle(centre, radiusKm, 360);%0A        List%3CGeneralPath%3E paths = toPath(projector, positions);%0A        g.setColor(Color.BLUE);%0A        draw(g, paths)
+8000
 ;%0A%0A 
@@ -1974,17 +1974,17 @@
 iusKm / 
-4
+2
 );%0A     
@@ -2097,16 +2097,206 @@
 nderer);
+%0A%0A        List%3CPosition%3E positions = getCircle(centre, radiusKm, 360);%0A        List%3CGeneralPath%3E paths = toPath(projector, positions);%0A        g.setColor(Color.BLUE);%0A        draw(g, paths);
 %0A    %7D%0A%0A
@@ -2703,32 +2703,32 @@
 or, positions);%0A
-
                 
@@ -2748,16 +2748,22 @@
 r(Color.
+LIGHT_
 GRAY);%0A 

add sampler