//
// GetReschedulePolicyResponseJsonMapper.m
// LiveProject
//
// Created by Ziv on 15/9/21.
//
//

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

@implementation GetReschedulePolicyResponseJsonMapper

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

		RKObjectMapping *sectionMapping = [RKObjectMapping mappingForClass:[GetReschedulePolicyResponseSection class]];
		[sectionMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetReschedulePolicyResponseSection class]]];
		[sectionMapping addRelationshipMappingWithSourceKeyPath:@"titleLabel" mapping:labelMapping];
		[sectionMapping addRelationshipMappingWithSourceKeyPath:@"detailLabels" mapping:labelMapping];

		RKObjectMapping *policyMapping = [RKObjectMapping mappingForClass:[GetReschedulePolicyResponse class]];
		[policyMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetReschedulePolicyResponse class]]];
		[policyMapping addRelationshipMappingWithSourceKeyPath:@"sections" mapping:sectionMapping];

		self.objectMapping = policyMapping;
	}
	return self;
}

#pragma mark - Public

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