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 ,ICallProtocol {
   
    var result: FlutterResult?
    
    
    func sessionDone(res:Any) {
        self.result?(res)
        
    }
    func sessionNotResponded(res:Any) {
        self.result?(res)
    }
    
  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
        self.result = result
        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
                 let vC_ID = arguments!["VC_ID"] as? Int
                 let tokenID = arguments!["TokenID"] as? String
                 let generalId = arguments!["generalId"] as? String
                 let doctorId = arguments!["DoctorId"] as? Int
                let baseUrl = arguments!["baseUrl"] as? String

                self.openVideoChat(result: result,kApiKey: kApiKey!,kSessionId:kSessionId!,kToken: kToken!, appLang: appLang!, vC_ID: vC_ID!,tokenID: tokenID!,generalId: generalId!,doctorId: doctorId!, baseUrl: baseUrl!)
            }
            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, vC_ID: Int,tokenID:String,generalId:String,doctorId:Int,baseUrl: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.VC_ID = vC_ID
                          videoVC.generalid = generalId
                          videoVC.TokenID = tokenID
                          videoVC.DoctorId = doctorId
                          videoVC.baseUrl = baseUrl
                          videoVC.callBack = self
                          videoVC.navigationController?.setNavigationBarHidden(true, animated: false)
                          navVC.modalPresentationStyle = .fullScreen
                          window.rootViewController?.present(navVC, animated: true, completion: nil)
           
              
          }
}
