#if canImport(UIKit)
import SwiftUI
import VNCCore
import CoreGraphics

struct FramebufferView: UIViewRepresentable {
    let controller: SessionController
    let inputMode: InputMode
    let selectedScreen: RemoteScreen?
    @Binding var trackpadCursor: CGPoint
    @Binding var showSoftwareKeyboard: Bool

    func makeUIView(context: Context) -> FramebufferUIView {
        let view = FramebufferUIView()
        view.controller = controller
        view.inputMode = inputMode
        view.selectedScreen = selectedScreen
        view.trackpadCursorNormalized = trackpadCursor
        view.onTrackpadCursorChanged = { [binding = $trackpadCursor] new in
            binding.wrappedValue = new
        }
        view.onKeyboardDismissed = { [binding = $showSoftwareKeyboard] in
            if binding.wrappedValue { binding.wrappedValue = false }
        }
        return view
    }

    func updateUIView(_ uiView: FramebufferUIView, context: Context) {
        uiView.controller = controller
        uiView.inputMode = inputMode
        uiView.selectedScreen = selectedScreen
        if uiView.trackpadCursorNormalized != trackpadCursor {
            uiView.trackpadCursorNormalized = trackpadCursor
        }
        uiView.apply(image: controller.currentImage,
                     framebufferSize: framebufferSize)
        uiView.setSoftwareKeyboardVisible(showSoftwareKeyboard)
        // Touch the revision so SwiftUI re-runs us when frames arrive
        _ = controller.imageRevision
    }

    private var framebufferSize: CGSize {
        if let size = controller.framebufferSize {
            return CGSize(width: CGFloat(size.width), height: CGFloat(size.height))
        }
        return .zero
    }
}
#else
import SwiftUI
import VNCCore
import CoreGraphics

struct FramebufferView: View {
    let controller: SessionController
    let inputMode: InputMode
    let selectedScreen: RemoteScreen?
    @Binding var trackpadCursor: CGPoint
    @Binding var showSoftwareKeyboard: Bool

    var body: some View { Color.black }
}
#endif
