//
//  NotificationService.swift
//  OrderSchedulingNotificationService
//
//  Created by 中道 on 2023/8/28.
//

import UserNotifications
import AVFoundation

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            JPushNotificationExtensionService.jpushSetAppkey(JPushKey)
            JPushNotificationExtensionService.jpushReceive(request) {
                contentHandler(request.content)
            }
            
            let userInfo = bestAttemptContent.userInfo

            if let dict = GROUP.parserAlertToDictionary(userInfo: userInfo) {
                if let title = dict[pushNoti_title_key] {
                    bestAttemptContent.title = title
                }
                if let body = dict[pushNoti_body_key] {
                    bestAttemptContent.body = body
                }
            }else if let body = GROUP.parserAlertToString(userInfo: userInfo) {
                bestAttemptContent.body = body
            }
            
            // 订单类型写入
            if GROUP.isOrderPushNotification(userInfo: userInfo) == true {
                var sound : UNNotificationSound?
                
                let juheVehcileName = userInfo[pushNoti_juheVehcileName_key] as? String
                if juheVehcileName?.isEmpty == false {
                    sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: newJuheOrderAudio_key))
                }else{
                    sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: newOrderAudio_key))
                }
                
                bestAttemptContent.sound = sound
                
                GROUP.setBestAttemptContentUserInfo(bestAttemptContentUserInfo: userInfo)
                contentHandler(bestAttemptContent)
            }else{
                // 当group中没有订单类型的数据时才写入
                if GROUP.isShowPopPushNotification(userInfo: userInfo) == true {
                    if let userInfo = GROUP.bestAttemptContentUserInfo,GROUP.isOrderPushNotification(userInfo: userInfo) == true {
                    }else{
                        GROUP.setBestAttemptContentUserInfo(bestAttemptContentUserInfo: userInfo)
                    }
                }
                // 语音播报能力不具备，只播放默认声音
                bestAttemptContent.sound = .default
                contentHandler(bestAttemptContent)
            }
        }
    }
    
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            let userInfo = bestAttemptContent.userInfo

            if let dict = GROUP.parserAlertToDictionary(userInfo: userInfo) {
                if let title = dict[pushNoti_title_key] {
                    bestAttemptContent.title = title
                }
                if let body = dict[pushNoti_body_key] {
                    bestAttemptContent.body = body
                }
            }else if let body = GROUP.parserAlertToString(userInfo: userInfo) {
                bestAttemptContent.body = body
            }
            
            // 订单类型写入
            if GROUP.isOrderPushNotification(userInfo: userInfo) == true {
                var sound : UNNotificationSound?
                
                let juheVehcileName = userInfo[pushNoti_juheVehcileName_key] as? String
                if juheVehcileName?.isEmpty == false {
                    sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: newJuheOrderAudio_key))
                }else{
                    sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: newOrderAudio_key))
                }
                
                bestAttemptContent.sound = sound
                
                GROUP.setBestAttemptContentUserInfo(bestAttemptContentUserInfo: userInfo)
                contentHandler(bestAttemptContent)
            }else{
                // 当group中没有订单类型的数据时才写入
                if GROUP.isShowPopPushNotification(userInfo: userInfo) == true{
                    if let userInfo = GROUP.bestAttemptContentUserInfo,GROUP.isOrderPushNotification(userInfo: userInfo) == true {
                    }else{
                        GROUP.setBestAttemptContentUserInfo(bestAttemptContentUserInfo: userInfo)
                    }
                }
                // 语音播报能力不具备，只播放默认声音
                bestAttemptContent.sound = .default
                contentHandler(bestAttemptContent)
            }
        }
    }

}
