//
//  MainFlutterVC.swift
//  Runner
//
//  Created by ZiKambrani on 25/03/1442 AH.
//

import UIKit
import Flutter
import NetworkExtension
import SystemConfiguration.CaptiveNetwork

class MainFlutterVC: FlutterViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        flutterMethodChannel?.setMethodCallHandler { (methodCall, result) in
//            
//            if methodCall.method == "connectHMGInternetWifi"{
//                self.connectHMGInternetWifi(methodCall:methodCall, result: result)
//                
//            }else if methodCall.method == "connectHMGGuestWifi"{
//                self.connectHMGGuestWifi(methodCall:methodCall, result: result)
//                
//            }else if methodCall.method == "isHMGNetworkAvailable"{
//                self.isHMGNetworkAvailable(methodCall:methodCall, result: result)
//                
//            }else if methodCall.method == "registerHmgGeofences"{
//                self.registerHmgGeofences(result: result)
//            }
//            
//            print("")
//        }
//        
//        FlutterText.with(key: "errorConnectingHmgNetwork") { (localized) in
//            print(localized)
//        }
        
    }
    
    
    // Connect HMG Wifi and Internet
    func connectHMGInternetWifi(methodCall:FlutterMethodCall ,result: @escaping FlutterResult){
        
        guard let pateintId = (methodCall.arguments as? [Any])?.first as? String
            else { return assert(true, "Missing or invalid arguments (Must have one argument 'String at 0'") }
        
        
        HMG_Internet.shared.connect(patientId: pateintId) { (status, message) in
            result(status ? 1 : 0)
            if status{
                self.showMessage(title:"Congratulations", message:message)
            }else{
                self.showMessage(title:"Ooops,", message:message)
            }
        }
    }
    
    // Connect HMG-Guest for App Access
    func connectHMGGuestWifi(methodCall:FlutterMethodCall ,result: @escaping FlutterResult){
        HMG_GUEST.shared.connect() { (status, message) in
            result(status ? 1 : 0)
            if status{
                self.showMessage(title:"Congratulations", message:message)
            }else{
                self.showMessage(title:"Ooops,", message:message)
            }
        }
    }
    
    func isHMGNetworkAvailable(methodCall:FlutterMethodCall ,result: @escaping FlutterResult) -> Bool{
        guard let ssid = methodCall.arguments as? String else {
            assert(true, "Missing or invalid arguments (Must have one argument 'String at 0'")
            return false
        }
        
        let queue = DispatchQueue.init(label: "com.hmg.wifilist")
        NEHotspotHelper.register(options: nil, queue: queue) { (command) in
            print(command)
            
            if(command.commandType == NEHotspotHelperCommandType.filterScanList) {
                if let networkList = command.networkList{
                    for network in networkList{
                        print(network.ssid)
                    }
                }
            }
        }
        return false
        
    }
    
    
    // Message Dailog
    func showMessage(title:String, message:String){
        DispatchQueue.main.async {
            let alert = UIAlertController(title: title, message: message, preferredStyle: .alert )
            alert.addAction(UIAlertAction(title: "OK", style: .destructive, handler: nil))
            self.present(alert, animated: true) {
                
            }
        }
    }
    
    // Register Geofence
    func registerHmgGeofences(result: @escaping FlutterResult){
        flutterMethodChannel?.invokeMethod("getGeofencePreferenceKey", arguments: nil){ geoFencesJsonString in
            if let jsonString = geoFencesJsonString as? String{
                let allZones = GeoZoneModel.list(from: jsonString)
                HMG_Geofence().register(geoZones: allZones)
                
            }else{
            }
        }
    }
    
}
