//
//  LeaveRoomRequest.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 "LeaveRoomRequest.h"
#import "SmartFox2XClient.h"

NSString * const LeaveRoomRequest_KEY_ROOM_ID = @"r";

@implementation LeaveRoomRequest



-(id)initWithRoom:(id <Room>)room {
	
	
	
	if ((self = [super initWithId:BaseRequest_LeaveRoom])) {
		
		_room = room;
	
	}
	
	return self;
	
}

+(id)requestWithRoom:(id <Room>)room {
	LeaveRoomRequest *req = [[LeaveRoomRequest alloc] initWithRoom:room];
	return req;
}

+(id)request {
	LeaveRoomRequest *req = [[LeaveRoomRequest alloc] initWithRoom:nil];
	return req;
}

-(void)validate:(SmartFox2XClient *)sfs {
	
	NSMutableArray *errors = [NSMutableArray array];
	
	if ([sfs.joinedRooms count] < 1) {
		[errors addObject:@"You are not joined in any rooms"];
	}
	
	if ([errors count] > 0) {
		@throw [SFSValidationException validationExceptionWithReason:@"LeaveRoom request error" errors:errors];
	}
}


-(void)execute:(SmartFox2XClient *)sfs{
	
	if (_room != nil) {
		[_sfso putInt:LeaveRoomRequest_KEY_ROOM_ID value:[_room id]];
	}
}


@end
