//
// ProductPickViewJsonMapper.m
// LiveProject
//
// Created by Ziv on 15/9/17.
//
//

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

@implementation BookingSummaryResponseJsonMapper

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

		RKObjectMapping *vendorReportMapping = [RKObjectMapping mappingForClass:[VendorReportData class]];
		[vendorReportMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[VendorReportData class]]];
		[vendorReportMapping addRelationshipMappingWithSourceKeyPath:@"bus_status_info" mapping:busStatusMapping];

		RKObjectMapping *bookingSummaryMapping = [RKObjectMapping mappingForClass:[BookingSummaryResponse class]];
		[bookingSummaryMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BookingSummaryResponse class]]];
		[bookingSummaryMapping addRelationshipMappingWithSourceKeyPath:@"datas" mapping:vendorReportMapping];

		self.objectMapping = bookingSummaryMapping;
	}
	return self;
}

#pragma mark - Public

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