import UIKit
import PushKit
import Flutter
import Firebase
import flutter_callkit_incoming
import flutter_local_notifications

// PKPushRegistryDelegate
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
     FirebaseApp.configure()
     FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { (registry) in
           GeneratedPluginRegistrant.register(with: registry)
       }
//       if #available(iOS 10.0, *) {
//         UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
//       }
    GeneratedPluginRegistrant.register(with: self)

        //Setup VOIP
//        let mainQueue = DispatchQueue.main
//        let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)
//        voipRegistry.delegate = self
//        voipRegistry.desiredPushTypes = [PKPushType.voIP]

        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    // Handle updated push credentials
//    func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, for type: PKPushType) {
//        print(credentials.token)
//        let deviceToken = credentials.token.map { String(format: "%02x", $0) }.joined()
//        //Save deviceToken to your server
//        SwiftFlutterCallkitIncomingPlugin.sharedInstance?.setDevicePushTokenVoIP(deviceToken)
//    }

//    func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {
//        print("didInvalidatePushTokenFor")
//        SwiftFlutterCallkitIncomingPlugin.sharedInstance?.setDevicePushTokenVoIP("")
//    }

//    // Handle incoming pushes
//    func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
//        print("didReceiveIncomingPushWith")
//        guard type == .voIP else { return }
//        print(payload.dictionaryPayload)
////         let id = payload.dictionaryPayload["id"] as? String ?? ""
////         let nameCaller = payload.dictionaryPayload["nameCaller"] as? String ?? ""
////         let handle = payload.dictionaryPayload["handle"] as? String ?? ""
//        let isVideo = payload.dictionaryPayload["isVideo"] as? Bool ?? false
////
////
//         let data = flutter_callkit_incoming.Data(id: "1", nameCaller: "Mohemm", handle: "handle", type: isVideo ? 1 : 0)
////         data.extra = ["user": "abc@123", "platform": "ios"]
////         data.iconName = "Mohemm"
//        SwiftFlutterCallkitIncomingPlugin.sharedInstance?.showCallkitIncoming(data, fromPushKit: true)
//    }


}
