//
//  Constant.swift
//  RichPushNotification
//
//  Created by William.Weng on 2022/10/23.
//

import UserNotifications

class Constant {
    // [設定推播的種型](https://stackoverflow.com/questions/54567970/how-to-differentiate-between-whether-a-notification-is-a-local-or-remote-notific)
    enum NotificationTriggerType {
        
        case location
        case push
        case timeInterval
        case calendar
        
        func toString() -> String {
            
            var message = ""
            
            switch self {
            case .location: message = "本地通知"
            case .push: message = "遠端通知"
            case .timeInterval: message = "定時通知"
            case .calendar: message = "行事曆通知"
            }
            
            return message
        }
        
        /// [判斷推播類型](https://franksios.medium.com/ios-本地通知-local-notification-b25229f279ec)
        /// - Parameter notification: [UNNotification](https://www.appcoda.com.tw/ios10-user-notifications/)
        /// - Returns: [NotificationTriggerType?](https://franksios.medium.com/ios-遠程通知-remote-notification-855578c0f088)
        static func parse(with notification: UNNotification) -> NotificationTriggerType? {
            
            var triggerType: NotificationTriggerType?
            
            if notification.request.trigger is UNLocationNotificationTrigger { triggerType = .location }
            if notification.request.trigger is UNPushNotificationTrigger { triggerType = .push }
            if notification.request.trigger is UNTimeIntervalNotificationTrigger { triggerType = .timeInterval }
            if notification.request.trigger is UNCalendarNotificationTrigger { triggerType = .calendar }
            
            return triggerType
        }
    }    
}
