//
//  PlayByTransferConvertor.swift
//  SwiftProject
//
//  Created by aaa on 2024/3/12.
//

import Foundation
import AVKit
import VideoToolbox
import CoreImage
import ImageIO



class PlayByTransferConvertor {
    func convertVideo(asset:AVAsset, assetOutput:AVAssetReaderTrackOutput,type:SpatialType,time: CMTime)->(CIImage?)  {
        var newpb:CIImage? = nil
        while let nextSampleBuffer = assetOutput.copyNextSampleBuffer() {
            let presentationTime = CMSampleBufferGetPresentationTimeStamp(nextSampleBuffer)
            if presentationTime == time {
                guard let taggedBuffers = nextSampleBuffer.taggedBuffers else { break }
                
                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 left =  UIImage(ciImage: lciImage )
                    let right =  UIImage(ciImage: rciImage )
                    
                    
                    var cwidth:CGFloat
                    var cheight:CGFloat
                    switch type {
                    case .hsbs:
                        cwidth = left.size.width
                        cheight = left.size.height
                        newpb = joinImages_sbs(left: left, right: right, imgWidth: cwidth, imgHeight:cheight )
                        break
                    case .fsbs:
                        cwidth = left.size.width
                        cheight = left.size.height
                        newpb = joinImages_fsbs(left: left, right: right, imgWidth: cwidth, imgHeight: cheight)
                        break
                    case .parallelEyes://平行眼
                        
                        newpb = joinImages(leftImage: lciImage, rightImage: rciImage)
                        break
                    case .crossedEyes://交叉眼
                        newpb = joinImages(leftImage: rciImage, rightImage: lciImage)
                        break
                    case .redBlueSolid://红蓝立体
                        newpb = joinImages_red_blue(lciImage: lciImage, rciImage: rciImage)
                        break
                    default:
                        break
                    }
                }
                break
            }
        }
        return newpb
    }
    
    
    //合成红蓝立体图片
    func joinImages_red_blue(lciImage:CIImage,rciImage:CIImage) -> CIImage {
        // 创建红色和蓝色滤镜
        let redColorMatrix: [CGFloat] = [
            0.0, 0.0, 0.0, 0.0, 0.0, // 红色通道
            0.0, 0.0, 0.0, 0.0, 0.0, // 绿色通道
            0.0, 0.0, 1.0, 0.0, 0.0, // 蓝色通道
            0.0, 0.0, 0.0, 1.0, 0.0  // 透明通道
        ]
        
        let blueColorMatrix: [CGFloat] = [
            1.0, 0.0, 0.0, 0.0, 0.0, // 红色通道
            0.0, 0.0, 0.0, 0.0, 0.0, // 绿色通道
            0.0, 0.0, 0.0, 0.0, 0.0, // 蓝色通道
            0.0, 0.0, 0.0, 1.0, 0.0  // 透明通道
        ]

        let redFilter = CIFilter(name: "CIColorMatrix")!
        redFilter.setValue(lciImage, forKey: kCIInputImageKey)
        redFilter.setValue(CIVector(values: redColorMatrix, count: redColorMatrix.count), forKey: "inputRVector")
        
        let blueFilter = CIFilter(name: "CIColorMatrix")!
        blueFilter.setValue(rciImage, forKey: kCIInputImageKey)
        blueFilter.setValue(CIVector(values: blueColorMatrix, count: blueColorMatrix.count), forKey: "inputBVector")
        var lastImg:CIImage? = nil
        // 获取处理后的图像
        if let redOutputImage = redFilter.outputImage,
           let blueOutputImage = blueFilter.outputImage {
            
            let compositeFilter = CIFilter(name: "CIScreenBlendMode")!
            compositeFilter.setValue(redOutputImage, forKey: kCIInputImageKey)
            compositeFilter.setValue(blueOutputImage, forKey: kCIInputBackgroundImageKey)
            
            let sharpenedFilter = CIFilter(name: "CISharpenLuminance")!
            sharpenedFilter.setValue(compositeFilter.outputImage, forKey: kCIInputImageKey)
            sharpenedFilter.setValue(2, forKey: kCIInputSharpnessKey)
            lastImg = sharpenedFilter.outputImage!
        }
        return lastImg!
    }
    
    
    //将两张图片合成一张图片 SBS
     func joinImages_sbs( left:UIImage, right:UIImage,imgWidth:CGFloat,imgHeight:CGFloat) -> CIImage {
        let newImageSize = CGSize(width:imgWidth, height: imgHeight);
        UIGraphicsBeginImageContextWithOptions(newImageSize, false, 1);
        left.draw(in: CGRect(x:0, y:0, width:imgWidth/2, height:imgHeight))
        right.draw(in: CGRect(x:imgWidth/2, y:0, width:imgWidth/2, height:imgHeight))
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext();

         
        let ci = CIImage(cgImage: image.cgImage!)
        return ci
    }
    
    //FSBS
     func joinImages_fsbs( left:UIImage, right:UIImage,imgWidth:CGFloat,imgHeight:CGFloat) -> CIImage {
        let newImageSize = CGSize(width:imgWidth, height: imgHeight);//在播放过程中，务必保证宽、高尺寸不变
        UIGraphicsBeginImageContextWithOptions(newImageSize, false, 1);
        left.draw(in: CGRect(x:0, y:imgHeight/4, width:imgWidth/2, height:imgHeight/2))
        right.draw(in: CGRect(x:imgWidth/2, y:imgHeight/4, width:imgWidth/2, height:imgHeight/2))
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext();

         
        let ci = CIImage(cgImage: image.cgImage!)
        return ci
    }
    
    
    //将两张图片合成一张图片 OU
     func joinImages_ou( left:UIImage, right:UIImage,imgWidth:CGFloat,imgHeight:CGFloat) -> CIImage {
        let newImageSize = CGSize(width:imgWidth, height: imgHeight);
        UIGraphicsBeginImageContextWithOptions(newImageSize, false, 1);
        left.draw(in: CGRect(x:0, y:0, width:imgWidth, height:imgHeight/2))
        right.draw(in: CGRect(x:0, y:imgHeight/2, width:imgWidth, height:imgHeight/2))
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext();
        
        let ci = CIImage(cgImage: image.cgImage!)
        return ci
    }
    
    //将两张图片合成一张图片
    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: left.size.height);
        UIGraphicsBeginImageContextWithOptions(newImageSize, false, 1);
        left.draw(in: CGRect(x:0, y:imageHeight/2, width:imageWidth/2, height:imageHeight))
        right.draw(in: CGRect(x:imageWidth/2, y:imageHeight/2, width:imageWidth/2, height:imageHeight))
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext();
        
        let ci = CIImage(cgImage: image.cgImage!)
        return ci
    }
}

