//
//  Tool.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/8/10.
//

import Foundation
import UIKit
import AVFoundation
import DDAudioPlayerKit_Private

public let TOOL = Tool.default

open class Tool {
    public static let `default` = Tool()
    // duration is deprecated in swift 
    @objc func getDuration(url:URL) -> CMTime {
        let urlAsset = AVURLAsset(url: url)
        var duration : CMTime = CMTime.zero
        duration = urlAsset.duration
        return duration
    }
    
    public func getVideoDateString(duration:Int) -> String {
        let min = duration / 60
        var minStr = ""
        if min > 9 {
            minStr = String(min)
        }else {
            minStr = "0"+String(min)
        }
        
        let sec = duration % 60
        var secStr = ""
        if sec > 9 {
            secStr = String(sec)
        }else{
            secStr = "0"+String(sec)
        }
        
        return minStr+":"+secStr
    }
    
    public func getDateStringCompareWithNow(dateFormat:String,dateString:String) -> String {
        let format = DateFormatter()
        format.dateFormat = dateFormat
        let date = format.date(from: dateString) ?? Date()
        let dateNow = Date()
        let dateNowString = format.string(from: dateNow)
        let dateYMD = dateString.components(separatedBy: " ").first
        let dateNowYMD = dateNowString.components(separatedBy: " ").first
        if dateYMD == dateNowYMD {
            let diffInterval = dateNow.timeIntervalSince1970 - date.timeIntervalSince1970
            var combineString : String
            if diffInterval >= 3600 {
                combineString = "今天" + (dateString.components(separatedBy: " ").last ?? "")
            }else if diffInterval >= 60 {
                combineString = String(Int(diffInterval / 60))+"分钟前"
            }else{
                combineString = String(Int(diffInterval))+"秒前"
            }
            return combineString
        }else{
            let dateYesterdayArr = format.string(from: dateNow.addingTimeInterval(-3600 * 24)).components(separatedBy: " ")
            let dateYesterdayYMD = dateYesterdayArr.first
            if dateYMD == dateYesterdayYMD {
                return "昨天" + (dateYesterdayArr.last ?? "")
            }
        }
        return dateString
    }
    
    // MARK: 远程推送相关
    func getOrderString(userInfo:[AnyHashable:Any]?) -> String{
        var string : String = ""

        if GROUP.isOrderPushNotification(userInfo: userInfo) == true {
            let taskCode = userInfo?[pushNoti_taskCode_key] as? String
            let juheVehcileName = userInfo?[pushNoti_juheVehcileName_key] as? String
            if let juheVehcileName, juheVehcileName.isEmpty == false {
                string = "您有一条新的聚合订单\(taskCode ?? ""),聚合车辆:\(juheVehcileName)"
            }else{
                string = "您有一条新订单\(taskCode ?? "")"
            }
        }
        
        return string
    }
    
    func dealWithOrderPushNotification(completionHandler:(([AnyHashable:Any]) -> Void)?) {
        if let userInfo = GROUP.bestAttemptContentUserInfo {
            if completionHandler != nil {
                completionHandler!(userInfo)
            }
            GROUP.setBestAttemptContentUserInfo(bestAttemptContentUserInfo: nil)
        }
    }
    
    func playAlertWith(userInfo:[AnyHashable:Any]?) {
//        if GROUP.newMessageAlert != true {
//            return
//        }
        if GROUP.isOrderPushNotification(userInfo: userInfo) == true {
            ACCEPTORDERTOOL.open(userInfo: userInfo)
        }else{
            PUSHNOTICOMMONTOOL.open(userInfo: userInfo)
        }
    }
    
    func playVoiceWith(userInfo:[AnyHashable:Any]?) {
        if GROUP.newMessageSound != true {
            return
        }
        
        stopVoice()
        
        if GROUP.isOrderPushNotification(userInfo: userInfo) == true {
            let juheVehcileName = userInfo?[pushNoti_juheVehcileName_key] as? String
            var auidoUrl : URL?
            if juheVehcileName?.isEmpty == false {
                auidoUrl = URL(string: Bundle.main.path(forResource: newJuheOrderAudio_key, ofType: nil) ?? "")
            }else{
                auidoUrl = URL(string: Bundle.main.path(forResource: newOrderAudio_key, ofType: nil) ?? "")
            }
            
            if let auidoUrl {
                DDAS.playSound(audioUrl: auidoUrl,name: "sound",numberOfLoops: 10,timeInterval: 2)
            }
        }else if GROUP.isBroadcastPushNotification(userInfo: userInfo) == true, let broadcast = GROUP.parserBroadcastToString(userInfo: userInfo) {
            DDSS.play(text: broadcast, name: "broadcast")
        }
    }

    func playVoiceWith(broadcast: String) {
        DDSS.play(text: broadcast, name: "broadcast",seperateChar: false)
    }
    
    func stopVoice() {
        DDAS.stopSound(name: "sound")
        DDSS.stop(name: "broadcast")
    }
}
