//
//  ScanCodeJsonMapper.m
//  Project
//
//  Created by Vae on 2018/6/21.
//

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

@implementation ScanCodeJsonMapper
- (instancetype)init {
    if (self = [super init]) {
        
        
        
        RKObjectMapping *stockMapping = [[RKObjectMapping alloc] initWithClass:[SoldStock class]];
        [stockMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:@[@"scheduleName", @"blockStatus", @"cn", @"departure", @"arrival"] retainEntries:@{ @"dateStock.date" : @"departureDate", @"dateStock.stockId" : @"stock_id", @"soldToName" : @"sold_to_name", @"dateStock.vendorCheckIn" : @"vendor_checkin" }]];
        
        RKObjectMapping *responseMapping = [[RKObjectMapping alloc] initWithClass:[ScanTicketBarcodeResponse class]];
        [responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[ScanTicketBarcodeResponse class]]];
        [responseMapping addRelationshipMappingWithSourceKeyPath:@"soldStock" mapping:stockMapping];
        
        self.objectMapping = responseMapping;
    }
    return self;
}

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