package module3;

//Java utilities libraries
import java.util.ArrayList;
//import java.util.Collections;
//import java.util.Comparator;
import java.util.List;

//Processing library
import processing.core.PApplet;

//Unfolding libraries
import de.fhpotsdam.unfolding.UnfoldingMap;
import de.fhpotsdam.unfolding.marker.Marker;
import de.fhpotsdam.unfolding.data.PointFeature;
import de.fhpotsdam.unfolding.marker.SimplePointMarker;
import de.fhpotsdam.unfolding.providers.Google;
import de.fhpotsdam.unfolding.providers.MBTilesMapProvider;
import de.fhpotsdam.unfolding.utils.MapUtils;

//Parsing library
import parsing.ParseFeed;

/** EarthquakeCityMap
 * An application with an interactive map displaying earthquake data.
 * Author: UC San Diego Intermediate Software Development MOOC team
 * @author Mohammed Ali
 * Date: July 17, 2015
 * */
public class EarthquakeCityMap extends PApplet {

	// You can ignore this.  It's to keep eclipse from generating a warning.
	private static final long serialVersionUID = 1L;

	// IF YOU ARE WORKING OFFLINE, change the value of this variable to true
	private static final boolean offline = false;
	
	// Less than this threshold is a light earthquake
	public static final float THRESHOLD_MODERATE = 5;
	// Less than this threshold is a minor earthquake
	public static final float THRESHOLD_LIGHT = 4;

	/** This is where to find the local tiles, for working without an Internet connection */
	public static String mbTilesString = "blankLight-1-3.mbtiles";
	
	// The map
	private UnfoldingMap map;
	
	//feed with magnitude 2.5+ Earthquakes
	private String earthquakesURL = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.atom";

	
	public void setup() {
		size(950, 600, OPENGL);

		if (offline) {
		    map = new UnfoldingMap(this, 200, 50, 700, 500, new MBTilesMapProvider(mbTilesString));
		    earthquakesURL = "2.5_week.atom"; 	// Same feed, saved Aug 7, 2015, for working offline
		}
		else {
			map = new UnfoldingMap(this, 200, 50, 700, 500, new Google.GoogleMapProvider());
			// IF YOU WANT TO TEST WITH A LOCAL FILE, uncomment the next line
			//earthquakesURL = "2.5_week.atom";
		}
		
	    map.zoomToLevel(2);
	    MapUtils.createDefaultEventDispatcher(this, map);	
			
	    // The List you will populate with new SimplePointMarkers
	    List<Marker> markers = new ArrayList<Marker>();

	    //Use provided parser to collect properties for each earthquake
	    //PointFeatures have a getLocation method
	    List<PointFeature> earthquakes = ParseFeed.parseEarthquake(this, earthquakesURL);	 
	    int markerColor = 0;
	    float markerRadius = 0.0f;
	    for(PointFeature feature: earthquakes){
	    	SimplePointMarker simplePointMarker = 
	    			new SimplePointMarker(feature.getLocation(), feature.properties);
	    	Object magnitudeObj = feature.getProperty("magnitude");
	    	float magnitude = Float.parseFloat(magnitudeObj.toString());
	    	if(magnitude <THRESHOLD_LIGHT){
	    		markerColor = color(0, 0, 255);
	    		markerRadius = 5.0f;
	    	}else if (magnitude < THRESHOLD_MODERATE){
	    		markerColor = color(255, 255, 0);
	    		markerRadius = 10.0f;
	    	}else{
	    		markerColor = color(255, 0, 0);
	    		markerRadius = 15.0f;
	    	}
	    	simplePointMarker.setColor(markerColor);
	    	simplePointMarker.setRadius(markerRadius);
	    	map.addMarker(simplePointMarker);
	    }
	}
		
	// A suggested helper method that takes in an earthquake feature and 
	// returns a SimplePointMarker for that earthquake
	// TODO: Implement this method and call it from setUp, if it helps
	private SimplePointMarker createMarker(PointFeature feature)
	{
		// finish implementing and use this method, if it helps.
		return new SimplePointMarker(feature.getLocation());
	}
	
	public void draw() {
	    background(10);
	    map.draw();
	    addKey();
	}


	// helper method to draw key in GUI
	// TODO: Implement this method to draw the key
	private void addKey() 
	{	//Set the key mar area
		fill(250, 243, 243);
		rect(30, 50, 150, 300, 24);
	
	}
}

@@ -4013,15 +4013,120 @@
 0, 24);%0A
+%09%09%0A%09%09//Set the title%0A%09%09textSize(14);%0A%09%09textAlign(CENTER);%0A%09%09fill(50);%0A%09%09text(%22Earthquake Key%22, 100, 70);%0A
 %09%0A%09%7D%0A%7D%0A

Add Earthquake Key Title