//
//  SpatialImageConvertor.swift
//  SwiftProject
//
//  Created by aaa on 2024/3/7.
//

import Foundation
import UIKit
class SpatialImageConvertor {
    //将空间照片转换为hsbs格式
    public class func convertToHSBS(imageData:Data) -> UIImage? {
        let (left_uiimage,right_uiimage) = SpatialImageConvertor.getLeftAndRightImgFrom(imageData: imageData)
        let newpb = SpatialImageConvertor.joinImages_sbs( left: left_uiimage!, right:right_uiimage! ,imgWidth: left_uiimage!.size.width,imgHeight:left_uiimage!.size.height)
        let lastImg = SpatialImageConvertor.convertCIImageToUIImage(ciImage: newpb)!
        return lastImg
    }
    
    //将空间照片转换为fsbs格式
    public class func convertToFSBS(imageData:Data) -> UIImage? {
        let (left_uiimage,right_uiimage) = SpatialImageConvertor.getLeftAndRightImgFrom(imageData: imageData)
        let newpb = SpatialImageConvertor.joinImages_sbs( left: left_uiimage!, right:right_uiimage! ,imgWidth: left_uiimage!.size.width*2,imgHeight:left_uiimage!.size.height)
        let lastImg = SpatialImageConvertor.convertCIImageToUIImage(ciImage: newpb)!
        return lastImg
    }
    
    //将空间照片转换为hou格式
    public class func convertToHOU(imageData:Data) -> UIImage? {
        let (left_uiimage,right_uiimage) = SpatialImageConvertor.getLeftAndRightImgFrom(imageData: imageData)
        let newpb = SpatialImageConvertor.joinImages_ou( left: left_uiimage!, right:right_uiimage! ,imgWidth: left_uiimage!.size.width,imgHeight:left_uiimage!.size.height)
        let lastImg = SpatialImageConvertor.convertCIImageToUIImage(ciImage: newpb)!
        return lastImg
    }
    
    //将空间照片转换为fou格式
    public class func convertToFOU(imageData:Data) -> UIImage? {
        let (left_uiimage,right_uiimage) = SpatialImageConvertor.getLeftAndRightImgFrom(imageData: imageData)
        let newpb = SpatialImageConvertor.joinImages_ou( left: left_uiimage!, right:right_uiimage! ,imgWidth: left_uiimage!.size.width,imgHeight:left_uiimage!.size.height*2)
        let lastImg = SpatialImageConvertor.convertCIImageToUIImage(ciImage: newpb)!
        return lastImg
    }
    
    
    
    class func getLeftAndRightImgFrom(imageData:Data) -> (left_uiimage:UIImage?,right_uiimage:UIImage?){
        guard let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil) else {
            return (nil,nil)
        }
        let frameCount = CGImageSourceGetCount(imageSource)
        var frames: [CGImage] = []
        for index in 0..<frameCount {
            guard let frameImage = CGImageSourceCreateImageAtIndex(imageSource, index, nil) else {
                continue
            }
            frames.append(frameImage)
        }
        
        if(frames.count < 2){return (nil,nil) }
        let lciImage = CIImage(cgImage: frames.first!)
        let rciImage = CIImage(cgImage: frames[1])
        
        let left_uiimage =  UIImage(ciImage: lciImage )
        
        let left_uiimage_new = left_uiimage.getUpImg()
        
        let right_uiimage =  UIImage(ciImage: rciImage )
        let right_uiimage_new = right_uiimage.getUpImg()
        return (left_uiimage_new,right_uiimage_new)
    }
    

    class func convertCIImageToCGImage(ciImage:CIImage) -> CGImage {
        let ciContext = CIContext.init()
        let cgImage:CGImage = ciContext.createCGImage(ciImage, from: ciImage.extent)!
        return cgImage
    }
    
   class func convertCIImageToUIImage(ciImage: CIImage) -> UIImage? {
        let context = CIContext(options: nil)
        if let cgImage = context.createCGImage(ciImage, from: ciImage.extent) {
            let uiImage = UIImage(cgImage: cgImage)
            return uiImage
        }
        return nil
    }
    
    
    //将两张图片合成一张图片 SBS
    class 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
    }
    
    //将两张图片合成一张图片 OU
    class 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
    }
}
