//
// GetDynamicPriceResponseJsonMapper.m
// LiveProject
//
// Created by Ziv on 15/9/18.
//
//

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

@implementation GetDynamicPriceResponseJsonMapper

- (instancetype)init {
	if (self = [super init]) {
		RKObjectMapping *priceEntryMapping = [RKObjectMapping mappingForClass:[GetProductOptionsResponsePeopleType class]];
		[priceEntryMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetProductOptionsResponsePeopleType class]] retainEntries:@{ @"type" : @"name", @"limitFrom" : @"ageFrom", @"limitTo" : @"ageTo", @"value" : @"peoplePrice" }]];

		RKObjectMapping *dynamicPriceResponseMapping = [RKObjectMapping mappingForClass:[GetDynamicPriceResponse class]];
		[dynamicPriceResponseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetDynamicPriceResponse class]]];
		[dynamicPriceResponseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"productPrice.priceEntries" toKeyPath:@"priceEntries" withMapping:priceEntryMapping]];

		self.objectMapping = dynamicPriceResponseMapping;
	}
	return self;
}

#pragma mark - Public

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