//
//  SetUserPositionRequest.m
//  SFS2X
//
//  Created by Lapo on 29/11/13.
//  Copyright (c) 2013 A51 Integrated | http://a51integrated.com. All rights reserved.
//

#import "SetUserPositionRequest.h"
#import "BaseRequest.h"
#import "SmartFox2XClient.h"
#import "MMORoom.h"

NSString* const SetUserPositionRequest_KEY_ROOM = @"r";
NSString* const SetUserPositionRequest_KEY_VEC3D = @"v";
NSString* const SetUserPositionRequest_KEY_PLUS_USER_LIST = @"p";
NSString* const SetUserPositionRequest_KEY_MINUS_USER_LIST = @"m";
NSString* const SetUserPositionRequest_KEY_PLUS_ITEM_LIST = @"q";
NSString* const SetUserPositionRequest_KEY_MINUS_ITEM_LIST = @"n";

@implementation SetUserPositionRequest
{
    @protected
        Vec3D* _pos;
        id<Room> _room;
}

-(id) initWithPosition:(Vec3D*)pos mmoRoom:(id<Room>)theRoom
{
    // TODO FIX!!!
    self = [super initWithId:BaseRequest_SetUserPosition];
    
    if (self)
    {
		_pos = pos;
        _room = theRoom;
	}
    
    return self;
}

+(id)requestWithPosition:(Vec3D*)pos mmoRoom:(id<Room>)theRoom
{
    return [[SetUserPositionRequest alloc] initWithPosition:pos mmoRoom:theRoom];
}

-(void)validate:(SmartFox2XClient *)sfs
{
    NSMutableArray *errors = [NSMutableArray array];
    
    if (_pos == nil)
    	[errors addObject:@"Position must be a valid Vec3D instance"];
    
    if (_room == nil)
        _room = [sfs lastJoinedRoom];
    
    if (_room == nil)
		[errors addObject:@"You are not joined in any room"];
	
    
    if (![_room isKindOfClass:[MMORoom class]])
        [errors addObject:@"Room is not an MMORoom"];
    
	
	if ([errors count] > 0)
		@throw [SFSValidationException validationExceptionWithReason:@"ChangeRoomCapacity request error"
                                                              errors:errors];
	
}

-(void)execute:(SmartFox2XClient *)sfs
{
    [_sfso putInt:SetUserPositionRequest_KEY_ROOM value:[_room id]];
    
    if (_pos.isFloat)
       [_sfso putFloatArray:SetUserPositionRequest_KEY_VEC3D value:[_pos toArray]];
    else
        [_sfso putIntArray:SetUserPositionRequest_KEY_VEC3D value:[_pos toArray]];
}


@end
