//
// GetProductOptionsResponseJsonMapper.m
// LiveProject
//
// Created by Ziv on 15/9/23.
//
//

#import "GetTourOptionsResponseJsonMapper.h"
#import "Lang.h"
#import "SDK.h"
#import "BeanUtils.h"
#import "CocoaUtils.h"
#import <RestKit/RKObjectMappingOperationDataSource.h>
#import "BusStation.h"
#import "BusStop.h"

@implementation GetTourOptionsResponseJsonMapper

- (instancetype)init {
	if (self = [super init]) {
		
		RKObjectMapping *variableMapping = [RKObjectMapping mappingForClass:[variable class]];
		[variableMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[variable class]]];
		
		RKObjectMapping *formulaMapping = [RKObjectMapping mappingForClass:[Formula class]];
		[formulaMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Formula class]]];
		[formulaMapping addRelationshipMappingWithSourceKeyPath:@"variables" mapping:variableMapping];
		
		RKObjectMapping *labelEntryMapping = [RKObjectMapping mappingForClass:[LabelEntry class]];
		[labelEntryMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[LabelEntry class]]];
		[labelEntryMapping addRelationshipMappingWithSourceKeyPath:@"formulas" mapping:formulaMapping];
		
		RKObjectMapping *stationMapping = [RKObjectMapping mappingForClass:[BusStation class]];
		[stationMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStation class]]];

		RKObjectMapping *stopMapping = [RKObjectMapping mappingForClass:[BusStop class]];
		[stopMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStop class]]  retainEntries:@{ @"value" : @"option_value", @"addressDescription" : @"address_desc" }]];
		[stopMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"station" withMapping:stationMapping]];

		RKObjectMapping *cityStopMapping = [RKObjectMapping mappingForClass:[GetTourOptionsResponseLocationTuple class]];
		[cityStopMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetTourOptionsResponseLocationTuple class]]];
		[cityStopMapping addRelationshipMappingWithSourceKeyPath:@"stations" mapping:stopMapping];

		RKObjectMapping *labelMapping = [RKObjectMapping mappingForClass:[GetTourOptionsResponseLabel class]];
		[labelMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetTourOptionsResponseLabel class]] arrayByAddingObjectsFromArray:@[@"options", @"arrivalAirportOptions", @"returnAirportOptions"]] retainEntries:@{ @"name" : @"alias", @"displayName" : @"name" }]];
		[labelMapping addRelationshipMappingWithSourceKeyPath:@"departs" mapping:cityStopMapping];
		[labelMapping addRelationshipMappingWithSourceKeyPath:@"arrivals" mapping:cityStopMapping];
		[labelMapping addRelationshipMappingWithSourceKeyPath:@"entries" mapping:labelEntryMapping];

		RKObjectMapping *peopleTypeMapping = [RKObjectMapping mappingForClass:[GetProductOptionsResponsePeopleType class]];
		[peopleTypeMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetProductOptionsResponsePeopleType class]]];

		RKObjectMapping *limiteNumMapping = [RKObjectMapping mappingForClass:[GetTourOptionsResponseRoomRestriction class]];
		[limiteNumMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetTourOptionsResponseRoomRestriction class]]];

		RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[GetTourOptionsResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetTourOptionsResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"labels" mapping:labelMapping];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"peopleTypes" mapping:peopleTypeMapping];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"limitPeopleNumberOfRoom" mapping:limiteNumMapping];

		self.objectMapping = responseMapping;
	}
	return self;
}

#pragma mark - Public

- (NSError *)parseWithJSONObject:(id)jsonObject {
	self.response = [[GetTourOptionsResponse 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];
	if (!error) {
		for (GetTourOptionsResponseLabel *label in self.response.labels) {
			for (GetTourOptionsResponseLocationTuple *tuple in label.arrivals) {
				for (BusStop *stop in tuple.stations) {
					stop.is_arrival = YES;
				}
			}
			if (label.options) {
			} else if (label.arrivalAirportOptions || label.returnAirportOptions) {
				label.type = @"flight-info";
			} else if ([label.alias isEqualToString:@"passport"]) {
				label.type = @"passport";
			} else if ([label.alias hasSuffix:@"Passport"]) {
				label.type = @"distinguish_passport";
			} else if (label.isKindAsStop) {
			} else if (label.isKindOfTime){
				label.type = @"time";
			}else {
				label.type = @"input";
			}
		}
	}
	return error;
}

@end
