//
//  HMGPlatformBridge.swift
//  Runner
//
//  Created by ZiKambrani on 14/12/2020.
//

import UIKit
import NetworkExtension
import SystemConfiguration.CaptiveNetwork


fileprivate var openTok:OpenTok?

class OpenTokPlatformBridge : NSObject{
    private var methodChannel:FlutterMethodChannel? = nil
    private var mainViewController:MainFlutterVC!
    private static var shared_:OpenTokPlatformBridge?

    class func initialize(flutterViewController:MainFlutterVC, registrar:FlutterPluginRegistrar?){
        shared_ = OpenTokPlatformBridge()
        shared_?.mainViewController = flutterViewController

        shared_?.openChannel()
        openTok = OpenTok(mainViewController: flutterViewController, registrar: registrar)
    }

    func shared() -> OpenTokPlatformBridge{
        assert((OpenTokPlatformBridge.shared_ != nil), "OpenTokPlatformBridge is not initialized, call initialize(mainViewController:MainFlutterVC) function first.")
        return OpenTokPlatformBridge.shared_!
    }

    private func openChannel(){
        methodChannel = FlutterMethodChannel(name: "OpenTok-Platform-Bridge", binaryMessenger: mainViewController.binaryMessenger)
        methodChannel?.setMethodCallHandler { (call, result) in
            print("Called function \(call.method)")

            switch(call.method) {
            case "initSession":
                openTok?.initSession(call: call, result: result)

            case "swapCamera":
                openTok?.swapCamera(call: call, result: result)

            case "toggleAudio":
                openTok?.toggleAudio(call: call, result: result)
                
            case "toggleVideo":
                openTok?.toggleVideo(call: call, result: result)
                
            case "hangupCall":
                openTok?.hangupCall(call: call, result: result)

            default:
                result(FlutterMethodNotImplemented)
            }

            print("")
        }
    }
}
