//
// BookingReportDetailResponseJsonMapper.m
// Project
//
// Created by Ziv on 2017/4/12.
//
//

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

@implementation BookingReportDetailResponseJsonMapper

- (instancetype)initWithIsDBDSearch:(BOOL)isDBDSearch{
    if (self = [super init]) {
        RKObjectMapping *destinationMapping = [[RKObjectMapping alloc] initWithClass:[Destination class]];

        RKObjectMapping *stationMapping = [[RKObjectMapping alloc] initWithClass:[BusStation class]];
        RKObjectMapping *scheduleStopMapping = [[RKObjectMapping alloc] initWithClass:[ScheduleStop class]];
        [scheduleStopMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[ScheduleStop class]]];
        if (!isDBDSearch) {
            //sbd
            [destinationMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Destination class]] retainEntries:@{@"cityName" : @"show_name"}]];
            [stationMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStation class]] retainEntries:@{@"stationName" : @"name"}]];
            [scheduleStopMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"busstation" withMapping:stationMapping]];
            [scheduleStopMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"city" withMapping:destinationMapping]];
        }else{
            //dbd
            [destinationMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Destination class]] retainEntries:@{@"folderId" : @"folder_id", @"name" : @"show_name"}]];
            [stationMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStation class]]];
            [scheduleStopMapping addRelationshipMappingWithSourceKeyPath:@"city" mapping:destinationMapping];
            [scheduleStopMapping addRelationshipMappingWithSourceKeyPath:@"busstation" mapping:stationMapping];
        }

    
        RKObjectMapping *detailMapping = [[RKObjectMapping alloc] initWithClass:[BookingReportDetailBean class]];
        [detailMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BookingReportDetailBean class]]];
        [detailMapping addRelationshipMappingWithSourceKeyPath:@"departureScheduleStop" mapping:scheduleStopMapping];
        [detailMapping addRelationshipMappingWithSourceKeyPath:@"arrivalScheduleStop" mapping:scheduleStopMapping];
        
        RKObjectMapping *summaryMapping = [[RKObjectMapping alloc] initWithClass:[BookingReportSummaryBean class]];
        [summaryMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BookingReportSummaryBean class]]];
        [summaryMapping addRelationshipMappingWithSourceKeyPath:@"bookingReportDetailList" mapping:detailMapping];
        [summaryMapping addRelationshipMappingWithSourceKeyPath:@"defaultBookingReportDetail" mapping:detailMapping];
        
        RKObjectMapping *responseMapping = [[RKObjectMapping alloc] initWithClass:[BookingReportDetailResponse class]];
        [responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BookingReportDetailResponse class]]];
        [responseMapping addRelationshipMappingWithSourceKeyPath:@"bookingReportSummaryList" mapping:summaryMapping];
        
        self.objectMapping = responseMapping;
    }
    return self;
}

#pragma mark - Public

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