import Flutter
import UIKit
import UMCommon

public class FlutterUmengPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "flutter_umeng", binaryMessenger: registrar.messenger())
    let instance = FlutterUmengPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    switch call.method {
    case "initCommon":

     guard let args = call.arguments as? [String: Any],
           let appKey = args["appKey"] as? String {
        initialize(appKey:appKey)
        return
     }
        
     print("ios友盟初始化失败 没接收到appKey")
     break

    case "setPolicyGrantResult":
      // initialize()
      break
    default:
      result(FlutterMethodNotImplemented)
    }
    result(true)
  }

  // 初始化
    public func initialize(appKey: String) {
    //设置AppKey，初始化友盟所有组件产品，开发者在友盟官网申请的appkey
    UMConfigure.initWithAppkey(appKey, channel: "AppStore")
    print("ios友盟初始化")
  }
}
