//
//  InviteUsersRequest.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 "InviteUsersRequest.h"
#import "SmartFox2XClient.h"
#import "User.h"
#import "Buddy.h"

NSString * const InviteUsersRequest_KEY_USER = @"u";
NSString * const InviteUsersRequest_KEY_USER_ID = @"ui";
NSString * const InviteUsersRequest_KEY_INVITATION_ID = @"ii";
NSString * const InviteUsersRequest_KEY_TIME = @"t";
NSString * const InviteUsersRequest_KEY_PARAMS = @"p";
NSString * const InviteUsersRequest_KEY_INVITEE_ID = @"ee";
NSString * const InviteUsersRequest_KEY_INVITED_USERS = @"iu";
NSString * const InviteUsersRequest_KEY_REPLY_ID = @"ri";
NSInteger const InviteUsersRequest_MAX_INVITATIONS_FROM_CLIENT_SIDE = 8;
NSInteger const InviteUsersRequest_MIN_EXPIRY_TIME = 5;
NSInteger const InviteUsersRequest_MAX_EXPIRY_TIME = 300;

@implementation InviteUsersRequest

@synthesize invitedUsers = _invitedUsers;
@synthesize secondsForAnswer = _secondsForAnswer;
@synthesize params = _params;


-(id)initWithInvitedUsers:(NSArray *)invitedUsers secondsForAnswer:(NSInteger)secondsForAnswer params:(id <ISFSObject>)params {
	if ((self = [super initWithId:BaseRequest_InviteUser])) {
		self.invitedUsers = invitedUsers;
		self.secondsForAnswer = secondsForAnswer;
		self.params = params;
	}
	
	return self;
}

+(id)requestWithInvitedUsers:(NSArray *)invitedUsers secondsForAnswer:(NSInteger)secondsForAnswer params:(id <ISFSObject>)params {
	//InviteUsersRequest *req = [[InviteUsersRequest alloc] initWithInvitedUsers:invitedUsers secondsForAnswer:secondsForAnswer params:nil];
    InviteUsersRequest *req = [[InviteUsersRequest alloc] initWithInvitedUsers:invitedUsers secondsForAnswer:secondsForAnswer params:params];
	return req;
}

-(void)validate:(SmartFox2XClient *)sfs {
	
	NSMutableArray *errors = [NSMutableArray array];
	
	if (_invitedUsers == nil || [_invitedUsers count] < 1) {
		[errors addObject:@"No invitation(s) to send"];
	}
	
	if ([_invitedUsers count] > InviteUsersRequest_MAX_INVITATIONS_FROM_CLIENT_SIDE) {
		[errors addObject:[NSString stringWithFormat:@"Too many invitations. Max allowed from client side is: %ld", (long)InviteUsersRequest_MAX_INVITATIONS_FROM_CLIENT_SIDE]];
	}
	
	if (_secondsForAnswer < 5 || _secondsForAnswer > 300) {
		[errors addObject:[NSString stringWithFormat:@"SecondsForAnswer value is out of range (%ld-%ld)", (long)InviteUsersRequest_MIN_EXPIRY_TIME, (long)InviteUsersRequest_MAX_EXPIRY_TIME]];
	}
	
	if ([errors count] > 0) {
		@throw [SFSValidationException validationExceptionWithReason:@"InvitationReply request error" errors:errors];
	}
}


-(void)execute:(SmartFox2XClient *)sfs{
	
	NSMutableArray *invitedUserIds = [NSMutableArray array];
	
	for (id item in _invitedUsers) {
		if ([item conformsToProtocol:@protocol(User)] || [item conformsToProtocol:@protocol(Buddy)]) {
			if ([item isEqual:sfs.mySelf]) {
				continue;
			}
			[invitedUserIds addObject:[NSNumber numberWithInt:(int)[(id <User>)item id]]];
		}
	}
	
	[self.sfso putIntArray:InviteUsersRequest_KEY_INVITED_USERS value:invitedUserIds];
	[self.sfso putShort:InviteUsersRequest_KEY_TIME value:_secondsForAnswer];
	if (_params != nil) {
		[self.sfso putSFSObject:InviteUsersRequest_KEY_PARAMS value:_params];
	}
	
}


@end
