//
//  RemoveBuddyRequest.m
//  SFS2X
//
//  Original development by Infosfer Game Technologies Ltd. | http://www.infosfer.com.
//
//  Maintained and developed by A51 Integrated.
//  Copyright 2012 A51 Integrated | http://a51integrated.com. All rights reserved.
//

#import "RemoveBuddyRequest.h"
#import "SmartFox2XClient.h"

NSString * const RemoveBuddyRequest_KEY_BUDDY_NAME = @"bn";

@implementation RemoveBuddyRequest

@synthesize name = _name;


-(id)initWithBuddyName:(NSString *)buddyName {
	if ((self = [super initWithId:BaseRequest_RemoveBuddy])) {
		self.name = buddyName;
	}
	
	return self;
}

+(id)requestWithBuddyName:(NSString *)buddyName {
	RemoveBuddyRequest *req = [[RemoveBuddyRequest alloc] initWithBuddyName:buddyName];
	return req;
}

-(void)validate:(SmartFox2XClient *)sfs {
	
	NSMutableArray *errors = [NSMutableArray array];
	
	if (![sfs.buddyManager isInited]) {
		[errors addObject:@"BuddyList is not inited. Please send an InitBuddyRequest first."];
	}
	
	if ([sfs.buddyManager myOnlineState] == NO) {
		[errors addObject:@"Can't remove buddy while offline"];
	}
	
	if (![sfs.buddyManager containsBuddy:_name]) {
		[errors addObject:[NSString stringWithFormat:@"Can't remove buddy, it's not in your list: %@", _name]];
	}
	
	if ([errors count] > 0) {
		@throw [SFSValidationException validationExceptionWithReason:@"BuddyList request error" errors:errors];
	}
}


-(void)execute:(SmartFox2XClient *)sfs{
	
	[_sfso putUtfString:RemoveBuddyRequest_KEY_BUDDY_NAME value:_name];
}


@end
