package com.cloud.hmg_patient_app.whatsapp

import com.ejada.hmg.MainActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import java.lang.ref.WeakReference

class WhatsAppOtpPlatformBridge(
    private var flutterEngine: FlutterEngine,
    private var mainActivity: MainActivity
) {


    private lateinit var channel: MethodChannel

    companion object {
        private const val CHANNEL = "whats_app_otp"
        var result: MethodChannel.Result? = null
    }

    fun invoke() {
        channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
        channel.setMethodCallHandler { call: MethodCall, result: MethodChannel.Result ->
            when (call.method) {
                "isWhatsAppInstalled" -> {
                    val isAppInstalled =
                        WhatsApp.isWhatsAppInstalled(WeakReference(mainActivity))
                    result.success(isAppInstalled)
                }

                "performHandShake" -> {
                    WhatsApp.performHandShake(WeakReference(mainActivity))
                }


                "startListening" -> {
                    WhatsAppOtpPlatformBridge.result = result
                }

                else -> {
                    result.notImplemented()
                }

            }
        }
    }
}