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

import Foundation
import UIKit
import AVFoundation

public let TOOL = Tool.default

open class Tool {
    public static let `default` = Tool()
    func getDuration(url:URL) -> CMTime {
        let urlAsset = AVURLAsset(url: url)
        var duration : CMTime = CMTime.zero
//        if #available(iOS 16.0, *) {
//            do {
//                duration = try await urlAsset.load(.duration)
//            }catch{
//            }
//        }else{
            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 call(phone:String) {
        guard let url  = URL(string: "tel:\(phone)") else {
            return
        }

        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url, options: [:],completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
    
    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
    }
    
    public func getVersion() -> String {
        return (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? "1.0.0"
    }
    
    public func getIPhoneCode() -> String {
        var systemInfo = utsname()
        uname(&systemInfo)

        let versionCode: String = String(validatingUTF8: NSString(bytes: &systemInfo.machine, length: Int(_SYS_NAMELEN), encoding: String.Encoding.ascii.rawValue)!.utf8String!)!

        switch versionCode {
            /*** iPhone ***/
            case "iPhone5,1", "iPhone5,2":                   return "iPhone5"
            case "iPhone5,3", "iPhone5,4":                   return "iPhone5C"
            case "iPhone6,1", "iPhone6,2":                   return "iPhone5S"
            case "iPhone7,2":                                return "iPhone6"
            case "iPhone7,1":                                return "iPhone6Plus"
            case "iPhone8,1":                                return "iPhone6S"
            case "iPhone8,2":                                return "iPhone6SPlus"
            case "iPhone8,3", "iPhone8,4":                   return "iPhoneSE"
            case "iPhone9,1", "iPhone9,3":                   return "iPhone7"
            case "iPhone9,2", "iPhone9,4":                   return "iPhone7Plus"
            case "iPhone10,1", "iPhone10,4":                 return "iPhone8"
            case "iPhone10,2", "iPhone10,5":                 return "iPhone8Plus"
            case "iPhone10,3", "iPhone10,6":                 return "iPhoneX"
            case "iPhone11,2":                               return "iPhoneXS"
            case "iPhone11,4", "iPhone11,6":                 return "iPhoneXS_Max"
            case "iPhone11,8":                               return "iPhoneXR"
            case "iPhone12,1":                               return "iPhone11"
            case "iPhone12,3":                               return "iPhone11Pro"
            case "iPhone12,5":                               return "iPhone11Pro_Max"
            case "iPhone12,8":                               return "iPhoneSE2"
            case "iPhone13,1":                               return "iPhone12Mini"
            case "iPhone13,2":                               return "iPhone12"
            case "iPhone13,3":                               return "iPhone12Pro"
            case "iPhone13,4":                               return "iPhone12Pro_Max"
            case "iPhone14,4":                               return "iPhone13Mini"
            case "iPhone14,5":                               return "iPhone13"
            case "iPhone14,2":                               return "iPhone13Pro"
            case "iPhone14,3":                               return "iPhone13Pro_Max"
            case "iPhone14,7":                               return "iPhone14"
            case "iPhone14,8":                               return "iPhone14Plus"
            case "iPhone15,2":                               return "iPhone14Pro"
            case "iPhone15,3":                               return "iPhone14Pro_Max"
        default:
            break
        }
        return "未知"
    }

    func playVoiceWith(userInfo:[AnyHashable:Any]?) {
        if let userInfo {
            let aps = userInfo["aps"] as? [String:Any]

            if let aps, (aps["alert"] as? String) == "新订单" {
                let juheVehcileName = userInfo["juheVehcileName"] as? String
                
                var auidoUrl : URL?
                
                if juheVehcileName?.isEmpty == false {
                    auidoUrl = URL(string: Bundle.main.path(forResource: "newJuheOrderAudio.wav", ofType: nil) ?? "")
                }else{
                    auidoUrl = URL(string: Bundle.main.path(forResource: "newOrderAudio.wav", ofType: nil) ?? "")
                }
                
                if let auidoUrl {
                    var audioSoundID:SystemSoundID = 0
                    AudioServicesCreateSystemSoundID(auidoUrl as CFURL, &audioSoundID)
                    AudioServicesPlaySystemSoundWithCompletion(audioSoundID) {
                    }
                }
            }
        }
    }
    
    func getOrderString(userInfo:[AnyHashable:Any]?) -> String{
        var string : String = ""

        if let userInfo {
            let aps = userInfo["aps"] as? [String:Any]
            if let aps, (aps["alert"] as? String) == "新订单" {
                let juheVehcileName = userInfo["juheVehcileName"] as? String
                let taskCode = userInfo["taskCode"] as? String
                if juheVehcileName?.isEmpty == false {
                    string = "您有一条新的聚合订单\(taskCode ?? "")"
                }else{
                    string = "您有一条新订单\(taskCode ?? "")"
                }
            }
        }
        
        return string
    }
}
