class DirectionApiLogic {

	public ReadableInstant whenIHaveToGo(String origin, String destination, TravelMode mode, DateTime arrivalTime) {
		DirectionsRoute[] routes;
		DirectionsRoute route;
		switch (mode) {
		case DRIVING:
		case BICYCLING:
			routes = this.calls.fromToWithDepartureTime(origin, destination, mode, DateTime.now());
			route = findBestRoute(routes);
			if (route != null && route.legs[0].durationInTraffic != null && arrivalTime.getMillis() > DateTime.now()
					.plusSeconds(Math.toIntExact(route.legs[0].durationInTraffic.inSeconds)).getMillis()) {
				return new DateTime(arrivalTime.minusSeconds(Math.toIntExact(route.legs[0].durationInTraffic.inSeconds))
						.getMillis());
			} else if (route != null && route.legs[0].duration != null && arrivalTime.getMillis() > DateTime.now()
					.plusSeconds(Math.toIntExact(route.legs[0].duration.inSeconds)).getMillis()) {
				return new DateTime(
						arrivalTime.minusSeconds(Math.toIntExact(route.legs[0].duration.inSeconds)).getMillis());
			}
			break;
		case TRANSIT:
			routes = this.calls.fromToWithArrivalTime(destination, origin, mode, arrivalTime);
			if (routes != null && routes[0] != null) {
				return routes[0].legs[0].departureTime;
			}
			break;
		default:
			break;
		}

		return null;
	}

}