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

NSString * const JoinRoomRequest_KEY_ROOM = @"r";
NSString * const JoinRoomRequest_KEY_USER_LIST = @"ul";
NSString * const JoinRoomRequest_KEY_ROOM_NAME = @"n";
NSString * const JoinRoomRequest_KEY_ROOM_ID = @"i";
NSString * const JoinRoomRequest_KEY_PASS = @"p";
NSString * const JoinRoomRequest_KEY_ROOM_TO_LEAVE = @"rl";
NSString * const JoinRoomRequest_KEY_AS_SPECTATOR = @"sp";

@implementation JoinRoomRequest


-(id)initWithId:(id)numericIdOrName pass:(NSString *)pass roomIdToLeave:(NSNumber *)roomIdToLeave asSpect:(BOOL)asSpect {
	
	if ((self = [super initWithId:BaseRequest_JoinRoom])) {
		
		__id = -1;
		
		if ([numericIdOrName isKindOfClass:[NSString class]]) {
			_name = (NSString *)numericIdOrName;
		}
		else if ([numericIdOrName isKindOfClass:[NSNumber class]]) {
			__id = [(NSNumber *)numericIdOrName intValue];
		}
		else if ([numericIdOrName conformsToProtocol:@protocol(Room)]) {
			__id = [(id <Room>)numericIdOrName id];
		}
		
		_pass = pass;
		_roomIdToLeave = roomIdToLeave;
		_asSpectator = asSpect;
	}
	
	return self;
}

+(id)requestWithId:(id)numericIdOrName pass:(NSString *)pass roomIdToLeave:(NSNumber *)roomIdToLeave asSpect:(BOOL)asSpect {
	JoinRoomRequest *req = [[JoinRoomRequest alloc] initWithId:numericIdOrName pass:pass roomIdToLeave:roomIdToLeave asSpect:asSpect];
	return req;
}

+(id)requestWithId:(id)numericIdOrName {
	JoinRoomRequest *req = [[JoinRoomRequest alloc] initWithId:numericIdOrName pass:nil roomIdToLeave:nil asSpect:NO];
	return req;
}


-(void)validate:(SmartFox2XClient *)sfs {
	
	NSMutableArray *errors = [NSMutableArray array];
	
	if(__id < 0 && (_name == nil || [_name length] == 0)) {
		[errors addObject:@"Missing Room id or name, you should provide at least one"];
	}
	
	if ([errors count] > 0) {
		@throw [SFSValidationException validationExceptionWithReason:@"JoinRoom request error" errors:errors];
	}
}


-(void)execute:(SmartFox2XClient *)sfs{
	
	if (__id > -1) {
		[_sfso putInt:JoinRoomRequest_KEY_ROOM_ID value:__id];
	}
	else if (_name != nil) {
		[_sfso putUtfString:JoinRoomRequest_KEY_ROOM_NAME value:_name];
	}
    
	if (_pass != nil) {
		[_sfso putUtfString:JoinRoomRequest_KEY_PASS value:_pass];
	}
    
	if (_roomIdToLeave != nil) {
		[_sfso putInt:JoinRoomRequest_KEY_ROOM_TO_LEAVE value:[_roomIdToLeave intValue]];
	}
    
    /*
     if ([_roomIdToLeave intValue] < 0)
     {
     // do nothing, stay in room
     }
     else if (![[NSNull null] isEqual:_roomIdToLeave])
     {
     [_sfso putInt:JoinRoomRequest_KEY_ROOM_TO_LEAVE value:[_roomIdToLeave intValue]];
     }
     else
     {
     [_sfso putInt:JoinRoomRequest_KEY_ROOM_TO_LEAVE value:[[sfs lastJoinedRoom] id]];
     }
     */
    
    /*
    else
    {
        [_sfso putInt:JoinRoomRequest_KEY_ROOM_TO_LEAVE value:[[sfs lastJoinedRoom] id]];
    }
     */
    
	if (_asSpectator) {
		[_sfso putBool:JoinRoomRequest_KEY_AS_SPECTATOR value:_asSpectator];
	}
}


@end
