import Flutter
import UIKit

///
let kAMapKey = "3ac08e5e14df9d7a52e98d40e21a0189";


@main
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {

      GeneratedPluginRegistrant.register(with: self)

      AMapNavSDKManager.shared().config(withKey: kAMapKey)
      
      // 注册平台视图工厂
      let registrar = self.registrar(forPlugin: "NativeFirstPagePlugin")
      
      let controller = window?.rootViewController as! FlutterViewController
      let nativeViewFactory = NativeViewFactory(messenger: controller.binaryMessenger)
      
      registrar?.register(
        nativeViewFactory,
          withId: "NativeFirstPage"
      )
      
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
      
  }
    
    
    
}




// 创建视图工厂
class NativeViewFactory: NSObject, FlutterPlatformViewFactory {
    private var messenger: FlutterBinaryMessenger

    init(messenger: FlutterBinaryMessenger) {
        self.messenger = messenger
        super.init()
    }
    
    func create(
        withFrame frame: CGRect,
        viewIdentifier viewId: Int64,
        arguments args: Any?
    ) -> FlutterPlatformView {
        return NativeFlutterView(frame: frame, viewId: viewId, args: args)
    }
    
    func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
        return FlutterStandardMessageCodec.sharedInstance()
    }
}

class NativeFlutterView: NSObject, FlutterPlatformView {
    private var _view: UIView
    private var _nativeVC: UIViewController
    
    init(frame: CGRect, viewId: Int64, args: Any?) {
        // 创建原生的 ViewController 视图
        let nativeVC = AMapNavSDKManager.shared().targetVC;
//        let nativeVC = NativeFirstPage();
        
        self._nativeVC = nativeVC
        
        print("---frame: \(frame)");
        
        _view = nativeVC.view
        _view.isUserInteractionEnabled = true
        _view.frame = CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))
        super.init()
    }
    
    func view() -> UIView {
        return _view
    }
}

