//
//  AnagogSDK.swift
//
//  Copyright © 2022 Anagog Ltd. All rights reserved.
//

import Foundation
import JedAIAppKit

extension AnagogSDK: UNUserNotificationCenterDelegate  {
    public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        JedAIApp.shared.didReceiveNotification(response.notification)
        if let oldImplementation = unUserNotificationCenterDelegate?.userNotificationCenter(_:didReceive:withCompletionHandler:) {
            oldImplementation(center, response, completionHandler)
        }
        else {
            completionHandler()
        }
    }
    
    public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        if let oldImplementation = unUserNotificationCenterDelegate?.userNotificationCenter(_:willPresent:withCompletionHandler:) {
            oldImplementation(center, notification, { options in
                completionHandler([options, .alert, .sound])
            })
        }
        else {
            completionHandler([.alert, .sound])
        }
    }
    
    @available(iOS 12.0, *)
    public func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
        unUserNotificationCenterDelegate?.userNotificationCenter?(center, openSettingsFor: notification)
    }
}
