//
// SearchTourJsonMapper.m
// LiveProject
//
// Created by Ziv on 15/9/24.
//
//

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

@implementation SearchTourResponseJsonMapper

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

		RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[SearchTourResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[SearchTourResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"products" mapping:searchDataMapping];

		self.objectMapping = responseMapping;
	}
	return self;
}

#pragma mark - Public

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