package uz.uzinfocom.myid

import android.content.Context
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import uz.myid.android.sdk.capture.MyIdClient

class MyIdPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {

    private lateinit var myIdSdk: MyIdSdk
    private lateinit var activityListener: MyIdSdkActivityListener
    private lateinit var methodChannel: MethodChannel
    private lateinit var applicationContext: Context
    private lateinit var pluginBinding: ActivityPluginBinding

    private val myIdClient = MyIdClient()

    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        onAttachedToEngine(
            flutterPluginBinding.applicationContext,
            flutterPluginBinding.binaryMessenger
        )
    }

    companion object {
        @JvmStatic
        fun registerWith(registrar: Registrar) {
            val instance = MyIdPlugin()
            instance.onAttachedToEngine(registrar.context(), registrar.messenger())
            instance.myIdSdk.setActivity(registrar.activity())
            registrar.addActivityResultListener(instance.activityListener)
        }
    }

    override fun onMethodCall(call: MethodCall, result: Result) {
        if (call.method == "startSdk") {
            val config = call.arguments as HashMap<*, *>
            @Suppress("UNCHECKED_CAST")
            myIdSdk.start(config["config"] as? HashMap<String, Any?>, result)
        } else {
            result.notImplemented()
        }
    }

    private fun onAttachedToEngine(context: Context, messenger: BinaryMessenger) {
        applicationContext = context
        methodChannel = MethodChannel(messenger, "myid_uz")
        methodChannel.setMethodCallHandler(this)
        activityListener = MyIdSdkActivityListener(myIdClient)
        myIdSdk = MyIdSdk(myIdClient, activityListener)
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        methodChannel.setMethodCallHandler(null)
    }

    override fun onDetachedFromActivity() {
        myIdSdk.setActivity(null)
        pluginBinding.removeActivityResultListener(activityListener)
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    }

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        myIdSdk.setActivity(binding.activity)
        pluginBinding = binding
        pluginBinding.addActivityResultListener(activityListener)
    }

    override fun onDetachedFromActivityForConfigChanges() {
    }
}
