package com.nutiteq.advancedmap.maplisteners;

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.webkit.WebView;

import com.nutiteq.MapView;
import com.nutiteq.components.MapPos;
import com.nutiteq.components.MapTile;
import com.nutiteq.components.MutableMapPos;
import com.nutiteq.geometry.Marker;
import com.nutiteq.geometry.VectorElement;
import com.nutiteq.layers.raster.WmsLayer;
import com.nutiteq.log.Log;
import com.nutiteq.projections.EPSG3857;
import com.nutiteq.ui.MapListener;
import com.nutiteq.ui.ViewLabel;
import com.nutiteq.utils.UiUtils;

/**
 * 
 * Used for click detection on WMS map, where GetFeatureInfo can be requested. Click initiates
 * HTTP request to server to get additional metadata as HTML. Then WebView is embed to a Label and shown
 * on map.
 * 
 * @author jaak
 *
 */
public class WmsLayerClickListener extends MapListener {

    private WmsLayer layer;
    private Marker clickMarker;
    private MapView mapView;

	// activity is often useful to handle click events
	public WmsLayerClickListener(Activity activity, MapView mapView, WmsLayer mapLayer, Marker clickMarker) {
		this.layer = mapLayer;
		this.clickMarker = clickMarker;
		this.mapView = mapView;
	}

	// Map drawing callbacks for OpenGL manipulations
	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
	}

	@Override
	public void onDrawFrameAfter3D(GL10 gl, float zoomPow2) {
	}

	@Override
	public void onDrawFrameBefore3D(GL10 gl, float zoomPow2) {
	}

	// Vector element (touch) handlers
	@Override
	public void onLabelClicked(VectorElement vectorElement, boolean longClick) {
	    Log.debug("clicked on label");
	}

	@Override
	public void onVectorElementClicked(VectorElement vectorElement, double x,
			double y, boolean longClick) {

	}

	// Map View manipulation handlers
	@Override
	public void onMapClicked(final double x, final double y,
			final boolean longClick) {
		// x and y are in base map projection, we convert them to the familiar
		// WGS84
		Log.debug("onMapClicked " + (new EPSG3857()).toWgs84(x, y).x + " "
				+ (new EPSG3857()).toWgs84(x, y).y + " longClick: " + longClick);
		
		MutableMapPos tilePos = new MutableMapPos();
		MapTile clickedTile = mapView.worldToMapTile(x, y, tilePos);

		Log.debug("clicked tile "+clickedTile+" pos:"+tilePos);
		
        // TODO: getFeatureInfo does network request, so call it from another thread

        String featureInfo = layer.getFeatureInfo(clickedTile, tilePos);

        if (featureInfo == null) {
            return;
        } else {
            updateMarker(new MapPos(x, y), featureInfo);
        }
		
	}
	

    private void updateMarker(MapPos pos, String text) {
        
        if(clickMarker != null){
            clickMarker.setMapPos(pos);
            mapView.selectVectorElement(clickMarker);
            WebView webView = ((WebView)((ViewLabel)clickMarker.getLabel()).getView());
            Log.debug("showing html: "+text);
            webView.loadDataWithBaseURL("file:///android_asset/",UiUtils.HTML_HEAD+text+UiUtils.HTML_FOOT, "text/html", "UTF-8",null);
            
        }
    }


	@Override
	public void onMapMoved() {
	}

}

@@ -119,16 +119,43 @@
 tivity;%0A
+import android.os.Handler;%0A
 import a
@@ -407,16 +407,72 @@
 lement;%0A
+import com.nutiteq.layers.raster.UtfGridLayerInterface;%0A
 import c
@@ -1070,16 +1070,61 @@
 mapView;
+%0A    private Handler handler = new Handler();
 %0A%0A%09// ac
@@ -1244,20 +1244,17 @@
 msLayer 
-mapL
+l
 ayer, Ma
@@ -1292,12 +1292,9 @@
 r = 
-mapL
+l
 ayer
@@ -2290,16 +2290,22 @@
 );%0A%09%09%0A%09%09
+final 
 MutableM
@@ -2343,16 +2343,22 @@
 os();%0A%09%09
+final 
 MapTile 
@@ -2473,104 +2473,179 @@
 s);%0A
+%0A
 %09%09
-%0A        // TODO:
+// perform network query to
  get
-F
+ f
 eature
-Info does network request, so call it from another thread%0A%0A       
+ info. This must be done in separate thread!%0A%09%09new Thread(new Runnable() %7B%0A%09%09  @Override%0A%09%09  public void run() %7B%0A%09          final
  Str
@@ -2707,16 +2707,19 @@
 ePos);%0A%0A
+%09  
         
@@ -2741,24 +2741,26 @@
  == null) %7B%0A
+%09 
             
@@ -2763,16 +2763,17 @@
     
+ 
 return;%0A
     
@@ -2768,16 +2768,19 @@
 return;%0A
+%09  
         
@@ -2792,82 +2792,322 @@
 e %7B%0A
-            updateMarker(new MapPos(x, y), featureInfo);%0A        %7D%0A%09%09
+%09              // update marker in UI thread %0A%09              handler.post(new Runnable() %7B%0A                      @Override%0A                      public void run() %7B%0A                          updateMarker(new MapPos(x, y), featureInfo);%0A                      %7D%0A%09              %7D);%0A%09          %7D%0A%09%09  %7D%0A%09%09%7D).start();
 %0A%09%7D
-%0A%09
 %0A%0A  
@@ -3640,10 +3640,144 @@
 ) %7B%0A%09%7D%0A%0A
+    public WmsLayer getLayer() %7B%0A      return layer;%0A    %7D%0A    %0A    public Marker getClickMarker() %7B%0A      return clickMarker;%0A    %7D%0A%0A
 %7D%0A

Fix WMS listener threading issue: perform network query in separate thread, not in UI thread