//
// SchedulePassengerListResponseJsonMapper.m
// Project
//
// Created by Ziv on 2017/4/18.
//
//

#import "CustomerListPageResponseJsonMapper.h"
#import "BeanUtils.h"
#import "CocoaUtils.h"
#import <RestKit/RKObjectMappingOperationDataSource.h>
#import "SoldStock.h"
#import <RestKit/RestKit.h>

@implementation CustomerListPageResponseJsonMapper

- (instancetype)init {
	if (self = [super init]) {
		RKObjectMapping *stockMapping = [[RKObjectMapping alloc] initWithClass:[SoldStock class]];
		[stockMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[[BeanUtils keysForAccessibleSimplePropertyWithClass:[SoldStock class]] arrayByAddingObjectsFromArray:@[@"departure", @"arrival"]] retainEntries:@{@"stockId" : @"stock_id", @"productId" : @"product_id", @"soldToName" : @"sold_to_name", @"dateStock.vendorCheckIn" : @"vendor_checkin"}]];

		RKObjectMapping *responseMapping = [[RKObjectMapping alloc] initWithClass:[CustomerListPageResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[CustomerListPageResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"soldStockList" mapping:stockMapping];

		self.objectMapping = responseMapping;
	}
	return self;
}

#pragma mark - Public

- (NSError *)parseWithJSONObject:(id)jsonObject {
	self.response = [[CustomerListPageResponse alloc] init];

	RKMappingOperation *mappingOperation = [[RKMappingOperation alloc] initWithSourceObject:jsonObject destinationObject:self.response mapping:self.objectMapping];
	RKObjectMappingOperationDataSource *mappingOperationDataSource = [[RKObjectMappingOperationDataSource alloc] init];
	mappingOperation.dataSource = mappingOperationDataSource;

	NSError *error = nil;
	[mappingOperation performMapping:&error];
	return error;
}

@end
