//
//  SetUserVariablesRequest.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 "SetUserVariablesRequest.h"
#import "UserVariable.h"
#import "SFSArray.h"
#import "Variable.h"

NSString * const SetUserVariablesRequest_KEY_USER = @"u";
NSString * const SetUserVariablesRequest_KEY_VAR_LIST = @"vl";

@implementation SetUserVariablesRequest

@synthesize userVariables = _userVariables;



-(id)initWithUserVariables:(NSArray *)userVariables {
	
	if ((self = [super initWithId:BaseRequest_SetUserVariables])) {
		self.userVariables = userVariables;
	}
	
	return self;
}

+(id)requestWithUserVariables:(NSArray *)userVariables {
	SetUserVariablesRequest *req = [[SetUserVariablesRequest alloc] initWithUserVariables:userVariables];
	return req;
}


-(void)validate:(SmartFox2XClient *)sfs {
	
	NSMutableArray *errors = [NSMutableArray array];
	
	if(_userVariables == nil || [_userVariables count] == 0){
		[errors addObject:@"No variables were specified"];
	}
	
	if ([errors count] > 0) {
		@throw [SFSValidationException validationExceptionWithReason:@"SetUserVariables request error" errors:errors];
	}
}


-(void)execute:(SmartFox2XClient *)sfs
{
	
	id <ISFSArray> varList = [SFSArray newInstance];
	
	for (id uVar in _userVariables)
    {
		
		if ([uVar conformsToProtocol:@protocol(UserVariable)])
        	[varList addSFSArray:[(id <UserVariable>)uVar toSFSArray]];
	}
		
	[_sfso putSFSArray:SetUserVariablesRequest_KEY_VAR_LIST value:varList];

}

@end
