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

NSString * const PlayerToSpectatorRequest_KEY_ROOM_ID = @"r";
NSString * const PlayerToSpectatorRequest_KEY_USER_ID = @"u";

@implementation PlayerToSpectatorRequest

@synthesize room = _room;


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

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

+(id)request {
	PlayerToSpectatorRequest *req = [[PlayerToSpectatorRequest 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:@"PlayerToSpectator request error" errors:errors];
	}
}


-(void)execute:(SmartFox2XClient *)sfs{
	
	if (_room == nil) {
		self.room = sfs.lastJoinedRoom;
	}
	
	[_sfso putInt:PlayerToSpectatorRequest_KEY_ROOM_ID value:[_room id]];
}

@end
