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

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

@implementation FinishRescheduleResponseJsonMapper

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

		RKObjectMapping *creditCardMapping = [RKObjectMapping mappingForClass:[CreditCard class]];
		[creditCardMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[CreditCard class]]];
		[creditCardMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"address" withMapping:addressMapping]];

		RKObjectMapping *refundTransactionMapping = [RKObjectMapping mappingForClass:[_RefundTransaction class]];
		[refundTransactionMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[_RefundTransaction class]]];

		RKObjectMapping *paymentMapping = [RKObjectMapping mappingForClass:[_PaymentInfo class]];
		[paymentMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[_PaymentInfo class]]];
		[paymentMapping addRelationshipMappingWithSourceKeyPath:@"credit_card" mapping:creditCardMapping];
		[paymentMapping addRelationshipMappingWithSourceKeyPath:@"refund_transactions" mapping:refundTransactionMapping];

		RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[FinishRescheduleResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[FinishRescheduleResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"paymentInfo" mapping:paymentMapping];

		self.objectMapping = responseMapping;
	}
	return self;
}

#pragma mark - Public

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