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

@implementation Logger

static BOOL _locked = YES;
static Logger *_instance = nil;

@synthesize enableConsoleTrace = _enableConsoleTrace;
@synthesize enableEventDispatching = _enableEventDispatching;
@synthesize loggingLevel = _loggingLevel;
@synthesize delegate = _delegate;


-(id)init {
	if ((self = [super init])) {
		if (_locked) {
			@throw [[NSException alloc] initWithName:@"Error" reason:@"Cannot instantiate the Logger using the constructor. Please use the getInstance method" userInfo:nil];
		}
		_loggingLevel = LogLevel_INFO;
		_enableConsoleTrace = YES;
		_enableEventDispatching = YES;
	}
	
	return self;
}

+(Logger *)getInstance {
	if (_instance == nil) {
		_locked = NO;
		_instance = [[Logger alloc] init];
		_locked = YES;
	}
	return _instance;
}

-(void)log:(NSInteger)level message:(NSString *)message {
    
	if (level < _loggingLevel) {
		return;
	}
	
	NSString *levelStr = [LogLevel fromString:level];
	
	if (_enableConsoleTrace) {
		NSLog(@"[SFS - %@]%@", levelStr, message);
	}
	
	NSMutableDictionary *params = [NSMutableDictionary dictionary];
	[params setValue:message forKey:@"message"];
	
	if (_enableEventDispatching) {
		LoggerEvent *evt = [[LoggerEvent alloc] initWithType:levelStr params:params];
		
		if ([_delegate respondsToSelector:@selector(onDebugMessage:)]) {
			[_delegate onDebugMessage:evt];
		}
	}
	
}

-(void)warn:(NSString *)params {
	[self log:LogLevel_WARN message:params];
}
-(void)info:(NSString *)params {
	[self log:LogLevel_INFO message:params];
}

-(void)debug:(NSString *)params {
	[self log:LogLevel_DEBUG message:params];
}

-(void)error:(NSString *)params {
	[self log:LogLevel_ERROR message:params];
}



@end
