//
// SearchBusXMLResolver.m
// GoToBus
//
// Created by iphone development on 6/27/13.
//
//

#import "SearchBusJSONMapper.h"
#import "Lang.h"
#import "BeanUtils.h"
#import "CocoaUtils.h"
#import <RestKit/RestKit.h>
#import <RestKit/RKObjectMappingOperationDataSource.h>
#import "WebService.h"
#import "BusSearchData.h"

@interface SearchBusJSONMapper ()
@property (nonatomic, strong) RKObjectMapping *objectMapping;
@end

@implementation SearchBusJSONMapper

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

		RKObjectMapping *busStopMapping = [RKObjectMapping mappingForClass:[BusStop class]];
		[busStopMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusStop class]] retainEntries:@{ @"value" : @"option_value", @"time" : @"ss_time", @"nextDay" : @"next_day", @"address" : @"address_desc" }]];
		[busStopMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"station" withMapping:busStationMapping]];

		RKObjectMapping *searchCityStopsMapping = [RKObjectMapping mappingForClass:[BusSearchLocationTuple class]];
		[searchCityStopsMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusSearchLocationTuple class]]];
		[searchCityStopsMapping addRelationshipMappingWithSourceKeyPath:@"stationList" mapping:busStopMapping];

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

		RKObjectMapping *flexiPolicyMapping = [[RKObjectMapping alloc] initWithClass:[FlexiReschedulePolicy class]];
		[flexiPolicyMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[FlexiReschedulePolicy class]]];

		RKObjectMapping *searchDataMapping = [RKObjectMapping mappingForClass:[BusSearchData class]];
        [searchDataMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusSearchData class]] retainEntries:@{ @"scheduleCode" : @"scheduleId" }]];
		[searchDataMapping addRelationshipMappingWithSourceKeyPath:@"arrivals" mapping:searchCityStopsMapping];
		[searchDataMapping addRelationshipMappingWithSourceKeyPath:@"departs" mapping:searchCityStopsMapping];
		[searchDataMapping addRelationshipMappingWithSourceKeyPath:@"peopleType" mapping:searchPeopleTypeMapping];
		[searchDataMapping addRelationshipMappingWithSourceKeyPath:@"flexiReschedulePolicy" mapping:flexiPolicyMapping];

		RKObjectMapping *specialRemakMapping = [RKObjectMapping mappingForClass:[BusSearchRemark class]];
		[specialRemakMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[BusSearchRemark class]]];

		RKObjectMapping *listBeanMapping = [RKObjectMapping mappingForClass:[SearchBusListBean class]];
		[listBeanMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[SearchBusListBean class]]];
		[listBeanMapping addRelationshipMappingWithSourceKeyPath:@"route" mapping:searchDataMapping];
		[listBeanMapping addRelationshipMappingWithSourceKeyPath:@"specialRemark" mapping:specialRemakMapping];


		RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[SearchBusResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[SearchBusResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"departureList" mapping:listBeanMapping];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"arrivalList" mapping:listBeanMapping];

		self.objectMapping = responseMapping;
	}
	return self;
}

- (NSError *)parseJSONObject:(id)jsonObject {
	self.response = [[SearchBusResponse 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]) {
		NSDictionary *departureRemarkHash = [NSDictionary dictionaryWithObjects:[self.response.departureList.specialRemark valueForKeyPath:@"policy"] forKeys:[self.response.departureList.specialRemark valueForKeyPath:@"id"]];
		NSDictionary *arrivalRemarkHash = [NSDictionary dictionaryWithObjects:[self.response.arrivalList.specialRemark valueForKeyPath:@"policy"] forKeys:[self.response.arrivalList.specialRemark valueForKeyPath:@"id"]];
		for (BusSearchData *busSearchData in self.response.departureList.route) {
			busSearchData.specialRemark = departureRemarkHash[@(busSearchData.vendorId)];
			for (BusSearchLocationTuple *tuple in busSearchData.arrivals) {
				for (BusStop *stop in tuple.stationList) {
					stop.is_arrival = YES;
				}
			}
		}
		for (BusSearchData *busSearchData in self.response.arrivalList.route) {
			busSearchData.specialRemark = arrivalRemarkHash[@(busSearchData.vendorId)];
			for (BusSearchLocationTuple *tuple in busSearchData.arrivals) {
				for (BusStop *stop in tuple.stationList) {
					stop.is_arrival = YES;
				}
			}
		}
	}
	return error;
}

@end
