//
// PrefetchLoginPostDataResponseJsonMapper.m
// Project
//
// Created by haibara on 5/10/17.
//
//

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

@implementation PrefetchLoginPostDataResponseJsonMapper

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

		RKObjectMapping *busStopMapping = [RKObjectMapping mappingForClass:[BusStop class]];
		[busStopMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStop class]]];

		RKObjectMapping *productMapping = [RKObjectMapping mappingForClass:[Product class]];
		[productMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Product class]] retainEntries:@{ @"listing_hour" : @"listingHour", @"listing_departure_time" : @"listingDepartureTime" }]];
		[productMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"bus_stops" toKeyPath:@"stops" withMapping:busStopMapping]];

		RKObjectMapping *destinationMapping = [RKObjectMapping mappingForClass:[Destination class]];
		[destinationMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Destination class]]];

		RKObjectMapping *routeMapping = [RKObjectMapping mappingForClass:[BusRoute class]];
		[routeMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusRoute class]]];

		RKObjectMapping *busStationMapping = [RKObjectMapping mappingForClass:[BusStation class]];
		[busStationMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStation class]]];

		[bodyMapping addRelationshipMappingWithSourceKeyPath:@"products" mapping:productMapping];
		[bodyMapping addRelationshipMappingWithSourceKeyPath:@"destinations" mapping:destinationMapping];
		[bodyMapping addRelationshipMappingWithSourceKeyPath:@"bus_routes" mapping:routeMapping];
		[bodyMapping addRelationshipMappingWithSourceKeyPath:@"bus_stations" mapping:busStationMapping];

		self.objectMapping = bodyMapping;
	}
	return self;
}

#pragma mark - Public

- (NSError *)parseWithJSONObject:(id)jsonObject {
	self.response = [[PrefetchLoginPostDataResponse 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]) {
		NSArray *products = self.response.products;
		NSArray *destinations = self.response.destinations;
		NSArray *busRoutes = self.response.bus_routes;
		NSArray *busStations = self.response.bus_stations;
		NSDictionary *destinationHash = [NSDictionary dictionaryWithObjects:destinations forKeys:[destinations valueForKeyPath:@"folder_id"]];
		for (BusRoute *busRoute in busRoutes) {
			busRoute.departure_object = destinationHash[@(busRoute.departure_city)];
			busRoute.arrival_object = destinationHash[@(busRoute.arrival_city)];
		}
		for (BusStation *busStation in busStations) {
			busStation.destination = destinationHash[@(busStation.station_city)];
		}
		NSDictionary *busStationHash = [NSDictionary dictionaryWithObjects:busStations forKeys:[busStations valueForKeyPath:@"aid"]];
		for (Product *product in products) {
			for (BusStop *busStop in product.stops) {
				busStop.station = busStationHash[@(busStop.bss_id)];
			}
		}
		[Vars appDelegate].loginUser.products = products;
		[Vars appDelegate].loginUser.busRoutes = busRoutes;
	}
	return error;
}

@end
