//
//  CreateSFSGameRequest.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 "CreateSFSGameRequest.h"
#import "SFSGameSettings.h"
#import "InviteUsersRequest.h"
#import "CreateRoomRequest.h"
#import "Buddy.h"
#import "User.h"
#import "MatchExpression.h"

NSString * const CreateSFSGameRequest_KEY_IS_PUBLIC = @"gip";
NSString * const CreateSFSGameRequest_KEY_MIN_PLAYERS = @"gmp";
NSString * const CreateSFSGameRequest_KEY_INVITED_PLAYERS = @"ginp";
NSString * const CreateSFSGameRequest_KEY_SEARCHABLE_ROOMS = @"gsr";
NSString * const CreateSFSGameRequest_KEY_PLAYER_MATCH_EXP = @"gpme";
NSString * const CreateSFSGameRequest_KEY_SPECTATOR_MATCH_EXP = @"gsme";
NSString * const CreateSFSGameRequest_KEY_INVITATION_EXPIRY = @"gie";
NSString * const CreateSFSGameRequest_KEY_LEAVE_ROOM = @"glr";
NSString * const CreateSFSGameRequest_KEY_NOTIFY_GAME_STARTED = @"gns";
NSString * const CreateSFSGameRequest_KEY_INVITATION_PARAMS = @"ip";


@implementation CreateSFSGameRequest

@synthesize createRoomRequest = _createRoomRequest;
@synthesize settings = _settings;




-(id)initWithSettings:(SFSGameSettings *)settings {

	if ((self = [super initWithId:BaseRequest_CreateSFSGame])) {
		
		self.settings = settings;
		
		_createRoomRequest = [[CreateRoomRequest alloc] initWithRoomSettings:settings autoJoin:NO roomToLeave:nil];
		
	}
	
	return self;
	
}

+(id)requestWithSettings:(SFSGameSettings *)settings {
	CreateSFSGameRequest *req = [[CreateSFSGameRequest alloc] initWithSettings:settings];
	return req;
}

-(void)validate:(SmartFox2XClient *)sfs {
	
	NSMutableArray *errors = [NSMutableArray array];
	
	@try {
		[_createRoomRequest validate:sfs];
	}
	@catch (SFSValidationException *e) {
		errors = e.errors;
	}
	
	if (_settings.minPlayersToStartGame > _settings.maxUsers) {
		[errors addObject:@"minPlayersToStartGame cannot be greater than maxUsers"];
	}
	
	if (_settings.invitationExpiryTime < InviteUsersRequest_MIN_EXPIRY_TIME || _settings.invitationExpiryTime > InviteUsersRequest_MAX_EXPIRY_TIME) {
		[errors addObject:[NSString stringWithFormat:@"Expiry time value is out of range (%ld-%ld)", (long)InviteUsersRequest_MIN_EXPIRY_TIME, (long)InviteUsersRequest_MAX_EXPIRY_TIME]];
	}
	
	if (_settings.invitedPlayers != nil && [_settings.invitedPlayers count] > InviteUsersRequest_MAX_INVITATIONS_FROM_CLIENT_SIDE) {
		[errors addObject:[NSString stringWithFormat:@"Cannot invite more than %ld players from client side", (long)InviteUsersRequest_MAX_INVITATIONS_FROM_CLIENT_SIDE]];
	}
	
	if ([errors count] > 0) {
		@throw [SFSValidationException validationExceptionWithReason:@"CreateSFSGameRoom request error" errors:errors];
	}
}


-(void)execute:(SmartFox2XClient *)sfs{
	
	[_createRoomRequest execute:sfs];
	super.sfso = [[_createRoomRequest getMessage] content];
	
	[_sfso putBool:CreateSFSGameRequest_KEY_IS_PUBLIC value:_settings.isPublic];
	[_sfso putShort:CreateSFSGameRequest_KEY_MIN_PLAYERS value:_settings.minPlayersToStartGame];
	[_sfso putShort:CreateSFSGameRequest_KEY_INVITATION_EXPIRY value:_settings.invitationExpiryTime];
	[_sfso putBool:CreateSFSGameRequest_KEY_LEAVE_ROOM value:_settings.leaveJoinedLastRoom];
	[_sfso putBool:CreateSFSGameRequest_KEY_NOTIFY_GAME_STARTED value:_settings.notifyGameStarted];
	
	if (_settings.playerMatchExpression != nil) {
		[_sfso putSFSArray:CreateSFSGameRequest_KEY_PLAYER_MATCH_EXP value:[_settings.playerMatchExpression toSFSArray]];
		
	}
	if (_settings.spectatorMatchExpression != nil) {
		[_sfso putSFSArray:CreateSFSGameRequest_KEY_SPECTATOR_MATCH_EXP value:[_settings.spectatorMatchExpression toSFSArray]];
		
	}
	
	if (_settings.invitedPlayers != nil) {
		NSMutableArray *playerIds = [NSMutableArray array];
		
		for (id player in _settings.invitedPlayers) {
			if ([player conformsToProtocol:@protocol(User)] || [player conformsToProtocol:@protocol(Buddy)]) {
				[playerIds addObject:[NSNumber numberWithInt:(int)[(id <User>)player id]]];
			}
		}
		[_sfso putIntArray:CreateSFSGameRequest_KEY_INVITED_PLAYERS value:playerIds];
	}
	
	if (_settings.searchableRooms != nil) {
		[_sfso putUtfStringArray:CreateSFSGameRequest_KEY_SEARCHABLE_ROOMS value:_settings.searchableRooms];
	}
	
	if (_settings.invitationParams != nil) {
		[_sfso putSFSObject:CreateSFSGameRequest_KEY_INVITATION_PARAMS value:_settings.invitationParams];
	}
}

@end
