#import "UnityAppController.h"
#import <SGSPush/SGSPush.h>
#import <SGSAuth/SGSAuth.h>

@interface StoveSDKAppController : UnityAppController <SGSPushNotificationCenterDelegate>

@end

@implementation StoveSDKAppController

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [[SGSPushNotificationCenter currentPushNotificationCenter] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    
    return [SGSApplicationDelegate application:app openURL:url options:options];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
    
    [[SGSPushNotificationCenter currentPushNotificationCenter] application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

// if (FOREGROUND_NOTIFICATION_ENABLED)
// {
/** Foreground Notification **/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[SGSPushNotificationCenter currentPushNotificationCenter] setDelegate:self];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

/** Foreground Notification **/
- (void)willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
    UNNotificationPresentationOptions options = 0;
    
    // ForegroundPushEnabled: 키 없으면 기본 true (포그라운드 푸시 사용)
    NSNumber *foregroundPushSettings = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"ForegroundPushEnabled"];
    BOOL foregroundPushEnabled = (foregroundPushSettings == nil) ? YES : foregroundPushSettings.boolValue;


    // EnablePushListOptions: 키 없으면 기본 true (list 기본 사용)
    NSNumber *pushListOptionEnabled = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"EnablePushListOptions"];
    BOOL enablePushListOptions = (pushListOptionEnabled == nil) ? YES : pushListOptionEnabled.boolValue;
    
    if (foregroundPushEnabled) {
        options |= UNNotificationPresentationOptionSound;
        
        if (@available(iOS 14.0, *)) {
            options |= UNNotificationPresentationOptionBanner;
            if (enablePushListOptions) {
                options |= UNNotificationPresentationOptionList;
            }
        } else {
            options |= UNNotificationPresentationOptionAlert;
        }
    }
    
    completionHandler(options);
}
// }


@end

IMPL_APP_CONTROLLER_SUBCLASS(StoveSDKAppController)
