package com.jtaxi.services.detectors;
import android.content.Context;
import android.location.Address;
import android.location.Location;
import com.google.android.gms.maps.model.LatLng;
import com.jtaxi.ProjectConfig;
import com.jtaxi.helper.MapHelper;
import com.jtaxi.services.JsonClientUsage;
import com.jtaxi.services.JsonResponseHandler;
import com.jtaxi.services.http.RequestAction;
import org.json.JSONObject;
/**
* Created by Roman on 3/26/14.
*/
public class EtaDetector {
public static final int LESS_MINUTE = 0;
public static final int MINUTE = 1;
public static RequestAction calculateETA(Context context, Address fromAddress, Address toAddress, Callback callback) {
LatLng from = MapHelper.fromAddress(fromAddress);
LatLng to = MapHelper.fromAddress(toAddress);
return calculateETA(context, from, to, callback);
}
public static RequestAction calculateETA(Context context, Location fromLocation, Location toLocation, Callback callback) {
LatLng from = MapHelper.fromLocation(fromLocation);
LatLng to = MapHelper.fromLocation(toLocation);
return calculateETA(context, from, to, callback);
}
public static RequestAction calculateETA(final Context context, LatLng from, LatLng to, final Callback callback) {
return JsonClientUsage.getHours(context, from, to, new JsonResponseHandler.RequestCallback() {
private static final String MINUTES = "minutes";
@Override
public void onSuccess(JSONObject data) {
Integer minutes = null;
try {
if (data.opt(MINUTES) != null) {
minutes = data.getInt(MINUTES);
}
} catch (Exception e) {
if (ProjectConfig.DEBUG) {
e.printStackTrace();
}
}
if (minutes != null) {
callback.onTimeCalculated(minutes);
} else {
callback.onFailure("Неможливо визначити час прибуття");
}
}
@Override
public void onFailure(String message) {
callback.onFailure(message);
}
});
}
public static interface Callback {
public void onTimeCalculated(int minutes);
public void onFailure(String message);
}
}