package org.oskari.print.loader;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import org.oskari.print.request.PrintLayer;
import org.oskari.print.request.PrintRequest;
import org.oskari.print.wmts.WMTSCapabilitiesCache;

import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.service.ServiceException;

public class AsyncImageLoader {

    public static final String GROUP_KEY = "LoadImageFromURL";

    public static Map<Integer, Future<BufferedImage>> initLayers(PrintRequest request, WMTSCapabilitiesCache wmtsCapsCache)
            throws ServiceException {
        final Map<Integer, Future<BufferedImage>> images = new HashMap<>();

        final List<PrintLayer> requestedLayers = request.getLayers();
        if (requestedLayers == null) {
            return images;
        }

        final int width = request.getWidth();
        final int height = request.getHeight();
        final double[] bbox = request.getBoundingBox();
        final String srsName = request.getSrsName();

        for (PrintLayer layer : requestedLayers) {
            switch (layer.getType()) { 
            case OskariLayer.TYPE_WMS:
                images.put(layer.getZIndex(), new CommandLoadImageWMS(layer, 
                        width, height, bbox, srsName).queue());
                break;
            case OskariLayer.TYPE_WMTS:
                images.put(layer.getZIndex(), new CommandLoadImageWMTS(layer, width, height, bbox, srsName,
                        wmtsCapsCache.get(layer), request.getResolution()).queue());
                break;
            case "myplaces":
                images.put(layer.getZIndex(), new CommandLoadImageMyPlaces(request.getUser(),
                        layer, width, height, bbox, srsName).queue());
                break;
            case OskariLayer.TYPE_USERLAYER:
                images.put(layer.getZIndex(), new CommandLoadImageUserLayer(request.getUser(),
                        layer, width, height, bbox, srsName).queue());
                break;
            case OskariLayer.TYPE_ANALYSIS:
                images.put(layer.getZIndex(), new CommandLoadImageAnalysis(request.getUser(),
                        layer, width, height, bbox, srsName).queue());
                break;
            case OskariLayer.TYPE_ARCGIS93:
                images.put(layer.getZIndex(), new CommandLoadImageArcGISREST(layer,
                        width, height, bbox, srsName).queue());
                break;
            }
        }

        return images;
    }

}

@@ -1650,691 +1650,8 @@
 ak;%0A
-            case %22myplaces%22:%0A                images.put(layer.getZIndex(), new CommandLoadImageMyPlaces(request.getUser(),%0A                        layer, width, height, bbox, srsName).queue());%0A                break;%0A            case OskariLayer.TYPE_USERLAYER:%0A                images.put(layer.getZIndex(), new CommandLoadImageUserLayer(request.getUser(),%0A                        layer, width, height, bbox, srsName).queue());%0A                break;%0A            case OskariLayer.TYPE_ANALYSIS:%0A                images.put(layer.getZIndex(), new CommandLoadImageAnalysis(request.getUser(),%0A                        layer, width, height, bbox, srsName).queue());%0A                break;%0A
     
@@ -1690,16 +1690,16 @@
 CGIS93:%0A
+
         
@@ -1875,16 +1875,17 @@
       %7D%0A
+%0A
         
@@ -1910,17 +1910,16 @@
 images;%0A
+
     %7D%0A
-%0A
 %7D%0A

remove userdata layers from AsyncImageLoader