//
//  JoinRoomInvitationRequest.m
//  SFS2X
//
//  Created by Lapo on 25/10/16.
//  Copyright © 2016 A51 Integrated | http://a51integrated.com. All rights reserved.
//

#import "JoinRoomInvitationRequest.h"
#import "BaseRequest.h"
#import "SFSObject.h"

NSString * const JoinRoomInvitationRequest_KEY_ROOM_ID = @"r";
NSString * const JoinRoomInvitationRequest_KEY_EXPIRY_SECONDS = @"es";
NSString * const JoinRoomInvitationRequest_KEY_INVITED_NAMES = @"in";
NSString * const JoinRoomInvitationRequest_KEY_AS_SPECT = @"as";
NSString * const JoinRoomInvitationRequest_KEY_OPTIONAL_PARAMS = @"op";


@implementation JoinRoomInvitationRequest

@synthesize targetRoom = _targetRoom;
@synthesize invitedUserNames = _invitedUserNames;
@synthesize expirySeconds = _expirySeconds;
@synthesize asSpectator = _asSpectator;
@synthesize params = _params;

-(id)initWithInvitedUserNames:(NSArray *)invitedNames targetRoom:(id<Room>)room expirySeconds:(NSInteger)secs asSpectator:(BOOL)asSpect params:(id <ISFSObject>)pms;
{
    if ((self = [super initWithId:BaseRequest_JoinRoomInvite])) {
        
        _targetRoom = room;
        _invitedUserNames = invitedNames;
        _expirySeconds = secs;
        _asSpectator = asSpect;
        _params = pms != nil ? pms : [[SFSObject alloc] init];
    }
    
    return self;
}

+(id)initWithInvitedUserNames:(NSArray *)invitedNames targetRoom:(id<Room>)room expirySeconds:(NSInteger)secs asSpectator:(BOOL)asSpect params:(id <ISFSObject>)pms;
{
    JoinRoomInvitationRequest* req = [[JoinRoomInvitationRequest alloc] initWithInvitedUserNames:invitedNames targetRoom:room expirySeconds:secs asSpectator:asSpect params:pms];
    
    return req;
}

-(void)validate:(SmartFox2XClient *)sfs
{
    
    NSMutableArray *errors = [NSMutableArray array];
    
    if (_targetRoom == nil)
        [errors addObject:@"Missing target room"];
    
    else if (_invitedUserNames == nil || _invitedUserNames.count < 1)
        [errors addObject:@"No invitees provided"];
    
    if ([errors count] > 0) {
        @throw [SFSValidationException validationExceptionWithReason:@"JoinRoomInvitationRequest error" errors:errors];
    }
}


-(void)execute:(SmartFox2XClient *)sfs
{

    [_sfso putInt:JoinRoomInvitationRequest_KEY_ROOM_ID value: [_targetRoom id]];
    [_sfso putUtfStringArray:JoinRoomInvitationRequest_KEY_INVITED_NAMES value: _invitedUserNames];
    [_sfso putInt:JoinRoomInvitationRequest_KEY_EXPIRY_SECONDS value: _expirySeconds];
    [_sfso putBool:JoinRoomInvitationRequest_KEY_AS_SPECT value: _asSpectator];
    [_sfso putSFSObject:JoinRoomInvitationRequest_KEY_OPTIONAL_PARAMS value: _params];
}

@end
