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

NSString * const SpectatorToPlayerRequest_KEY_ROOM_ID = @"r";
NSString * const SpectatorToPlayerRequest_KEY_USER_ID = @"u";
NSString * const SpectatorToPlayerRequest_KEY_PLAYER_ID = @"p";

@implementation SpectatorToPlayerRequest

@synthesize room = _room;



-(id)initWithTargetRoom:(id <Room>)targetRoom {
	
	if ((self = [super initWithId:BaseRequest_SpectatorToPlayer])) {
		self.room = targetRoom;
	}
	
	return self;
}

+(id)requestWithTargetRoom:(id <Room>)targetRoom {
	SpectatorToPlayerRequest *req = [[SpectatorToPlayerRequest alloc] initWithTargetRoom:targetRoom];
	return req;
}

+(id)request{
	SpectatorToPlayerRequest *req = [[SpectatorToPlayerRequest alloc] initWithTargetRoom: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) {
		self.room = sfs.lastJoinedRoom;
	}
	
	[_sfso putInt:SpectatorToPlayerRequest_KEY_ROOM_ID value:[_room id]];
	
}

@end
