//
// SearchSelfCheckInJsonMapper.m
// Project
//
// Created by ziv on 2017/7/31.
//
//

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

@implementation SelfCheckInPageResponseJsonMapper

- (instancetype)init {
	if (self = [super init]) {
		RKObjectMapping *stockMapping = [[RKObjectMapping alloc] initWithClass:[SoldStock class]];
		[stockMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:@[@"scheduleName", @"blockStatus", @"cn", @"departure", @"arrival"] retainEntries:@{ @"dateStock.date" : @"departureDate", @"dateStock.stockId" : @"stock_id", @"soldToName" : @"sold_to_name", @"dateStock.vendorCheckIn" : @"vendor_checkin" }]];

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

		self.objectMapping = responseMapping;
	}
	return self;
}

- (NSError *)parseWithJSONObject:(id)jsonObject {
	self.response = [[SelfCheckInPageResponse 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
