//
//  ZZHHelper.swift
//  SwiftProject
//
//  Created by aaa on 2024/3/11.
//

import Foundation
import AVFoundation


class ZZHHelper {
    class func formatVideoTime(second:Int) -> String {
        let allTime: Int = second
        var hours = 0
        var minutes = 0
        var seconds = 0
        var hoursText = ""
        var minutesText = ""
        var secondsText = ""
        
        hours = allTime / 3600
        hoursText = hours > 9 ? "\(hours)" : "0\(hours)"
        
        minutes = allTime % 3600 / 60
        minutesText = minutes > 9 ? "\(minutes)" : "0\(minutes)"
        
        seconds = allTime % 3600 % 60
        secondsText = seconds > 9 ? "\(seconds)" : "0\(seconds)"
        
        if hoursText == "00" {
            return "\(minutesText):\(secondsText)"
        }else{
            return "\(hoursText):\(minutesText):\(secondsText)"
        }
    }
    
    
    
    
    //MARK: 检查是否为空间视频
    class func isSpatialVideo(asset: AVAsset)  -> Bool {
//        for track in asset.tracks {
//            if track.mediaType == .video {
//                for formatDesc in track.formatDescriptions {
//                    let dic = CMFormatDescriptionGetExtensions(formatDesc as! CMFormatDescription) as! Dictionary<String, Any>
//                    print("fromatdesc:\n\(dic)\n\n")
//                }
//            }
//        }
//        return false
        
        
        let metadata = asset.metadata(forFormat: AVMetadataFormat.quickTimeMetadata)
        let isSpatialVideo = metadata.contains { item in
            print("item:\n\(item) \n\n\n")
            if let identifier = item.identifier?.rawValue {
                return (identifier == "mdta/com.apple.quicktime.spatial.format-version") || (identifier == "mdta/com.apple.quicktime.spatial-overcapture.quality-scoring-version")
            }
            return false
        }
        return isSpatialVideo
        
    }
    
    //设置曝光时间
    class func setNowTimeToUserDefaultWithKey(_ key:String) {
        UserDefaults.standard.setValue(Date.now, forKey: key)
        UserDefaults.standard.synchronize()
    }
    
    //主要用于统计曝光时间
    class func getSecFromUserDefaultByKey(_ key:String)->TimeInterval {
        var sec:TimeInterval = 0
        let date = UserDefaults.standard.value(forKey: key)
        if let d = date as? Date{
             sec = DateInterval.init(start: d, end: Date.now).duration
        }
        return sec
    }
}
