//
// QuickScanTicketJsonMapper.m
// Project
//
// Created by haibara on 2/18/16.
//
//

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

@implementation QuickScanTicketResponseJsonMapper

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

		RKObjectMapping *quickScanTicketResponseMapping = [RKObjectMapping mappingForClass:[QuickScanTicketResponse class]];
		[quickScanTicketResponseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[QuickScanTicketResponse class]]];
		[quickScanTicketResponseMapping addRelationshipMappingWithSourceKeyPath:@"sold_stock" mapping:soldStockMapping];

		self.objectMapping = quickScanTicketResponseMapping;
	}
	return self;
}

#pragma mark - Public

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