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

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

@implementation BookGoodsResponseJsonMapper

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

		RKObjectMapping *unifideorder2PrepayMapping = [RKObjectMapping mappingForClass:[Unifiedorder2Prepay class]];
		[unifideorder2PrepayMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Unifiedorder2Prepay class]]];

		RKObjectMapping *wechatpayMapping = [RKObjectMapping mappingForClass:[_WechatpayPayment class]];
		[wechatpayMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[_WechatpayPayment class]]];
		[wechatpayMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"unifiedorder2Pay" toKeyPath:@"unifiedorder_prepay" withMapping:unifideorder2PrepayMapping]];

		RKObjectMapping *alipayMapping = [RKObjectMapping mappingForClass:[_AlipayPayment class]];
		[alipayMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[_AlipayPayment class]]];

		RKObjectMapping *uppayMapping = [RKObjectMapping mappingForClass:[_UnionpayPayment class]];
		[uppayMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[_UnionpayPayment class]]];

		RKObjectMapping *eTicketMapping = [RKObjectMapping mappingForClass:[_ETicket class]];
		[eTicketMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[_ETicket class]]];

		RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[BookGoodsResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BookGoodsResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"paypal_payment" mapping:paypalMapping];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"wechatpay_payment" mapping:wechatpayMapping];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"alipay_payment" mapping:alipayMapping];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"unionpay_payment" mapping:uppayMapping];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"order_summary" mapping:eTicketMapping];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"etickets" mapping:eTicketMapping];

		self.objectMapping = responseMapping;
	}
	return self;
}

#pragma mark - Public

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