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

NSString * const FindRoomsRequest_KEY_EXPRESSION = @"e";
NSString * const FindRoomsRequest_KEY_GROUP = @"g";
NSString * const FindRoomsRequest_KEY_LIMIT = @"l";
NSString * const FindRoomsRequest_KEY_FILTERED_ROOMS = @"fr";

@implementation FindRoomsRequest


-(id)initWithExpr:(MatchExpression *)expr groupId:(NSString *)groupId limit:(NSInteger)limit {
	
	if ((self = [super initWithId:BaseRequest_FindRooms])) {
	
		_matchExpr = expr;
		_groupId = groupId;
		_limit = limit;
		
	}
	
	return self;
}

+(id)requestWithExpr:(MatchExpression *)expr groupId:(NSString *)groupId limit:(NSInteger)limit {
	FindRoomsRequest *req = [[FindRoomsRequest alloc] initWithExpr:expr groupId:groupId limit:limit];
	return req;
}

+(id)requestWithExpr:(MatchExpression *)expr {
	FindRoomsRequest *req = [[FindRoomsRequest alloc] initWithExpr:expr groupId:nil limit:0];
	return req;
}

-(void)validate:(SmartFox2XClient *)sfs {
	
	NSMutableArray *errors = [NSMutableArray array];
	
	if(_matchExpr == nil) {
		[errors addObject:@"Missing Match Expression"];
	}
	
	
	if ([errors count] > 0) {
		@throw [SFSValidationException validationExceptionWithReason:@"FindRooms request error" errors:errors];
	}
}


-(void)execute:(SmartFox2XClient *)sfs{
	[_sfso putSFSArray:FindRoomsRequest_KEY_EXPRESSION value:[_matchExpr toSFSArray]];
	if (_groupId != nil) {
		[_sfso putUtfString:FindRoomsRequest_KEY_GROUP value:_groupId];
	}
	
	if (_limit > 0) {
		[_sfso putShort:FindRoomsRequest_KEY_LIMIT value:_limit];
	}
}



@end
