//import Flutter
//import UIKit
//
//@UIApplicationMain
//@objc class AppDelegate: FlutterAppDelegate {
//  override func application(
//    _ application: UIApplication,
//    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
//  ) -> Bool {
//    GeneratedPluginRegistrant.register(with: self)
//      
//    weak var penguin = self.registrar(forPlugin: "penguin")
//    let penguinFactory = PenguinViewFactory(messenger: penguin!.messenger())
//    self.registrar(forPlugin: "PenguinView")!.register(penguinFactory, withId: "penguin_lib")
//    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
//  }
//}



import Flutter
import UIKit

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    var flutterEngine: FlutterEngine?
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        GeneratedPluginRegistrant.register(with: self)
        let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
        let channel = FlutterMethodChannel(name: "com.aamir/launch_penguin_ui",
                                           binaryMessenger: controller.binaryMessenger)
        channel.setMethodCallHandler { [weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) in
            if let arguments = call.arguments as? [String: Any],
               let storyboardName = arguments["storyboardName"] as? String {
                if  call.method == "launchPenguin"{
                    self?.launchPenguinView(arguments: arguments, result: result)
                }
                else {
                    result(FlutterMethodNotImplemented)
                }
            } else {
                result(FlutterError(code: "INVALID_ARGUMENT", message: "Storyboard name is required", details: nil))
            }
            
        }
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    
    private func launchPenguinView(arguments: [String: Any], result: @escaping FlutterResult) {
          guard let controller = window?.rootViewController as? FlutterViewController else {
              result(FlutterError(code: "NO_CONTROLLER", message: "No Flutter view controller found", details: nil))
              return
          }
          
          let penguinView = PenguinView(
              frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height),
              viewIdentifier: 0,
              arguments: arguments,
              binaryMessenger: controller.binaryMessenger
          )
          
        let penguinUIView = penguinView.view()
          penguinUIView.frame = controller.view.bounds
          penguinUIView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
          
          controller.view.addSubview(penguinUIView)
          
          result(nil) // Call result to indicate the method was successfully handled
      }
}


