//
/***************************************************************
 * Copyright © 2017 HERE Global B.V. All rights reserved. *
 **************************************************************/

#import "HereSDKDemandRideOffersRequest+JSDictionary.h"
#import "HereSDKDemandRoute+JSDictionary.h"

@implementation HereSDKDemandRideOffersRequest (JSDictionary)

+ (HereSDKDemandRideOffersRequest *)requestFromJSParams:(NSDictionary *)params
{
  HereSDKDemandRoute *route = [HereSDKDemandRoute routeFromJSParams:params[@"rideWaypoints"]];

  HereSDKDemandBookingConstraints *bookingContraints = nil;
  if (params[@"constraints"])
  {
    bookingContraints = [HereSDKDemandBookingConstraints new];
    [bookingContraints setNumberOfPassengers: [params[@"constraints"][@"passengerCount"] intValue]];
    [bookingContraints setNumberOfSuitcases: [params[@"constraints"][@"suitcaseCount"] intValue]];
  }

  NSDate *prebookPickuptime = [NSDate dateWithTimeIntervalSince1970:([params[@"prebookPickupTime"] doubleValue] / 1000.0)];

  HereSDKDemandPriceRange *priceRange = nil;
  if (params[@"priceRange"])
  {
    NSDecimalNumber *lowerPrice = [params[@"priceRange"] objectForKey:@"lowerBound"];
    NSDecimalNumber *upperPrice = [params[@"priceRange"] objectForKey:@"upperBound"];
    priceRange = [HereSDKDemandPriceRange priceRangeWithUpperBound:upperPrice lowerBound:lowerPrice currency:params[@"priceRange"][@"currencyCode"]];
  }


  HereDemandRideOffersRequestSortType typedSortType = HereDemandRideOffersRequestSortTypeUnknown;
  if (params[@"sortType"])
  {
    if ([params[@"sortType"] isEqualToString:@"BY_PRICE"]) {
      typedSortType = HereDemandRideOffersRequestSortTypeByPrice;
    } else if ([params[@"sortType"] isEqualToString:@"BY_ETA"]) {
      typedSortType = HereDemandRideOffersRequestSortType_ByEta;
    }
  }

  HereSDKDemandTransitOptions *transitOptions = nil;
  if (params[@"transitOptions"])
  {
    NSDictionary *transitOptionsDict = params[@"transitOptions"];
    NSNumber *maxTransfers = nil;
    if (transitOptionsDict[@"maxAllowedTransfers"]) {
      int maxTransfersVal = [transitOptionsDict[@"maxAllowedTransfers"] intValue];
      maxTransfers = [NSNumber numberWithInt:maxTransfersVal];
    }

    NSNumber *maxWalkingDistanceMeters = nil;
    if (transitOptionsDict[@"maxWalkingDistance"]) {
      int maxWalkingDistanceMetersVal = [params[@"transitOptions"][@"maxWalkingDistance"] intValue];
      maxWalkingDistanceMeters = [NSNumber numberWithInt:maxWalkingDistanceMetersVal];
    }

    transitOptions = [HereSDKDemandTransitOptions transitOptionsWithMaxTransfers:maxTransfers maxWalkingDistance:maxWalkingDistanceMeters locale:transitOptionsDict[@"locale"]];
  }


  HereSDKDemandRideOffersRequest *request = [HereSDKDemandRideOffersRequest rideOffersWithRoute:route
                                                                                    constraints:bookingContraints
                                                                              prebookPickupTime:prebookPickuptime
                                                                                     priceRange:priceRange
                                                                                       sortType:typedSortType
                                                                                  passengerNote:params[@"passengerNote"]
                                                                                 transitOptions:transitOptions];

  return request;
}

@end
