/* (non-Javadoc)
   * @see com.nebulent.vectura.services.resources.v1.AccountResource#createAccountRide(java.lang.String, nebulent.schema.software.vectura._1.Ride)
   */
  @Override
  public Ride createAccountRide(String accountId, Ride ride) {
    if (StringUtils.isBlank(accountId)) {
      throw new BadRequestException(
          new StatusResponse(false, "Account ID is a required field.", null));
    }

    ride.setAccountId(accountId);
    com.nebulent.vectura.data.model.mongodb.Ride mongoRide = DomainUtils.toRide(ride);
    mongoRide = getMongoRepository().getRideRepository().save(mongoRide);
    ride = DomainUtils.toRide(mongoRide);

    // Create pick-up location.
    Place place = new Place();
    place.setAddress(ride.getDropOffAddress());
    Place dropoffPlace = createAccountPlace(accountId, place);
    if (dropoffPlace != null) {
      ride.setDropOffAddress(dropoffPlace.getAddress());
    }

    // Create drop-off location.
    place = new Place();
    place.setAddress(ride.getPickupAddress());
    Place pickupPlace = createAccountPlace(accountId, place);
    if (pickupPlace != null) {
      ride.setPickupAddress(pickupPlace.getAddress());
    }

    return ride;
  }
  /* (non-Javadoc)
   * @see com.nebulent.vectura.services.resources.v1.AccountResource#createAccountPlace(java.lang.String, nebulent.schema.software.vectura._1.Place)
   */
  @Override
  public Place createAccountPlace(String accountId, Place placeType) {
    if (StringUtils.isBlank(accountId)) {
      throw new BadRequestException(
          new StatusResponse(false, "Account ID is a required field.", null));
    }

    placeType.setAccountId(accountId);
    com.nebulent.vectura.data.model.mongodb.Place location = DomainUtils.toLocation(placeType);
    location.setAccountUuid(accountId);

    Map<AddressComponent, String> parsedAddr =
        AddressParser.parseAddress(location.getAddress().toSingleLine());
    String normalizedAddress = AddressStandardizer.toSingleLine(parsedAddr);
    int addressHash = new HashCodeBuilder().append(normalizedAddress).toHashCode();

    if (addressHash != 0) {
      System.out.println(
          "Trying to find by address hash:"
              + addressHash
              + " and "
              + location.getAddress().toString());
      com.nebulent.vectura.data.model.mongodb.Place placeByHash =
          getMongoRepository().findPlaceByAccountUuidAndAddressHash(accountId, addressHash);
      if (placeByHash == null) {
        AddressInfo addressType = mapService.getLocationByAddress(location.getAddress().toString());
        if (addressType != null) {
          com.nebulent.vectura.data.model.mongodb.core.AddressInfo addressInfo =
              DomainUtils.toAddress(addressType);
          if (logger.isDebugEnabled()) {
            logger.debug("Adding location with address:" + addressInfo);
          }
          if (addressInfo != null && StringUtils.isNotBlank(addressInfo.getAddressLine1())) {
            location.setAddress(addressInfo);
            location.getAddress().setHash(addressHash);
            location.setLocation(location.getAddress().getLocation());
            location = getMongoRepository().getPlaceRepository().save(location);
          }
        }
      } else {
        System.out.println(
            "Found by address hash:" + addressHash + " and " + placeByHash.toString());

        location = placeByHash;
      }
    }

    return DomainUtils.toLocation(location);
  }