import UIKit
import Flutter
import OpenTok

//  Created by Mohammad Aljammal & Elham Rababah on 24/06/20.
//  Copyright © 2020 Cloud. All rights reserved.

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    
    
    let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
    let videoCallChannel = FlutterMethodChannel(name: "Dr.cloudSolution/videoCall",
                                              binaryMessenger: controller.binaryMessenger)
    videoCallChannel.setMethodCallHandler({
      (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
        switch call.method {
          case "openVideoCall":
            do {
                let arguments = call.arguments as? NSDictionary
                let kApiKey = arguments!["kApiKey"] as? String
                let kSessionId = arguments!["kSessionId"] as? String
                let kToken = arguments!["kToken"] as? String
                let appLang = arguments!["appLang"] as? String
                self.openVideoChat(result: result,kApiKey: kApiKey!,kSessionId:kSessionId!,kToken: kToken!, appLang: appLang!)
            }
            default:
            result(FlutterMethodNotImplemented)
          }
    })
    
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
    
    
    
    private func openVideoChat(result: FlutterResult,kApiKey: String, kSessionId: String,kToken: String,appLang:String) {
           
               let storyboard = UIStoryboard(name: "Main", bundle: nil)
                          let identifier = "ViewControllerNav"
                          let navVC = storyboard.instantiateViewController(withIdentifier: identifier) as! UINavigationController
                          let videoVC = navVC.viewControllers.first as! ViewController
                          videoVC.kApiKey=kApiKey
                          videoVC.kSessionId=kSessionId
                          videoVC.kToken=kToken
                          videoVC.navigationController?.setNavigationBarHidden(true, animated: false)
                          navVC.modalPresentationStyle = .fullScreen
                          window.rootViewController?.present(navVC, animated: true, completion: nil)
           
              
          }
}
