//
//  VideoConvertor3.swift
//  tdvideo
//
//  Created by mac on 2024/2/18.
//

import Foundation
import AVKit
import VideoToolbox
import CoreImage
import ImageIO

class VideoConvertor3 {
    
   
    ///在立体视频中，正在处理的当前帧的左眼视图。
    var datas:NSMutableArray = NSMutableArray()
    
    
    func convertVideo( asset : AVAsset, progress: ((Float)->())? = nil  ) async throws {
        
        let assetReader = try AVAssetReader(asset: asset)
        
        //加载音轨
        let output = try await AVAssetReaderTrackOutput(
            track: asset.loadTracks(withMediaType: .video).first!,
            outputSettings: [
                AVVideoDecompressionPropertiesKey: [
                    kVTDecompressionPropertyKey_RequestedMVHEVCVideoLayerIDs: [0, 1] as CFArray,
                ],
            ]
        )
        assetReader.add(output)
        assetReader.startReading()
        
        
        while let nextSampleBuffer = output.copyNextSampleBuffer() {
            
            guard let taggedBuffers = nextSampleBuffer.taggedBuffers else { return }
            
            let leftEyeBuffer = taggedBuffers.first(where: {
                $0.tags.first(matchingCategory: .stereoView) == .stereoView(.leftEye)
            })?.buffer
            let rightEyeBuffer = taggedBuffers.first(where: {
                $0.tags.first(matchingCategory: .stereoView) == .stereoView(.rightEye)
            })?.buffer
            
            if let leftEyeBuffer,
               let rightEyeBuffer,
               case let .pixelBuffer(leftEyePixelBuffer) = leftEyeBuffer,
               case let .pixelBuffer(rightEyePixelBuffer) = rightEyeBuffer {
                
                let lciImage = CIImage(cvPixelBuffer: leftEyePixelBuffer)
                let rciImage = CIImage(cvPixelBuffer: rightEyePixelBuffer)
                
                
                let presentationTime = CMSampleBufferGetPresentationTimeStamp(nextSampleBuffer)
                let dataDic = ["time":presentationTime,"left":lciImage,"right":rciImage] as [String : Any]
                print("解码")
                datas.add(dataDic)
            }
        }
        print( "status - \(assetReader.status)")
        print( "status - \(assetReader.error?.localizedDescription ?? "None")")
        print( "解码完成")
        progress?(0.5)
    }
    
    
    //将两张图片合成一张图片
    func joinImages( leftImage:CIImage, rightImage:CIImage) -> CIImage {
        let left =  UIImage(ciImage: leftImage )
        let right =  UIImage(ciImage: rightImage )
        
        let imageWidth = left.size.width/2 + right.size.width/2
        let imageHeight = left.size.height/2
        
        let newImageSize = CGSize(width:imageWidth, height: imageHeight);
        UIGraphicsBeginImageContextWithOptions(newImageSize, false, 1);
        left.draw(in: CGRect(x:0, y:0, width:imageWidth/2, height:imageHeight))
        right.draw(in: CGRect(x:imageWidth/2, y:0, width:imageWidth/2, height:imageHeight))
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext();
        
        return CIImage(cgImage: image.cgImage!)
    }
}

