//
// TrackBusStatusDetailsResponseJsonMapper.m
// Project
//
// Created by Ziv on 2017/5/17.
//
//

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

@implementation BusStatusDetailResponseJsonMapper

- (instancetype)init {
	if (self = [super init]) {
		RKObjectMapping *destinationMapping = [[RKObjectMapping alloc] initWithClass:[Destination class]];
		[destinationMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Destination class]] retainEntries:@{@"cityName" : @"show_name"}]];

		RKObjectMapping *stationMapping = [[RKObjectMapping alloc] initWithClass:[BusStation class]];
		[stationMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStation class]] retainEntries:@{@"scheduleStopName" : @"name"}]];
		[stationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"destination" withMapping:destinationMapping]];

		RKObjectMapping *stopMapping = [[RKObjectMapping alloc] initWithClass:[ScheduleStop class]];
		[stopMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[ScheduleStop class]] retainEntries:@{@"scheduleStopId" : @"id", @"scheduleDepartureTime" : @"leavingTime", @"scheduleArrivalTime" : @"arrivalTime"}]];
		[stopMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"busstation" withMapping:stationMapping]];

		RKObjectMapping *statusMapping = [[RKObjectMapping alloc] initWithClass:[BusStatusTimeBean class]];
		[statusMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStatusTimeBean class]]];

		RKObjectMapping *driverMapping = [[RKObjectMapping alloc] initWithClass:[DriverBean class]];
		[driverMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[DriverBean class]]];

		RKObjectMapping *detailMapping = [[RKObjectMapping alloc] initWithClass:[BusStatusDetailBean class]];
		[detailMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStatusDetailBean class]]];
		[detailMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"scheduleStop" withMapping:stopMapping]];
		[detailMapping addRelationshipMappingWithSourceKeyPath:@"departureStopStatus" mapping:statusMapping];
		[detailMapping addRelationshipMappingWithSourceKeyPath:@"arrivalStopStatus" mapping:statusMapping];
		[detailMapping addRelationshipMappingWithSourceKeyPath:@"assignedDrivers" mapping:driverMapping];

		RKObjectMapping *locationMapping = [[RKObjectMapping alloc] initWithClass:[BusStatusLocationBean class]];
		[locationMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStatusLocationBean class]]];

		RKObjectMapping *responseMapping = [[RKObjectMapping alloc] initWithClass:[BusStatusDetailResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStatusDetailResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"resultList" mapping:detailMapping];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"currentLocation" mapping:locationMapping];

		self.objectMapping = responseMapping;
	}
	return self;
}

- (NSError *)parseWithJSONObject:(id)jsonObject {
	self.response = [[BusStatusDetailResponse 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;
	if ([mappingOperation performMapping:&error]) {
		for (BusStatusDetailBean *bean in self.response.resultList) {
			bean.scheduleStop.isPickUp = bean.departureStopStatus;
			bean.scheduleStop.isDropOff = bean.arrivalStopStatus;
		}
	}
	return error;
}

@end
