/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2013 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */
package org.geomajas.widget.layer.client.util;

import org.geomajas.gwt.client.Geomajas;
import org.geomajas.gwt.client.map.layer.Layer;
import org.geomajas.gwt.client.map.layer.RasterLayer;
import org.geomajas.gwt.client.map.layer.VectorLayer;
import org.geomajas.gwt.client.util.UrlBuilder;
import org.geomajas.layer.LayerType;
import org.geomajas.widget.layer.configuration.client.ClientExtraLayerInfo;

import com.smartgwt.client.widgets.Img;

/**
 * Helper class for working with layerIcons.
 * 
 * @author Kristof Heirwegh
 */
public final class LayerIconUtil {


	private static final String LEGEND_ICONS_PATH = "legendgraphic";

	private static final String LEGEND_ICONS_TYPE = ".png";
	
	private LayerIconUtil() {
		// utility class, hide constructor
	}

	/**
	 * Get small layer icon as image.
	 * 
	 * @param layer
	 *            layer
	 * @return image
	 */
	public static Img getSmallLayerIcon(Layer<?> layer) {
		String url = getSmallLayerIconUrl(layer);
		if (url != null) {
			return new Img(url);
		} else {
			return null;
		}
	}

	/**
	 * Get small layer image URL.
	 * 
	 * @param layer
	 *            layer
	 * @return image URL
	 */
	public static String getSmallLayerIconUrl(Layer<?> layer) {
		ClientExtraLayerInfo eli = (ClientExtraLayerInfo) layer.getLayerInfo().getWidgetInfo(
				ClientExtraLayerInfo.IDENTIFIER);
		if (eli != null && eli.getSmallLayerIconUrl() != null && !"".equals(eli.getSmallLayerIconUrl())) {
			return eli.getSmallLayerIconUrl();
		} else {
			return null;
		}
	}

	public static Img getLargeLayerIcon(Layer<?> layer) {
		ClientExtraLayerInfo eli = (ClientExtraLayerInfo) layer.getLayerInfo().getWidgetInfo(
				ClientExtraLayerInfo.IDENTIFIER);
		if (eli != null && eli.getLargeLayerIconUrl() != null && !"".equals(eli.getLargeLayerIconUrl())) {
			return new Img(eli.getLargeLayerIconUrl());
		} else {
			if (layer instanceof RasterLayer) {
				UrlBuilder url = new UrlBuilder(Geomajas.getDispatcherUrl());
				url.addPath(LEGEND_ICONS_PATH);
				url.addPath(layer.getServerLayerId() + LEGEND_ICONS_TYPE);
				Img legendImage = new Img(url.toString());
				legendImage.setBackgroundColor("white");
				return legendImage;
			} else if (layer instanceof VectorLayer) {
				LayerType layerType = ((VectorLayer) layer).getLayerInfo().getLayerType();
				switch (layerType) {
					case POINT:
					case MULTIPOINT:
						return new Img(GltLayout.layerVectorPointIconLargeUrl);

					case LINESTRING:
					case MULTILINESTRING:
						return new Img(GltLayout.layerVectorLineIconLargeUrl);

					case POLYGON:
					case MULTIPOLYGON:
					case GEOMETRY:
						return new Img(GltLayout.layerVectorPolygonIconLargeUrl);

					default:
						throw new IllegalStateException("Unknown vector layer type " + layerType);
				}
			} else {
				throw new IllegalStateException("Unknown layer type");
			}
		}
	}

	public static Img getLegendImage(Layer<?> layer) {
		ClientExtraLayerInfo eli = (ClientExtraLayerInfo) layer.getLayerInfo().getWidgetInfo(
				ClientExtraLayerInfo.IDENTIFIER);
		if (eli != null && eli.getLegendImageUrl() != null && !"".equals(eli.getLegendImageUrl())) {
			return new Img(eli.getLegendImageUrl());
		} else {
			return null;
		}
	}

	public static String getLegendUrl(Layer<?> layer) {
		ClientExtraLayerInfo eli = (ClientExtraLayerInfo) layer.getLayerInfo().getWidgetInfo(
				ClientExtraLayerInfo.IDENTIFIER);
		if (eli != null && eli.getLegendImageUrl() != null && !"".equals(eli.getLegendImageUrl())) {
			return "images/" + eli.getLegendImageUrl();
		} else {
			return null;
		}
	}

	public static Img getLabelOverlayImg() {
		return new Img(GltLayout.layerLabelOverlayUrl);
	}

	public static Img getOpacityUnderlayImg() {
		return new Img(GltLayout.layerOpacityUnderlayUrl);
	}

}

@@ -464,49 +464,8 @@
 l;%0A%0A
-import org.geomajas.gwt.client.Geomajas;%0A
 impo
@@ -504,24 +504,24 @@
 ayer.Layer;%0A
+
 import org.g
@@ -620,56 +620,8 @@
 er;%0A
-import org.geomajas.gwt.client.util.UrlBuilder;%0A
 impo
@@ -896,135 +896,8 @@
  %7B%0A%0A
-%0A%09private static final String LEGEND_ICONS_PATH = %22legendgraphic%22;%0A%0A%09private static final String LEGEND_ICONS_TYPE = %22.png%22;%0A%09%0A
 %09pri
@@ -2099,283 +2099,57 @@
 %09%09%09%09
-UrlBuilder url = new UrlBuilder(Geomajas.getDispatcherUrl());%0A%09%09%09%09url.addPath(LEGEND_ICONS_PATH);%0A%09%09%09%09url.addPath(layer.getServerLayerId() + LEGEND_ICONS_TYPE);%0A%09%09%09%09Img legendImage = new Img(url.toString());%0A%09%09%09%09legendImage.setBackgroundColor(%22white%22);%0A%09%09%09%09return legendImage
+return new Img(GltLayout.layerRasterIconLargeUrl)
 ;%0A%09%09

Revert "Implemented WMS legend images in layer detail window #251"