//
// GetBusVendorReviewResponseJsonMapper.m
// Project
//
// Created by Ziv on 16/11/4.
//
//

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

@implementation GetBusVendorReviewsResponseJsonMapper

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

		RKObjectMapping *reviewMapping = [RKObjectMapping mappingForClass:[BusVendorReview class]];
		[reviewMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusVendorReview class]]];

		RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[GetBusVendorReviewsResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetBusVendorReviewsResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"reviews_summary" mapping:discriptionMapping];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"reviews" mapping:reviewMapping];

		self.objectMapping = responseMapping;
	}
	return self;
}

#pragma mark - Public

- (NSError *)parseWithJSONObject:(id)jsonObject {
	self.response = [[GetBusVendorReviewsResponse 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;
	if ([mappingOperation performMapping:&error]) {
		self.response.reviews_summary.image_url = [Utility securityHtmlString:self.response.reviews_summary.image_url];
	}
	return error;
}

@end
