#import <Foundation/Foundation.h>

#import <SGSBase/SGSBase.h>
#import <SGSLog/SGSLog.h>

#import "LogNativeInterface.h"

// MARK: - Extern APIs

void stove_log_add(const char *logEvent, const char *identifier) {
    NSString *nsLogEvent = StoveSDKMakeNSString(logEvent);
    NSString *nsIdentifier = StoveSDKMakeNSString(identifier);
    
    SGSLogEvent *logEventObject = [SGSLogEvent logEventWithDictionary:[nsLogEvent JSONObject]];
    
    [SGSLog addLogEvent:logEventObject completionHandler:^(SGSResult * _Nonnull result) {
        NSDictionary *resultDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [result dictionary], @"result",
                                   nil];
        
        StoveSDKNativeCallbackPayload *payload =
        [StoveSDKNativeCallbackPayload payloadWithIdentifier:nsIdentifier value:resultDic];
        [payload sendMessage];
    }];
}

void stove_log_flush() {
    
    [SGSLog flush];
}

const char *stove_log_version() {
    NSString * version = [SGSLog moduleVersion];
    return StoveSDKMakeCString(version);
}

const char *stove_log_sdk_versions() {
    NSDictionary *stoveVersion = [SGSLog SDKVersions];
    NSString *versionString = [stoveVersion JSONString];

    return StoveSDKMakeCString(versionString);
}

void stove_log_set_external_id(const char *externalId) {
    NSString *nsExternalId = StoveSDKMakeNSString(externalId);
    [SGSLog setExternalId:nsExternalId];
}

void stove_game_log_add(const char *logEvent, const char *identifier) {
    NSString *nsLogEvent = StoveSDKMakeNSString(logEvent);
    NSString *nsIdentifier = StoveSDKMakeNSString(identifier);

    NSDictionary *jsonDictionary = [nsLogEvent JSONObject];
    if (jsonDictionary == nil) {
        SGSResult *cancelResult = [[SGSResult alloc] initWithDomain:@"com.stove.cancel"
                                                          errorCode:10
                                                       errorMessage:@"Canceled"
                                                           userInfo:nil];
        NSDictionary *resultDic = @{@"result": [cancelResult dictionary]};
        StoveSDKNativeCallbackPayload *payload =
        [StoveSDKNativeCallbackPayload payloadWithIdentifier:nsIdentifier value:resultDic];
        [payload sendMessage];
        return;
    }

    SGSGameLogEvent *logEventObject = [SGSGameLogEvent gameLogEventWithDictionary:jsonDictionary];
    if (logEventObject == nil) {
        SGSResult *cancelResult = [[SGSResult alloc] initWithDomain:@"com.stove.cancel"
                                                          errorCode:10
                                                       errorMessage:@"Canceled"
                                                           userInfo:nil];
        NSDictionary *resultDic = @{@"result": [cancelResult dictionary]};
        StoveSDKNativeCallbackPayload *payload =
        [StoveSDKNativeCallbackPayload payloadWithIdentifier:nsIdentifier value:resultDic];
        [payload sendMessage];
        return;
    }

    [SGSGameLog add:logEventObject completionHandler:^(SGSResult * _Nonnull result) {
        NSDictionary *resultDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [result dictionary], @"result",
                                   nil];

        StoveSDKNativeCallbackPayload *payload =
        [StoveSDKNativeCallbackPayload payloadWithIdentifier:nsIdentifier value:resultDic];
        [payload sendMessage];
    }];
}

void stove_game_log_flush() {
    [SGSGameLog flush];
}