//
//  QuickJoinOrCreateRoomRequest.m
//  SFS2X
//  Copyright © 2022 GOTOANDPLAY s.n.c | https://www.smartfoxserver.com All rights reserved.
//

#import "QuickJoinOrCreateRoomRequest.h"
#import "RoomSettings.h"

NSString * const QuickJoinOrCreateRoomRequest_KEY_MATCH_EXPRESSION = @"me";
NSString * const QuickJoinOrCreateRoomRequest_KEY_GROUP_LIST = @"gl";
NSString * const QuickJoinOrCreateRoomRequest_KEY_ROOM_SETTINGS = @"rs";
NSString * const QuickJoinOrCreateRoomRequest_KEY_ROOM_TO_LEAVE = @"tl";

@implementation QuickJoinOrCreateRoomRequest

-(id) initWithMatchExpression:(MatchExpression*)matchExp groupList:(NSArray *)groupList roomSettings:(RoomSettings*)settings roomToLeave:(id<Room>)roomToLeave
{
	if ((self = [super initWithId:BaseRequest_QuickJoinOrCreateRoom])) {
		
		_matchExp = matchExp;
		_groupList = groupList;
		_roomSettings = settings;
		_roomToLeave = roomToLeave;
		
		_createRoomRequest = [CreateRoomRequest requestWithRoomSettings:settings autoJoin:NO roomToLeave:nil];
	}
	
	return self;
}


+(id) requestWithMatchExpression:(MatchExpression*)matchExp groupList:(NSArray*)groupList roomSettings:(RoomSettings*)settings roomToLeave:(id<Room>)roomToLeave
{
	QuickJoinOrCreateRoomRequest* req = [[QuickJoinOrCreateRoomRequest alloc] initWithMatchExpression:matchExp groupList:groupList roomSettings:settings roomToLeave:roomToLeave];
	
	return req;
}


+(id) requestWithMatchExpression:(MatchExpression*)matchExp groupList:(NSArray*)groupList roomSettings:(RoomSettings*)settings
{
	QuickJoinOrCreateRoomRequest* req = [[QuickJoinOrCreateRoomRequest alloc] initWithMatchExpression:matchExp groupList:groupList roomSettings:settings roomToLeave:nil];
	
	return req;
}


-(void)validate:(SmartFox2XClient *)sfs
{
	NSMutableArray *errors = [NSMutableArray array];
	
	if (_matchExp == nil)
		[errors addObject:@"Missing MatchExpression parameter"];
	
	if (_groupList == nil)
		[errors addObject:@"List of groups to search is nil"];
	
	if ([_groupList count] < 1)
		[errors addObject:@"List of groups is empty"];
	
	if (_roomSettings == nil)
		[errors addObject:@"No Room settings provided"];
	
		
	// Validate the "Create Room" part...
	@try
	{
		[_createRoomRequest validate:sfs];
	}
	@catch (SFSValidationException *e)
	{
		errors = e.errors;
	}

	if ([errors count] > 0)
	{
		@throw [SFSValidationException validationExceptionWithReason:@"QuickJoinOrCreateRoom request error" errors:errors];
	}
}


-(void)execute:(SmartFox2XClient *)sfs
{
	// Execute the "Create Room" logic
	[_createRoomRequest execute:sfs];
	
	id<ISFSObject> roomSettings = [[_createRoomRequest getMessage] content];
	
	// Populate the data needed by the server
	[_sfso putSFSArray:QuickJoinOrCreateRoomRequest_KEY_MATCH_EXPRESSION value:[_matchExp toSFSArray]];
	
	[_sfso putUtfStringArray:QuickJoinOrCreateRoomRequest_KEY_GROUP_LIST value:_groupList];
	
	[_sfso putSFSObject:QuickJoinOrCreateRoomRequest_KEY_ROOM_SETTINGS value:roomSettings];
	
	if (_roomToLeave)
	{
		[_sfso putInt:QuickJoinOrCreateRoomRequest_KEY_ROOM_TO_LEAVE
				value:_roomToLeave.id];
	}
}

@end

