//
//  QuickJoinGameRequest.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 "QuickJoinGameRequest.h"
#import "MatchExpression.h"

NSString * const QuickJoinGameRequest_KEY_ROOM_LIST = @"rl";
NSString * const QuickJoinGameRequest_KEY_GROUP_LIST = @"gl";
NSString * const QuickJoinGameRequest_KEY_ROOM_TO_LEAVE = @"tl";
NSString * const QuickJoinGameRequest_KEY_MATCH_EXPRESSION = @"me";

@implementation QuickJoinGameRequest

static NSInteger MAX_ROOMS = 32;

@synthesize whereToSearch = _whereToSearch;
@synthesize matchExpression = _matchExpression;
@synthesize roomToLeave = _roomToLeave;


-(id)initWithMatchExpression:(MatchExpression *)matchExpression whereToSearch:(NSArray *)whereToSearch roomToLeave:(id <Room>)roomToLeave {
	if ((self = [super initWithId:BaseRequest_QuickJoinGame])) {
		
		self.whereToSearch = whereToSearch;
		self.matchExpression = matchExpression;
		self.roomToLeave = roomToLeave;
	}
	
	return self;
}

+(id)requestWithMatchExpression:(MatchExpression *)matchExpression whereToSearch:(NSArray *)whereToSearch roomToLeave:(id <Room>)roomToLeave {
	QuickJoinGameRequest *req = [[QuickJoinGameRequest alloc] initWithMatchExpression:matchExpression whereToSearch:whereToSearch roomToLeave:roomToLeave];
	return req;
}

+(id)requestWithMatchExpression:(MatchExpression *)matchExpression whereToSearch:(NSArray *)whereToSearch {
	QuickJoinGameRequest *req = [[QuickJoinGameRequest alloc] initWithMatchExpression:matchExpression whereToSearch:whereToSearch roomToLeave:nil];
	return req;
}

-(void)validate:(SmartFox2XClient *)sfs {
	
	NSMutableArray *errors = [NSMutableArray array];
	
	
	if (_whereToSearch == nil || [_whereToSearch count] < 1) {
		[errors addObject:@"Missing whereToSearch parameter"];
	}
	
	if ([_whereToSearch count] > MAX_ROOMS) {
		[errors addObject:[NSString stringWithFormat:@"Too many Rooms specified in the whereToSearch parameter. Client limit is: %ld", (long)MAX_ROOMS]];
	}
	
	if ([errors count] > 0) {
		@throw [SFSValidationException validationExceptionWithReason:@"QuickJoinGame request error" errors:errors];
	}
}


-(void)execute:(SmartFox2XClient *)sfs{
	
	if ([[_whereToSearch objectAtIndex:0] isKindOfClass:[NSString class]]) {
		[_sfso putUtfStringArray:QuickJoinGameRequest_KEY_GROUP_LIST value:_whereToSearch];
	}
	else if([[_whereToSearch objectAtIndex:0] conformsToProtocol:@protocol(Room)]){
		
		NSMutableArray *roomIds = [NSMutableArray array];
		
		for (int i = 0; i < [_whereToSearch count]; i++) {
			id item = [_whereToSearch objectAtIndex:i];
			
			if ([item conformsToProtocol:@protocol(Room)]) {
				[roomIds addObject:[NSNumber numberWithInt:(int)[(id <Room>)item id]]];
			}
		}
		
		[_sfso putIntArray:QuickJoinGameRequest_KEY_ROOM_LIST value:roomIds];
		
	}
	else {
		@throw [[NSException alloc] initWithName:@"ArgumentError" reason:[NSString stringWithFormat:@"Invalid type in whereToSearch parameter: %@", [_whereToSearch objectAtIndex:0]] userInfo:nil];
	}
	
	if (_roomToLeave != nil) {
		[_sfso putInt:QuickJoinGameRequest_KEY_ROOM_TO_LEAVE value:[_roomToLeave id]];
	}
	if (_matchExpression != nil) {
		[_sfso putSFSArray:QuickJoinGameRequest_KEY_MATCH_EXPRESSION value:[_matchExpression toSFSArray]];
	}

}

@end
