//
//  SFSUserManager.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 "SFSUserManager.h"
#import "Logger.h"

@implementation SFSUserManager

@dynamic userCount;


-(id)initWithSfs:(SmartFox2XClient *)sfs {
	if ((self = [super init])) {
		_smartFox = sfs;
		_log = [Logger getInstance];
		_usersById = [[NSMutableDictionary alloc] init];
		_usersByName = [[NSMutableDictionary alloc] init];
        _globalLock = [[NSObject alloc] init];
	}
	
	return self;
}

// This doesn't require locking as we already lock the outside calls
-(void)_addUser:(id <User>)user
{
    [_usersByName setObject:user forKey:[user name]];
    [_usersById setObject:user forKey:[NSNumber numberWithInt:(int)[user id]]];
}

#pragma mark IUserManager protocol

-(BOOL)containsUserName:(NSString *)userName
{
    @synchronized (_globalLock)
    {
        return ([_usersByName objectForKey:userName] != nil);
    }
}

-(BOOL)containsUserId:(NSInteger)userId
{
    @synchronized (_globalLock)
    {
        return ([_usersById objectForKey:[NSNumber numberWithInt:(int)userId]] != nil);
    }
}

-(BOOL)containsUser:(id <User>)user
{
    @synchronized (_globalLock)
    {
        return ([[_usersByName allValues] indexOfObject:user] != NSNotFound);
    }
}
				

-(id <User>)getUserByName:(NSString *)userName
{
    @synchronized (_globalLock)
    {
        return [_usersByName objectForKey:userName];
    }
}

-(id <User>)getUserById:(NSInteger)userId
{
    @synchronized (_globalLock)
    {
        return [_usersById objectForKey:[NSNumber numberWithInt:(int)userId]];
    }
}

-(void)addUser:(id <User>)user
{
    @synchronized (_globalLock)
    {
        if ([self containsUser:user])
            [_log warn:[NSString stringWithFormat:@"Unexpected: duplicate user in UserManager: %@", user]];
        else
            [self _addUser:user];
    }
}

-(void)removeUser:(id <User>)user
{
    @synchronized (_globalLock)
    {
        if([self containsUser:user])
        {
            [_usersByName removeObjectForKey:[user name]];
            [_usersById removeObjectForKey:[NSNumber numberWithInt:(int)[user id]]];
        }
    }
}

-(void)removeUserById:(NSInteger)id_
{
    id <User> user = [self getUserById:id_];
	
	if (user != nil)
		[self removeUser:user];
	
}

-(void)replaceAll:(NSArray*)userList;
{
    @synchronized (_globalLock)
    {
        [_usersByName removeAllObjects];
        [_usersById removeAllObjects];
        
        for (id user in userList)
        {
            if ([user conformsToProtocol:@protocol(User)])
                [self addUser:user];
        }
    }
}

-(NSInteger)userCount
{
    @synchronized (_globalLock)
    {
        return [_usersById count];
    }
	
}

-(NSArray *)getUserList
{
    @synchronized (_globalLock)
    {
        return [_usersById allValues];
    }
}

-(SmartFox2XClient *)smartFox {
	return _smartFox;
}


@end
