import UIKit
import Flutter

@objc class SceneDelegate: FlutterSceneDelegate {
  var flutterViewController: FlutterViewController?

  override func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = scene as? UIWindowScene else { return }

    window = UIWindow(windowScene: windowScene)

    let flutterVC = FlutterViewController()
    flutterViewController = flutterVC

    window?.rootViewController = flutterVC
    window?.makeKeyAndVisible()

    registerSceneLifeCycle(with: flutterVC.engine)

    super.scene(scene, willConnectTo: session, options: connectionOptions)
  }

  override func sceneDidDisconnect(_ scene: UIScene) {
    if let flutterVC = flutterViewController {
      self.unregisterSceneLifeCycle(with: flutterVC.engine)
    }
    super.sceneDidDisconnect(scene)
  }
}
