//
// JGotoWS.m
// IvyMedia
//
// Created by paul on 8/26/14.
//
//
#import "JGotoWS.h"
#import "SDK.h"
#import "BeanUtils.h"
#import "CocoaUtils.h"
#import <BlocksKit/BlocksKit.h>
#import <RestKit/RKObjectMappingOperationDataSource.h>
#import <XMLDictionary/XMLDictionary.h>
#import "Utility.h"
#import "Vars.h"

// @implementation RKPropertyMapping (Item)
//
// - (void)setItemName:(NSString *)itemName {
// [self bk_associateValue:itemName withKey:@selector(itemName)];
// }
//
// - (NSString *)itemName {
// return [self bk_associatedValueForKey:@selector(itemName)];
// }
//
// @end

@implementation LoginResponseBody

#pragma mark - Public

+ (RKObjectMapping *)objectMappingForClass:(Class)class {
	RKObjectMapping *addressMapping = [RKObjectMapping mappingForClass:[Address class]];
	[addressMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Address class]] retainEntries:@{ @"addressId" : @"aid", @"streetAdditional" : @"street2", @"otherState" : @"other_state", @"firstName" : @"first_name", @"lastName" : @"last_name", @"altPhone" : @"phone2" }]];


	RKObjectMapping *creditMapping = [RKObjectMapping mappingForClass:[CreditCard class]];
	[creditMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[CreditCard class]] retainEntries:@{ @"creditCardId" : @"ccid", @"holderName" : @"name_on_card", @"cardType" : @"card_type", @"cardNumber" : @"card_number", @"expireYear" : @"expire_year", @"expireMonth" : @"expire_month", @"securityCode" : @"x_card_code" }]];
	[creditMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"billingAddress" toKeyPath:@"address" withMapping:addressMapping]];

	RKObjectMapping *vendorMapping = [RKObjectMapping mappingForClass:[Vendor class]];
	[vendorMapping addAttributeMappingsFromDictionary:[CocoaUtils dictionaryForKeys:[BeanUtils keysForAccessibleSimplePropertyWithClass:[Vendor class]] retainEntries:@{ @"userId" : @"id", @"userName" : @"uid", @"blockStatus" : @"block_status"}]];
	[vendorMapping addRelationshipMappingWithSourceKeyPath:@"contactInfos" mapping:addressMapping];
	[vendorMapping addRelationshipMappingWithSourceKeyPath:@"creditCards" mapping:creditMapping];

	RKObjectMapping *loginMapping = [RKObjectMapping mappingForClass:class];
	[loginMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:class]];
	[loginMapping addRelationshipMappingWithSourceKeyPath:@"user" mapping:vendorMapping];

	return loginMapping;
}

@end

@implementation JGotoWS

- (instancetype)init {
	if (self = [super init]) {
		self.apiUid = defaultAPIID;
		self.apiPassword = defaultAPIPassword;
		self.domainId = [Vars clientDomainId];
		self.cookie_string = [Vars appDelegate].loginUser.cookie_string;
	}
	return self;
}

#pragma mark - Protected

- (NSString *)address {
	return [Utility perlCgiBin:@"restful"];
}

- (NSString *)name {
	NSString *className = NSStringFromClass(self.class);
	return [className stringByReplacingOccurrencesOfString:@"[A-Z]" withString:@"_$0" options:NSRegularExpressionSearch range:NSMakeRange(1, className.length - 1)].lowercaseString;
}

#pragma mark - Public

- (NSString *)innerXML {
	return [CocoaUtils xmlStringWithProperties:[BeanUtils describeAccessibleSimple:self]];
}

@end

@implementation JGotoWSResponse
@end

@implementation Login

- (instancetype)init {
	if (self = [super init]) {
		self.apiVersion = 12;
	}
	return self;
}

@end

@implementation LoginResponse
@end

@implementation DeviceInfo

#pragma mark - Public

- (NSString *)innerXML {
	return [CocoaUtils xmlStringWithNonZeroProperties:[BeanUtils describeAccessibleSimple:self]];
}

@end

@implementation AppInfo
#pragma mark - Public

- (NSString *)innerXML {
	return [CocoaUtils xmlStringWithProperties:[BeanUtils describeAccessibleSimple:self]];
}

@end

@implementation LoginInfo

#pragma mark - Public

- (NSString *)innerXML {
	return [CocoaUtils xmlStringWithNonZeroProperties:[BeanUtils describeAccessibleSimple:self]];
}

@end

@implementation InitApp

- (instancetype)init {
	self = [super init];
	if (self) {
		self.apiVersion = 13;
		self.app_info = [[AppInfo alloc] init];
		self.device_info = [[DeviceInfo alloc] init];
		self.login_info = [[LoginInfo alloc] init];
	}
	return self;
}

#pragma mark - Public

- (NSString *)innerXML {
	NSMutableString *string = [NSMutableString string];
	[string im_appendString:[CocoaUtils xmlStringWithProperties:[BeanUtils describeAccessibleSimple:self]]];
	if (self.device_info) {
		[string im_appendString:[CocoaUtils xmlStringWithValue:[self.device_info innerXML] forElement:@"device_info"]];
	}
	if (self.app_info) {
		[string im_appendString:[CocoaUtils xmlStringWithValue:[self.app_info innerXML] forElement:@"app_info"]];
	}
	if (self.login_info) {
		[string im_appendString:[CocoaUtils xmlStringWithValue:[self.login_info innerXML] forElement:@"login_info"]];
	}
	return [NSString stringWithString:string];
}

@end

@implementation InitAppResponse
@end

// @interface InitAppJsonMapper ()
// @property (nonatomic, strong) RKObjectMapping *objectMapping;
// @end
//
// @implementation InitAppJsonMapper
//
// - (instancetype)init {
// if (self = [super init]) {
// RKObjectMapping *appInfoMapping = [RKObjectMapping mappingForClass:[AppInfo class]];
// [appInfoMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[AppInfo class]]];
// RKObjectMapping *loginInfoMapping = [RKObjectMapping mappingForClass:[LoginInfo class]];
// [loginInfoMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[LoginInfo class]]];
// RKObjectMapping *initAppMapping = [RKObjectMapping mappingForClass:[InitApp class]];
// [initAppMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[InitApp class] parentsUtil:[JGotoWS class]]];
// [initAppMapping addRelationshipMappingWithSourceKeyPath:@"app_info" mapping:appInfoMapping];
// [initAppMapping addRelationshipMappingWithSourceKeyPath:@"login_info" mapping:loginInfoMapping];
// self.objectMapping = initAppMapping;
// }
// return self;
// }
//
// - (NSString *)serializeWithObject:(id)object {
// RKObjectMappingOperationDataSource *dataSource = [RKObjectMappingOperationDataSource new];
// NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
// RKMappingOperation *operation = [[RKMappingOperation alloc] initWithSourceObject:object destinationObject:parameters mapping:[self.objectMapping inverseMapping]];
// operation.dataSource = dataSource;
// [operation start];
//
// [Utility fillJSONObject:parameters objectMapping:self.objectMapping];
//
// return [CocoaUtils stringWithElement:@"root" byXmlString:[parameters XMLString]];
// }
//
// @end

@implementation AddRequest
@end

@implementation AddRequestResponse
@end

@implementation PurchaseReport

- (instancetype)init {
	if (self = [super init]) {
		self.apiVersion = 5;
	}
	return self;
}

@end

@implementation PurchaseRecordResponse
@end



