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

@implementation SFSBuddy

@dynamic name;
@dynamic variables;
@dynamic id;
@dynamic isBlocked;
@dynamic isTemp;
@dynamic isOnline;
@dynamic nickName;
@dynamic state;


-(id)initWithId:(NSInteger)id_ name:(NSString *)name isBlocked:(BOOL)isBlocked isTemp:(BOOL)isTemp {
	
	if ((self = [super init])) {
		_id = id_;
		self.name = name; //TODO: CHECK VALIDITY OF THIS CHANGE
        //_name = name;
        
		_isBlocked = isBlocked;
		_variables = [NSMutableDictionary dictionary];
		_isTemp = isTemp;
	}
	
	return self;
	
}

+(id <Buddy>)fromSFSArray:(id <ISFSArray>)arr {
	id <Buddy> buddy = [[SFSBuddy alloc] initWithId:[arr getInt:0] name:[arr getUtfString:1] isBlocked:[arr getBool:2] isTemp:([arr size] > 3 ? [arr getBool:4] : NO)];
	
	id <ISFSArray> bVarsData = [arr getSFSArray:3];
	
	for (int j = 0; j < [bVarsData size]; j++) {
		[buddy setVariable:[SFSBuddyVariable fromSFSArray:[bVarsData getSFSArray:j]]];
	}
	
	return buddy;
	
}


-(NSInteger)id {
	return _id;
}

-(void)setId:(NSInteger)id_ {
	_id = id_;
}

-(NSString *)name {
	return _name;
}

-(void)setName:(NSString *)value {
	_name = value;
}


-(BOOL)isBlocked {
	return _isBlocked;
}

-(void)setBlocked:(BOOL)value {
	_isBlocked = value;
}

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

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


-(void)setVariables:(NSArray *)variables
{
    @synchronized (_variables)
    {
        for (id bVar in variables)
        {
            if ([bVar conformsToProtocol:@protocol(BuddyVariable)])
                [self setVariable:bVar];
        }
    }
}

-(BOOL)isTemp
{
	return _isTemp;
}

-(void)setIsTemp:(BOOL)value
{
	_isTemp = value;
}

-(BOOL)isOnline {
	id <BuddyVariable> bv = [self getVariable:ReservedBuddyVariables_BV_ONLINE];
	
	BOOL onlineStateVar = bv == nil ? YES : [bv getBoolValue];
	
	return onlineStateVar && _id > -1;
}



-(NSString *)state {
	id <BuddyVariable> bv = [self getVariable:ReservedBuddyVariables_BV_STATE];
	return bv == nil ? nil : [bv getStringValue];
}

-(NSString *)nickName {
	id <BuddyVariable> bv = [self getVariable:ReservedBuddyVariables_BV_NICKNAME];
	return bv == nil ? nil : [bv getStringValue];
}

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

-(BOOL)containsVariable:(NSString *)varName
{
    @synchronized (_variables)
    {
        return ([_variables objectForKey:varName] != nil);
    }
}

-(NSArray *)getOfflineVariables
{
	NSMutableArray *offlineVars = [NSMutableArray array];
	
    @synchronized (_variables)
    {
        for (id item in _variables)
        {
            if ([item conformsToProtocol:@protocol(BuddyVariable)])
            {
                if ([[(id <BuddyVariable>)item name] characterAtIndex:0] == [SFSBuddyVariable_OFFLINE_PREFIX characterAtIndex:0]) {
                    [offlineVars addObject:item];
                }
            }
        }
    }
    
    return offlineVars;
}


-(NSArray *)getOnlineVariables
{
	NSMutableArray *onlineVars = [NSMutableArray array];
	
    @synchronized (_variables)
    {
        for (id item in _variables)
        {
            if ([item conformsToProtocol:@protocol(BuddyVariable)])
            {
                if ([[(id <BuddyVariable>)item name] characterAtIndex:0] != [SFSBuddyVariable_OFFLINE_PREFIX characterAtIndex:0]) {
                    [onlineVars addObject:item];
                }
            }
        }
    }
    
    return onlineVars;
}


-(void)removeVariable:(NSString *)varName
{
    @synchronized (_variables)
    {
        [_variables removeObjectForKey:varName];
    }
}

-(void)clearVolatileVariables
{
    @synchronized (_variables)
    {
        for (id bVar in _variables)
        {
            if ([bVar conformsToProtocol:@protocol(BuddyVariable)])
            {
                if ([[(id <BuddyVariable>)bVar name] characterAtIndex:0] != [SFSBuddyVariable_OFFLINE_PREFIX characterAtIndex:0]) {
                    [self removeVariable:[bVar name]];
                }
            }
        }
    }
}

-(NSString *)description
{
	return [NSString stringWithFormat:@"[Buddy: %@, id: %ld]", _name, (long)_id];
}


@end
