//
// RNHsCoreLogger.m
// Created for HelpshiftX in 2020
// Copyright © 2020 Helpshift. All rights reserved.
//

#import "RNHSCoreLogger.h"

static BOOL consoleLoggingEnabled = false;

static HsLogLevel logLevel = kAdminConfig_loglevel_default_value;

// Use this constant to set a desired log level for local testing
static const int DESIRED_LOG_LEVEL = HsLogLevelDebug;

@implementation RNHSCoreLogger

+ (void) enableConsoleLogging:(BOOL)enableConsoleLogging {
    consoleLoggingEnabled = enableConsoleLogging;
}

+ (void) setLogLevel:(HsLogLevel)inputLogLevel {
    logLevel = inputLogLevel;
}

+ (void) logErrorWithTag:(NSString *)tag message:(NSString *)message {
    [self logWithLevel:HsLogLevelError tag:tag message:message error:nil];
}

+ (void) logErrorWithTag:(NSString *)tag message:(NSString *)message error:(NSError *)error {
    [self logWithLevel:HsLogLevelError tag:tag message:message error:error];
}

+ (void) logWarningWithTag:(NSString *)tag message:(NSString *)message {
    [self logWithLevel:HsLogLevelWarning tag:tag message:message error:nil];
}

+ (void) logWarningWithTag:(NSString *)tag message:(NSString *)message error:(NSError *)error {
    [self logWithLevel:HsLogLevelWarning tag:tag message:message error:error];
}

+ (void) logDebugWithTag:(NSString *)tag message:(NSString *)message {
    [self logWithLevel:HsLogLevelDebug tag:tag message:message error:nil];
}

+ (void) logDebugWithTag:(NSString *)tag message:(NSString *)message error:(NSError *)error {
    [self logWithLevel:HsLogLevelDebug tag:tag message:message error:error];
}

+ (void) logWithLevel:(int)level tag:(NSString *)tag message:(NSString *)message error:(NSError *)error {
    @synchronized(self) {
        if(consoleLoggingEnabled) {
            const NSString *logLevelString;
            switch(level) {
                case HsLogLevelError:
                    logLevelString = level_ERROR;
                    break;
                case HsLogLevelWarning:
                    logLevelString = level_WARN;
                    break;
                case HsLogLevelDebug:
                    logLevelString = level_DEBUG;
                    break;
                case HsLogLevelFatal:
                    logLevelString = level_FATAL;
                    break;
                default:
                    logLevelString = level_DEBUG;
                    break;
            }

            if(level <= DESIRED_LOG_LEVEL) {
                NSMutableString *string = [NSMutableString stringWithFormat:@"[%@ %@]: %@", tag, logLevelString, message];
                if(error) {
                    [string appendFormat:@"\nError : %@\n UserInfo: %@", error.localizedDescription, error.userInfo];
                }
                NSLog(@"%@",string);
            }
        }
    }
}

@end
