//
// CheckAvailabilityAndPricesResponseJsonMapper.m
// LiveProject
//
// Created by Ziv on 15/9/22.
//
//

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

@implementation CheckAvailabilityAndPricesResponseJsonMapper

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

		RKObjectMapping *entryMapping = [RKObjectMapping mappingForClass:[_Entry class]];
		[entryMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[_Entry class]]];
		[entryMapping addRelationshipMappingWithSourceKeyPath:@"people_types" mapping:priceEntryMapping];

		RKObjectMapping *couponMapping = [RKObjectMapping mappingForClass:[_Coupon class]];
		[couponMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[_Coupon class]]];

		RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[CheckAvailabilityAndPricesResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[CheckAvailabilityAndPricesResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"entries" mapping:entryMapping];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"coupons" mapping:couponMapping];

		self.objectMapping = responseMapping;
	}
	return self;
}

#pragma mark - Public

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