package de.fhpotsdam.unfolding.examples.marker.infomarker;

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

import org.apache.log4j.Logger;

import processing.core.PApplet;
import processing.core.PFont;
import processing.xml.XMLElement;
import codeanticode.glgraphics.GLConstants;
import de.fhpotsdam.unfolding.Map;
import de.fhpotsdam.unfolding.geo.Location;
import de.fhpotsdam.unfolding.mapdisplay.MapDisplayFactory;
import de.fhpotsdam.unfolding.marker.Marker;
import de.fhpotsdam.unfolding.marker.MarkerManager;
import de.fhpotsdam.unfolding.providers.OpenStreetMap;
import de.fhpotsdam.unfolding.utils.MapUtils;

/**
 * Multiple markers on single map. If mouse hovers over a marker, it is selcted, thus the label is
 * shown.
 * 
 * On click the clicked marker is returned from the MarkerManager and printed (debug).
 */
public class InfoMarkerApp extends PApplet {

	public static Logger log = Logger.getLogger(ZoomDependentMarkerApp.class);

	Map map;

	public void setup() {
		size(800, 600, GLConstants.GLGRAPHICS);
		smooth();

		map = new Map(this, "map", 10, 10, 780, 580, true, false,
				new OpenStreetMap.CloudmadeProvider(MapDisplayFactory.OSM_API_KEY, 23058));
		map.setTweening(false);
		MapUtils.createDefaultEventDispatcher(this, map);

		PFont font = loadFont("Miso-Light-12.vlw");
		// Create markers and add them to the MarkerManager
		List<Marker> labeledMarkers = GeoRSSLoader.loadGeoRSSMarkers(this, "bbc-georss-test.xml", font);
		MarkerManager markerManager = new MarkerManager(map, labeledMarkers);
		map.mapDisplay.addMarkerManager(markerManager);
	}

	public void draw() {
		background(0);

		// Draws map, and all markers connected to this map from the MarkerManager
		map.draw();
	}

	public void mouseMoved() {
		MarkerManager mm = map.mapDisplay.getLastMarkerManager();
		
		// Deselect all marker
		for (LabeledMarker lm : (List<LabeledMarker>) mm.getMarkers()) {
			lm.setSelected(false);
		}
		
		// Select hit marker
		LabeledMarker marker = (LabeledMarker) mm.getFirstHitMarker(mouseX, mouseY);
		if (marker != null) {
			marker.setSelected(true);
		}

	}
}

//<<<<<<< HEAD:examples/de/fhpotsdam/unfolding/examples/marker/infomarker/InfoMarkerApp.java
//=======
//	public static List<Marker> loadGeoRSSMarkers(PApplet p, String url, PFont font) {
//		List<Marker> markers = new ArrayList<Marker>();
//
//		XMLElement rss = new XMLElement(p, url);
//		XMLElement[] itemXMLElements = rss.getChildren("channel/item");
//		for (int i = 0; i < itemXMLElements.length; i++) {
//			String name = itemXMLElements[i].getChild("title").getContent();
//			XMLElement latXML = itemXMLElements[i].getChild("geo:lat");
//			XMLElement lonXML = itemXMLElements[i].getChild("geo:long");
//			if (latXML != null && latXML.getContent() != null) {
//				float lat = Float.valueOf(latXML.getContent());
//				float lon = Float.valueOf(lonXML.getContent());
//
//				Location location = new Location(lat, lon);
//				LabeledMarker labeledMarker = new LabeledMarker(font, name, location, 10);
//				// labeledMarker.setVisible(false);
//				labeledMarker.color = p.color(255, 0, 0);
//				markers.add(labeledMarker);
//			}
//		}
//		return markers;
//	}
//
//>>>>>>> develop:examples/de/fhpotsdam/unfolding/examples/marker/InfoMarkerApp.java
//}

@@ -2100,1171 +2100,4 @@
 %7D%0A%7D%0A
-%0A//%3C%3C%3C%3C%3C%3C%3C HEAD:examples/de/fhpotsdam/unfolding/examples/marker/infomarker/InfoMarkerApp.java%0A//=======%0A//%09public static List%3CMarker%3E loadGeoRSSMarkers(PApplet p, String url, PFont font) %7B%0A//%09%09List%3CMarker%3E markers = new ArrayList%3CMarker%3E();%0A//%0A//%09%09XMLElement rss = new XMLElement(p, url);%0A//%09%09XMLElement%5B%5D itemXMLElements = rss.getChildren(%22channel/item%22);%0A//%09%09for (int i = 0; i %3C itemXMLElements.length; i++) %7B%0A//%09%09%09String name = itemXMLElements%5Bi%5D.getChild(%22title%22).getContent();%0A//%09%09%09XMLElement latXML = itemXMLElements%5Bi%5D.getChild(%22geo:lat%22);%0A//%09%09%09XMLElement lonXML = itemXMLElements%5Bi%5D.getChild(%22geo:long%22);%0A//%09%09%09if (latXML != null && latXML.getContent() != null) %7B%0A//%09%09%09%09float lat = Float.valueOf(latXML.getContent());%0A//%09%09%09%09float lon = Float.valueOf(lonXML.getContent());%0A//%0A//%09%09%09%09Location location = new Location(lat, lon);%0A//%09%09%09%09LabeledMarker labeledMarker = new LabeledMarker(font, name, location, 10);%0A//%09%09%09%09// labeledMarker.setVisible(false);%0A//%09%09%09%09labeledMarker.color = p.color(255, 0, 0);%0A//%09%09%09%09markers.add(labeledMarker);%0A//%09%09%09%7D%0A//%09%09%7D%0A//%09%09return markers;%0A//%09%7D%0A//%0A//%3E%3E%3E%3E%3E%3E%3E develop:examples/de/fhpotsdam/unfolding/examples/marker/InfoMarkerApp.java%0A//%7D%0A

clean up