#import <Foundation/Foundation.h>

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

#import "PushNativeInterface.h"

// MARK: - Extern APIs

void stove_push_registerPush(const char *token, const char *identifier) {
    NSString *nsToken = StoveSDKMakeNSString(token);
    NSString *nsIdentifier = StoveSDKMakeNSString(identifier);
    
    [SGSPushNotification registerWithDeviceToken:nsToken completionHandler:^(SGSResult * _Nonnull result) {
        NSDictionary *resultDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [result dictionary], @"result",
                                   nil];
        
        StoveSDKNativeCallbackPayload *payload =
        [StoveSDKNativeCallbackPayload payloadWithIdentifier:nsIdentifier value:resultDic];
        [payload sendMessage];
    }];
}

void stove_push_fetchPushSettings(const char *identifier) {
    NSString *nsIdentifier = StoveSDKMakeNSString(identifier);
    
    [SGSPushNotification fetchPushNotificationSettingsWithCompletionHandler:^(SGSResult * _Nonnull result, SGSPushNotificationSettings * _Nullable pushNotificationSettings) {
        NSDictionary *resultDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [result dictionary], @"result",
                                   [pushNotificationSettings dictionary], @"pushSettings",
                                   nil];
        
        StoveSDKNativeCallbackPayload *payload =
        [StoveSDKNativeCallbackPayload payloadWithIdentifier:nsIdentifier value:resultDic];
        [payload sendMessage];
    }];
}

void stove_push_updatePushSettings(const char *pushSettings, const char *identifier) {
    NSString *nsPushSettings = StoveSDKMakeNSString(pushSettings);
    NSString *nsIdentifier = StoveSDKMakeNSString(identifier);
    
    [SGSPushNotification updatePushNotificationSettings:[SGSPushNotificationSettings pushNotificationSettingsWithDictionary:[nsPushSettings JSONObject]]
                                      completionHandler:^(SGSResult * _Nonnull result, SGSPushNotificationSettings * _Nullable pushNotificationSettings) {
        NSDictionary *resultDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [result dictionary], @"result",
                                   [pushNotificationSettings dictionary], @"pushSettings",
                                   nil];
        
        StoveSDKNativeCallbackPayload *payload =
        [StoveSDKNativeCallbackPayload payloadWithIdentifier:nsIdentifier value:resultDic];
        [payload sendMessage];
    }];
}

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