/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.GPSCalculation;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;

/**
 * Generates attraction points out of real data from osm
 * The maximal number of attraction points can be set
 * 
 * @author Martin Hellwig
 * @version 1.0, 02.07.2015
 */
public class OnlineJSONAttractionGenerator extends JSONAttractionGenerator {

	private String amenity;
		
	@XMLConfigurableConstructor({"numberOfAttractionPoints", "maximumRadius", "placementJsonPath", "amenity"})
	public OnlineJSONAttractionGenerator(int numberOfAttractionPoints, double maximumRadius, 
			String placementJsonPath, String amenity) {

		super(numberOfAttractionPoints, maximumRadius, placementJsonPath + 
				"pois" + 
				GPSCalculation.getLatCenter() + 
				GPSCalculation.getLonCenter() + 
				GPSCalculation.getZoom() +
				amenity + ".json");
		
		this.amenity = amenity;
	}	

	@Override
	protected JSONArray getPOIArray() {	
		//Check if the file with same properties (same location) already exists
		File f = new File(placementJsonFile);
		if(!f.exists()) {
			String poiString = "";
			JSONArray allPOI = null;
			InputStream in;
			try {
				in = new URL( "http://overpass-api.de/api/interpreter?data=%5Bout:json%5D;node%5Bamenity=" + amenity + "%5D%28" 
						+ GPSCalculation.getLatUpper() + "%2C" + GPSCalculation.getLonLeft() + "%2C" 
						+ GPSCalculation.getLatLower() + "%2C" + GPSCalculation.getLonRight() + "%29%3Bout%3B" ).openStream();
				poiString = IOUtils.toString(in);
				//Save the json data in file
				PrintWriter out = new PrintWriter(placementJsonFile);
				out.print(poiString);
				out.close();
				JSONObject poiData = new JSONObject(poiString);
				allPOI = poiData.getJSONArray("elements");
			} catch (JSONException e) {
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return allPOI;
		}
		else {
			return super.getPOIArray();
		}		
	}

}
