//
//  GetTourSearchSourceResponseJsonMapper.m
//  TakeTours
//
//  Created by 包光清 on 2019/2/18.
//

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

@implementation GetTourSearchSourceResponseJsonMapper
- (instancetype)init {
	if (self = [super init]) {
		RKObjectMapping *aliaseMapping = [RKObjectMapping mappingForClass:[Aliase class]];
		[aliaseMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Aliase class]] retainEntries:@{@"id" : @"aliase_id"}]];
		
		RKObjectMapping *areaMapping = [[RKObjectMapping alloc] initWithClass:[Area class]];
		[areaMapping addRelationshipMappingWithSourceKeyPath:@"aliases" mapping:aliaseMapping];
		[areaMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Area class]] retainEntries:@{@"id" : @"area_id"}]];
		
		RKObjectMapping *responseMapping = [[RKObjectMapping alloc] initWithClass:[GetTourSearchSourceResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetTourSearchSourceResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"object_list" mapping:areaMapping];
		[responseMapping addAttributeMappingsFromArray:@[ @"departures", @"destinations"]];
		self.objectMapping = responseMapping;
	}
	return self;
}

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