import UIKit
import SwiftUI
import AVFoundation

/// UIKit 视图：以 AVSampleBufferDisplayLayer 为 backing layer，
/// 用于渲染"自编码"模式下的 H.264 硬解画面（对应 Android 端的 SurfaceView）。
final class SampleBufferDisplayView: UIView {

    override class var layerClass: AnyClass {
        AVSampleBufferDisplayLayer.self
    }

    var sampleBufferLayer: AVSampleBufferDisplayLayer {
        layer as! AVSampleBufferDisplayLayer
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        sampleBufferLayer.videoGravity = .resizeAspect
        backgroundColor = .black
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        sampleBufferLayer.videoGravity = .resizeAspect
        backgroundColor = .black
    }
}

/// SwiftUI 包装
struct SelfCodecDisplayViewRepresentable: UIViewRepresentable {
    let view: SampleBufferDisplayView

    func makeUIView(context: Context) -> SampleBufferDisplayView {
        view
    }

    func updateUIView(_ uiView: SampleBufferDisplayView, context: Context) {}
}
