import UIKit
import Flutter
import GoogleMaps

var userNotificationCenterDelegate:UNUserNotificationCenterDelegate? = nil

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    let locationManager = CLLocationManager()
    var flutterViewController:MainFlutterVC!
    
    override func application( _ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        GMSServices.provideAPIKey("AIzaSyCmevVlr2Bh-c8W1VUzo8gt8JRY7n5PANw")
        GeneratedPluginRegistrant.register(with: self)
        
        initializePlatformChannels()
        
        if let _ = launchOptions?[.location] {
            HMG_Geofence.initGeofencing()
        }
        
        UNUserNotificationCenter.current().delegate = self
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    
    func initializePlatformChannels(){
        if let mainViewController = window.rootViewController as? MainFlutterVC{ // platform initialization suppose to be in foreground
            flutterViewController = mainViewController
            HMGPlatformBridge.initialize(flutterViewController: flutterViewController)
            OpenTokPlatformBridge.initialize(flutterViewController: flutterViewController, registrar: self.registrar(forPlugin: "open-tok"))
            
        }else if let mainViewController = initialViewController(){  // platform initialization suppose to be in background
            flutterViewController = mainViewController
            HMGPlatformBridge.initialize(flutterViewController: flutterViewController)
            OpenTokPlatformBridge.initialize(flutterViewController: flutterViewController, registrar: self.registrar(forPlugin: "open-tok"))
        }
    }
    
    
    func initialViewController() -> MainFlutterVC?{
        return nil //UIStoryboard(name: "Main", bundle: .main).instantiateInitialViewController() as? MainFlutterVC
    }
}

extension AppDelegate{
    override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        if (notification.request.content.categoryIdentifier == HmgLocalNotificationCategoryIdentifier){
            completionHandler([.alert,.sound])
        }else{
            super.userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)
        }
    }
}


/*
 let dart = FlutterDartProject(precompiledDartBundle: .main)
      let engine = FlutterEngine(name: "com.hmg.cs", project: dart, allowHeadlessExecution: true)
      if engine.run(){
          flutterMethodChannel = FlutterMethodChannel(name: "HMG-Platform-Bridge", binaryMessenger: engine.binaryMessenger)
          
          Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { (timer) in
              FlutterText.with(key: "alreadyConnectedHmgNetwork"){ localized in
                  print(localized)
              }
          }
      }
 */
