package de.apps4ics.mountainnavigation.handlers;

import android.os.AsyncTask;
import android.util.Log;

import org.osmdroid.bonuspack.location.NominatimPOIProvider;
import org.osmdroid.bonuspack.location.POI;
import org.osmdroid.util.GeoPoint;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import de.apps4ics.mountainnavigation.MainActivity;
import de.apps4ics.mountainnavigation.pois.Poi;

/**
 * Created by Vinz on 11.03.2016.
 */
public class OsmHandler {
    public static final String OSM_TYPE_PATH = "path";
    public static final String OSM_TYPE_SPRING = "spring";
    public static final String OSM_TYPE_STREAM = "stream";
    public static final String OSM_TYPE_PEAK = "peak";
    public static final String OSM_TYPE_WILD_HUT = "wilderness_hut";
    public static final String OSM_TYPE_ALP_HUT = "alpine_hut";
    public static final String OSM_TYPE_WATER = "water";
    public static final String OSM_TYPE_AERIALWAY = "aerialway";
    public static final String[] OSM_TYPES = new String[]{ OSM_TYPE_PATH, OSM_TYPE_SPRING, OSM_TYPE_STREAM, OSM_TYPE_PEAK, OSM_TYPE_WILD_HUT, OSM_TYPE_ALP_HUT, OSM_TYPE_WATER, OSM_TYPE_AERIALWAY };

    public List<POI> getPoisAround(double lat, double lon, int limit) {
        List<POI> poisAround = new ArrayList<>();
        for(String type : OSM_TYPES) {
            try {
                poisAround.addAll(new GetPoiFromOsm().execute(type).get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        if(limit != -1) return poisAround.subList(0, Math.min(limit, poisAround.size()));
        else return poisAround;
    }
}

class GetPoiFromOsm extends AsyncTask<String, Void, List> {
    protected List<POI> doInBackground(String... params) {
        try {
            NominatimPOIProvider poiProvider = new NominatimPOIProvider("");
            List<POI> foundPois = poiProvider.getPOICloseTo(new GeoPoint(MainActivity.getLocation()), params[0], 50, 0.1);
            return foundPois;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(List arrayList) {
        /*for(POI p : (ArrayList<Poi>) arrayList) {
            Log.d(MainActivity.TAG, p.mCategory + ", " + p.mType + ", " + p.mUrl);
        }*/
    }
}