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

#import "HereSDKDemandRideQuery+JSDictionary.h"

@implementation HereSDKDemandRideQuery (JSDictionary)

+ (HereSDKDemandRideQuery *)requestFromJSParams:(NSDictionary *)params
{
    NSNumber* fromUpdateTime = [params objectForKey:@"fromUpdateTime"];
    NSDate* fromUpdateTimeDate = fromUpdateTime ?
    [NSDate dateWithTimeIntervalSince1970:(fromUpdateTime.longLongValue / 1000.0)] : nil;
    NSNumber* limit = [params objectForKey:@"limit"] ?: @(200);
    NSString* statusFilter = [params objectForKey:@"statusFilter"];
    NSString* sortBySrt = [params objectForKey:@"sortBy"];
    
    HereSDKDemandRideQueryStatusFilter filter = [HereSDKDemandRideQuery statusFilterByString:statusFilter];
    
    HereSDKDemandRideQuerySortType type = [HereSDKDemandRideQuery sortTypeByString:sortBySrt];
    
    return [HereSDKDemandRideQuery rideQueryWithUpdateTime:fromUpdateTimeDate limit:[limit intValue] statusFilter:filter sortBy:type];
}

+ (HereSDKDemandRideQueryStatusFilter) statusFilterByString:(nullable NSString *)statusFilter{
    return [[@{
            @"PAST" : @(HereSDKDemandRideQueryStatusPast),
            @"FUTURE" : @(HereSDKDemandRideQueryStatusFuture),
            @"ONGOING" : @(HereSDKDemandRideQueryStatusOngoing),
            @"ALL" : @(HereSDKDemandRideQueryStatusAll)
            } objectForKey:statusFilter] ?: @(HereSDKDemandRideQueryStatusAll) intValue];
}

+ (HereSDKDemandRideQuerySortType) sortTypeByString:(nullable NSString *)sortBy{
    return [[@{
               @"UPDATE_TIME_ASC" : @(HereSDKDemandRideQuerySortTypeUpdateTimeAsc),
               @"UPDATE_TIME_DESC" : @(HereSDKDemandRideQuerySortTypeUpdateTimeDesc),
               @"CREATE_TIME_ASC" : @(HereSDKDemandRideQuerySortTypeCreateTimeAsc),
               @"CREATE_TIME_DESC" : @(HereSDKDemandRideQuerySortTypeCreateTimeDesc),
               } objectForKey:sortBy] ?: @(HereSDKDemandRideQuerySortTypeUpdateTimeAsc) intValue];
}

@end
