//
// TourSearchDataResponseJsonMapper.m
// Project
//
// Created by Ziv on 15/10/27.
//
//

#import "GetTourDetailsResponseJsonMapper.h"
#import "BeanUtils.h"
#import "CocoaUtils.h"
#import <RestKit/RKObjectMappingOperationDataSource.h>
#import "Parking.h"
#import "TourSearchData.h"

@implementation GetTourDetailsResponseJsonMapper

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

		RKObjectMapping *dateMapping = [RKObjectMapping mappingForClass:[GetTourDetailsResponseDailyTrip class]];
		[dateMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetTourDetailsResponseDailyTrip class]]];

		RKObjectMapping *destinationMapping = [RKObjectMapping mappingForClass:[Destination class]];
		[destinationMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Destination class]] retainEntries:@{ @"station_city_name" : @"show_name" }]];

		RKObjectMapping *stationMapping = [RKObjectMapping mappingForClass:[BusStation class]];
		[stationMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStation class]]];
		[stationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"destination" withMapping:destinationMapping]];

		RKObjectMapping *stopMapping = [RKObjectMapping mappingForClass:[BusStop class]];
		[stopMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStop class]]];
		[stopMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"station" withMapping:stationMapping]];

		RKObjectMapping *parkMapping = [RKObjectMapping mappingForClass:[Parking class]];
		[parkMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Parking class]]];

		RKObjectMapping *detailsMapping = [RKObjectMapping mappingForClass:[GetTourDetailsResponseData class]];
		[detailsMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetTourDetailsResponseData class]]];
		[detailsMapping addRelationshipMappingWithSourceKeyPath:@"dates" mapping:dateMapping];
		[detailsMapping addRelationshipMappingWithSourceKeyPath:@"bus_stops" mapping:stopMapping];
		[detailsMapping addRelationshipMappingWithSourceKeyPath:@"parking_stops" mapping:parkMapping];

		RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[GetTourDetailsResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetTourDetailsResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"product" mapping:searchDataMapping];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"details" mapping:detailsMapping];

		self.objectMapping = responseMapping;
	}
	return self;
}

#pragma mark - Public

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