//
//  PQTextFilter.swift
//  BFFramework
//
//  Created by ak on 2021/10/9.
//  功能：创建文本fitler

import Foundation
import UIKit

import OpenGLES

import AVFoundation
 
open class PQTextFilter: PQBaseFilter {
 
 
    // 字幕gpu texture
    var subTitleTexture: GLuint = 0

    var subtitleImage: UIImage?
    deinit {
        FilterLog(message: "字幕析构 ")
        clearData()
    }

    init(sticker: PQEditVisionTrackMaterialsModel) {
        super.init(fragmentShader: AlphaPassthroughFragmentShader, numberOfInputs: 1)
        stickerInfo = sticker
        subTitleTexture = 0
        
//        BFLog("\(stickerInfo.materialPosition)", message: 2)
 
        createTexture()
        
    }
 
    // 清空数据
    public override func clearData() {
        super.clearData()

        subtitleImage = nil
        glDeleteTextures(1, &subTitleTexture)
        subTitleTexture = 0

    }
  
    func createTexture() {
        DispatchQueue.main.async {[weak self] in
 
            autoreleasepool {
                if(self == nil){
                    BFLog(message: "self is null error!!!!!")
                    return
                }
                let subtitleLab =
                UILabel.init(frame: CGRect(x: 0, y: 0, width: self?.stickerInfo?.materialPosition?.width ?? 0, height: self?.stickerInfo?.materialPosition?.height ?? 0))
             
                subtitleLab.numberOfLines = 2
                subtitleLab.lineBreakMode = .byWordWrapping

                FilterLog(message: "字幕初始化时大小 \(subtitleLab.frame)")
                subtitleLab.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5)
                subtitleLab.alpha = 1
          
                let paraph = NSMutableParagraphStyle()
                paraph.lineSpacing = 0
                paraph.alignment = .center
 
                
                let attributedText: NSMutableAttributedString = NSMutableAttributedString(string: (self?.stickerInfo?.subtitleInfo?.text ?? ""), attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: CGFloat(self?.stickerInfo?.subtitleInfo?.fontSize ?? 0)), NSAttributedString.Key.foregroundColor:UIColor.white,NSAttributedString.Key.kern: 0,NSAttributedString.Key.paragraphStyle:paraph])
                subtitleLab.attributedText = attributedText
 
                let size: CGSize = subtitleLab.bounds.size
                //如果传0,则这个参数会用设备的scale,
                UIGraphicsBeginImageContextWithOptions(size, false, 2)
                if let context = UIGraphicsGetCurrentContext() {
                    subtitleLab.layer.render(in: context)
                }

                self?.subtitleImage = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
                UIGraphicsEndImageContext()
                
                FilterLog(message: "合成图片大小: \(String(describing: self?.subtitleImage?.size))")

            }
 
            if self?.subtitleImage?.cgImage != nil, self?.subTitleTexture == 0 {
                sharedImageProcessingContext.runOperationSynchronously {
                    autoreleasepool {
                        self?.subTitleTexture = PQGPUImageTools.setupTexture(image: (self?.subtitleImage!.cgImage!)!)
                    }
                }
            }
        }
    }
 
    override open func renderFrame() {
        let inputFramebuffer: Framebuffer = inputFramebuffers[0]!
        let inputSize = inputFramebuffer.sizeForTargetOrientation(.portrait)

        let currTime = CMTimeGetSeconds(CMTime(value: inputFramebuffer.timingStyle.timestamp!.value, timescale: inputFramebuffer.timingStyle.timestamp!.timescale))
        FilterLog(message: "subtitle 当前时间: \(currTime)")

        // 原有画布
        renderFramebuffer = sharedImageProcessingContext.framebufferCache.requestFramebufferWithProperties(orientation: .portrait, size: inputSize, stencil: false)

        let textureProperties = InputTextureProperties(textureCoordinates: inputFramebuffer.orientation.rotationNeededForOrientation(.portrait).textureCoordinates(), texture: inputFramebuffer.texture)

        renderFramebuffer.activateFramebufferForRendering()
        clearFramebufferWithColor(backgroundColor)
        renderQuadWithShader(shader, uniformSettings: uniformSettings,
                             vertexBufferObject: sharedImageProcessingContext.standardImageVBO, inputTextures: [textureProperties])
        releaseIncomingFramebuffers()

        if subTitleTexture != 0 {
            FilterLog(message: "subTitleTexture 有值可以正常显示")
            let texturePropertiesimagetwo = InputTextureProperties(textureCoordinates: inputFramebuffer.orientation.rotationNeededForOrientation(.portrait).textureCoordinates(), texture: subTitleTexture)
 
            let verticesPoint = PQGPUImageTools.computeVertices(viewSize: CGSize.init(width: CGFloat(inputSize.width), height: CGFloat(inputSize.height)), _bounds: CGRect.init(x: stickerInfo?.materialPosition?.x ?? 0, y:  stickerInfo?.materialPosition?.y ?? 0, width: stickerInfo?.materialPosition?.width ?? 0, height: stickerInfo?.materialPosition?.height ?? 0))
            
            renderQuadWithShader(shader,
                                 uniformSettings: uniformSettings,
                                 vertexBufferObject: PQGPUImageTools.NXGenerateVBO(for: verticesPoint),
                                 inputTextures: [texturePropertiesimagetwo])

            releaseIncomingFramebuffers()
        }else{
            FilterLog(message: "subTitleTexture is nil!!!!!")
        }
    }

    override public func transmitPreviousImage(to _: ImageConsumer, atIndex _: UInt) {
        print("this is running")
    }
}
 
