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

#import "InitAppResponseJsonMapper.h"
#import "Lang.h"
#import "BeanUtils.h"
#import "CocoaUtils.h"
#import <RestKit/RKObjectMappingOperationDataSource.h>
#import "LoginResponseJsonMapper.h"

@implementation InitAppResponseJsonMapper

- (instancetype)init {
	if (self = [super init]) {
		RKObjectMapping *imageMapping = [RKObjectMapping mappingForClass:[TourSearchImage class]];
		[imageMapping addAttributeMappingsFromArray:[[BeanUtils keysForAccessibleSimplePropertyWithClass:[TourSearchImage class]] arrayByAddingObject:@"params"]];

		RKObjectMapping *appInfoMapping = [RKObjectMapping mappingForClass:[AppInfoResponse class]];
		[appInfoMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[AppInfoResponse class]]];
		[appInfoMapping addRelationshipMappingWithSourceKeyPath:@"slide_images" mapping:imageMapping];
		[appInfoMapping addRelationshipMappingWithSourceKeyPath:@"hot_destination_images" mapping:imageMapping];
		
		RKObjectMapping *loginMapping = [LoginResponseBody objectMappingForClass:[LoginResponse class]];

		RKObjectMapping *initAppMapping = [RKObjectMapping mappingForClass:[InitAppResponse class]];
		[initAppMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[InitAppResponse class]]];
		[initAppMapping addRelationshipMappingWithSourceKeyPath:@"app_info_response" mapping:appInfoMapping];
		[initAppMapping addRelationshipMappingWithSourceKeyPath:@"login_response" mapping:loginMapping];

		self.objectMapping = initAppMapping;
	}
	return self;
}

#pragma mark - Public

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