//
//  SFSBuddyManager.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 "SFSBuddyManager.h"
#import "ArrayUtil.h"
#import "ReservedBuddyVariables.h"
#import "SFSBuddyVariable.h"

@implementation SFSBuddyManager


@dynamic buddyList;
@dynamic buddyStates;
@dynamic isInited;
@dynamic myNickName;
@dynamic myOnlineState;
@dynamic myState;
@dynamic myVariables;
@dynamic offlineBuddies;
@dynamic onlineBuddies;


-(id)initWithSfs:(SmartFox2XClient *)sfs
{
	if ((self = [super init]))
    {
		_sfs = sfs;
    
        _buddiesByName = [NSMutableDictionary dictionary];
        _myVariables = [NSMutableDictionary dictionary];
		_inited = NO;
	}
	
	return self;
}

#pragma mark IBuddyManager protocol

-(BOOL)isInited
{
	return _inited;
}

-(void)setInited:(BOOL)value
{
	_inited = YES;
}

-(void)addBuddy:(id <Buddy>)buddy
{
    @synchronized (_buddiesByName)
    {
        [_buddiesByName setObject:buddy forKey:[buddy name]];
    }
}

-(id <Buddy>)getBuddyById:(NSInteger)id_
{
    @synchronized (_buddiesByName)
    {
        if (id_ > -1)
        {
            for (id buddy in [_buddiesByName allValues])
            {
                if ([(id <Buddy>)buddy id] == id_)
                    return buddy;
            }
        }
    }
	
	return nil;
}

-(id <Buddy>)removeBuddyById:(NSInteger)id_
{
	id <Buddy> buddy = [self getBuddyById:id_];
	
	if (buddy != nil)
 		[_buddiesByName removeObjectForKey:[buddy name]];
	
    return buddy;
}

-(id <Buddy>)getBuddyByName:(NSString *)name {
	return [_buddiesByName objectForKey:name];
}

-(id <Buddy>)removeBuddyByName:(NSString *)name {

	id <Buddy> buddy = [self getBuddyByName:name];
	
	if (buddy != nil)
    {
        @synchronized (_buddiesByName)
        {
            [_buddiesByName removeObjectForKey:name];
        }
	}
	
	return buddy;
}

-(BOOL)containsBuddy:(NSString *)name
{
    @synchronized (_buddiesByName)
    {
        return ([self getBuddyByName:name] != nil);
    }
}


-(id <Buddy>)getBuddyByNickName:(NSString *)nickName
{
    @synchronized (_buddiesByName)
    {
        for (id buddy in [_buddiesByName allValues])
        {
            if ([buddy conformsToProtocol:@protocol(Buddy)])
            {
                
                if ([[(id <Buddy>)buddy nickName] isEqualToString:nickName])
                    return buddy;
            }
        }
    }
	
	return nil;
}

-(NSArray *)offlineBuddies
{
	NSMutableArray *arr = [NSMutableArray array];
	
    @synchronized (_buddiesByName)
    {
        for (id buddy in [_buddiesByName allValues])
        {
            if ([buddy conformsToProtocol:@protocol(Buddy)])
            {
                if (![(id <Buddy>)buddy isOnline])
                    [arr addObject:buddy];
            }
            
        }
    }
	
	return arr;
}

-(NSArray *)onlineBuddies
{
	NSMutableArray *arr = [NSMutableArray array];
    
    @synchronized (_buddiesByName)
    {
        for (id buddy in [_buddiesByName allValues])
        {
            if ([buddy conformsToProtocol:@protocol(Buddy)])
            {
                if ([(id <Buddy>)buddy isOnline])
                    [arr addObject:buddy];
            }
        }
    }
	
	return arr;
}

-(NSArray *)buddyList
{
    @synchronized (_buddiesByName)
    {
        return [ArrayUtil objToArray:_buddiesByName];
    }
}

-(NSArray *)buddyStates
{
	return _buddyStates;
}

-(id <BuddyVariable>)getMyVariable:(NSString *)varName
{
    @synchronized (_myVariables)
    {
        return [_myVariables objectForKey:varName];
    }
	
}

-(NSArray *)myVariables
{
    @synchronized (_myVariables)
    {
        return [ArrayUtil objToArray:_myVariables];
    }
	
}

-(BOOL)myOnlineState
{
	if (!_inited)
		return NO;
	
	
	BOOL onlineState = YES;
	
	id <BuddyVariable> onlineVar = [self getMyVariable:ReservedBuddyVariables_BV_ONLINE];
	
	if (onlineVar != nil) {
		onlineState = [onlineVar getBoolValue];
	}
	
	return onlineState;

}

-(NSString *)myNickName
{
	return _myNickName;
}

-(NSString *)myState
{
	return _myState;
}


-(void)setMyVariable:(id <BuddyVariable>)bVar
{
    @synchronized (_myVariables)
    {
        [_myVariables setObject:bVar forKey:[bVar name]];
    }
    
}


-(void)setMyVariables:(NSArray *)variables
{
	for (id <BuddyVariable> bVar in variables)
    {
		if ([bVar conformsToProtocol:@protocol(BuddyVariable)])
			[self setMyVariable:bVar];
	}
}


-(void)setMyOnlineState:(BOOL)isOnline
{
	[self setMyVariable:[[SFSBuddyVariable alloc] initWithName:ReservedBuddyVariables_BV_ONLINE value:[NSNumber numberWithBool:isOnline] type:-1]];
}


-(void)setMyNickName:(NSString *)nickName
{
	_myNickName = nickName;
}


-(void)setMyState:(NSString *)state
{
	_myState = state;
}


-(void)setBuddyStates:(NSArray *)states
{
	_buddyStates = (NSMutableArray *)states;
}

-(void)clearAll
{
    @synchronized (_buddiesByName)
    {
        [_buddiesByName removeAllObjects];
    }
}

@end
