package com.cloudsolutions.HMGPatientApp

import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import com.cloudsolutions.HMGPatientApp.penguin.PenguinView
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import com.cloudsolutions.HMGPatientApp.PermissionManager.HostNotificationPermissionManager
import com.cloudsolutions.HMGPatientApp.PermissionManager.HostBgLocationManager
import com.cloudsolutions.HMGPatientApp.PermissionManager.HostGpsStateManager
import io.flutter.plugin.common.MethodChannel

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

    private lateinit var channel: MethodChannel

    companion object {
        private const val CHANNEL = "launch_penguin_ui"
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun create() {
//        openTok = OpenTok(mainActivity, flutterEngine)
        channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
        channel.setMethodCallHandler { call: MethodCall, result: MethodChannel.Result ->
            when (call.method) {
                "launchPenguin" -> {
                    print("the platform channel is being called")

                    if (HostNotificationPermissionManager.isNotificationPermissionGranted(mainActivity))
                    else HostNotificationPermissionManager.requestNotificationPermission(mainActivity)
                    HostBgLocationManager.requestLocationBackgroundPermission(mainActivity)
                    HostGpsStateManager.requestLocationPermission(mainActivity)
                    val args = call.arguments as Map<String, Any>?
                    Log.d("TAG", "configureFlutterEngine: $args")
                    println("args")
                    args?.let {
                        PenguinView(
                            mainActivity,
                            100,
                            args,
                            flutterEngine.dartExecutor.binaryMessenger,
                            activity = mainActivity,
                            channel
                        )
                    }
                }

                else -> {
                    result.notImplemented()
                }
            }
        }
    }

}
