//
// GetPricesResponseJsonMapper.m
// Project
//
// Created by haibara on 5/12/17.
//
//

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

@implementation GetPricesResponseJsonMapper

- (instancetype)init {
	if (self = [super init]) {
		RKObjectMapping *priceMapping = [RKObjectMapping mappingForClass:[GetProductOptionsResponsePeopleType class]];
		[priceMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetProductOptionsResponsePeopleType class]]];

		RKObjectMapping *priceEntryMapping = [RKObjectMapping mappingForClass:[ProductAiPriceBean class]];
		[priceEntryMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[ProductAiPriceBean class]]];
		[priceEntryMapping addRelationshipMappingWithSourceKeyPath:@"price" mapping:priceMapping];

		RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[GetPricesResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetPricesResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"prices" mapping:priceEntryMapping];

		self.objectMapping = responseMapping;
	}
	return self;
}

#pragma mark - Public

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