//
//  GroupData.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/8/30.
//

import Foundation

public let GROUP = GroupData.default

open class GroupData {
    public static let `default` = GroupData()
    let groudUserDefault = UserDefaults(suiteName: GROUPID)
    
    // 解析出字典
    func parserAlertToDictionary(userInfo:[AnyHashable:Any]?) -> Dictionary<String,String>? {
        let aps = userInfo?[pushNoti_aps_key] as? [String:Any]
        
        if let data = (aps?[pushNoti_alert_key] as? String)?.data(using: .utf8) {
            let dict = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: String]
            return dict
        }
        
        return nil
    }
    
    func parserAlertToString(userInfo:[AnyHashable:Any]?) -> String? {
        let aps = userInfo?[pushNoti_aps_key] as? [String:Any]
        let string = aps?[pushNoti_alert_key] as? String
        return string
    }
    
    // 判断新推送是不是新订单
    func isOrderPushNotification(userInfo:[AnyHashable:Any]?) -> Bool {
        if let userInfo {
            if let pushType = (userInfo[pushNoti_pushType_key] as? String), pushType == "1" {
                return true
            }
        }
        return false
    }
    
    // 判断新推送是不是取消订单
    func isCancelOrderPushNotification(userInfo:[AnyHashable:Any]?) -> Bool {
        if let userInfo {
            if let pushType = (userInfo[pushNoti_pushType_key] as? String), pushType == "8" {
                return true
            }
        }
        return false
    }
    
    // 判断是不是跳转报备的订单推送
    func isReportIndexPushNotification(userInfo:[AnyHashable:Any]?) -> Bool {
        if let userInfo {
            if let pushType = (userInfo[pushNoti_pushType_key] as? String), pushType == "10" {
                return true
            }
        }
        return false
    }
    
    // 解析需要播放的文字
    func parserBroadcastToString(userInfo:[AnyHashable:Any]?) -> String? {
        if let broadcastKeywords = (userInfo?[pushNoti_broadcastKeywords_key] as? String),broadcastKeywords.isEmpty == false {
            return broadcastKeywords
        }
        return nil
    }
    
    // 判断是不是需要语音广播
    func isBroadcastPushNotification(userInfo:[AnyHashable:Any]?) -> Bool {
        if let broadcastKeywords = (userInfo?[pushNoti_broadcastKeywords_key] as? String),broadcastKeywords.isEmpty == false {
            return true
        }
        return false
    }
    
    // 是否需要弹窗
    func isShowPopPushNotification(userInfo:[AnyHashable:Any]?) -> Bool {
        if let isShowPop = (userInfo?[pushNoti_isShowPop_key] as? Bool),isShowPop == true {
            return true
        }
        if let isShowPop = (userInfo?[pushNoti_isShowPop_key] as? String),isShowPop == "true" {
            return true
        }
        return false
    }
    
    // 不需要记录
    public var bestAttemptContentUserInfo : [AnyHashable:Any]? {
        return groudUserDefault?.object(forKey: bestAttemptContentUserInfo_key) as? [AnyHashable:Any]
    }
    
    func setBestAttemptContentUserInfo(bestAttemptContentUserInfo:[AnyHashable:Any]?) {
        groudUserDefault?.set(bestAttemptContentUserInfo, forKey: bestAttemptContentUserInfo_key)
    }
    
    // 不需要记录
    public var newMessageSound : Bool {
        
        if let value = groudUserDefault?.object(forKey: newMessageSound_key) as? Bool {
            return value
        }
        /// 暂时解决崩溃的问题
        if #available(iOS 17.0, *) {
            return false
        }
        
        return true
    }
    
    func setNewMessageSound(newMessageSound:Bool?) {
        groudUserDefault?.set(newMessageSound, forKey: newMessageSound_key)
    }
    
//    // 不需要记录
//    public var newMessageAlert : Bool {
//        return (groudUserDefault?.object(forKey: newMessageAlert_key) as? Bool) ?? true
//    }
//
//    func setNewMessageAlert(newMessageAlert:Bool?) {
//        groudUserDefault?.set(newMessageAlert, forKey: newMessageAlert_key)
//    }
    
    func clear() {
//        GROUP.setNewMessageAlert(newMessageAlert: nil)
        GROUP.setNewMessageSound(newMessageSound: nil)
        GROUP.setBestAttemptContentUserInfo(bestAttemptContentUserInfo: nil)
    }
}
