package com.example.doctor_app_flutter

import android.app.Activity
import android.content.Intent
import androidx.annotation.NonNull
import com.example.doctor_app_flutter.Model.GetSessionStatusModel
import com.example.doctor_app_flutter.Model.SessionStatusModel
import com.example.doctor_app_flutter.ui.VideoCallActivity
import com.google.gson.GsonBuilder
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity : FlutterFragmentActivity(), MethodChannel.MethodCallHandler {

    private val CHANNEL = "Dr.cloudSolution/videoCall"
    private var result: MethodChannel.Result? = null
    private var call: MethodCall? = null
    private val LAUNCH_VIDEO: Int = 1


    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler(this)
    }


    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {

        this.result = result
        this.call = call

        if (call.method == "openVideoCall") {
            val apiKey = call.argument<String>("kApiKey")
            val sessionId = call.argument<String>("kSessionId")
            val token = call.argument<String>("kToken")
            val appLang = call.argument<String>("appLang")
            val baseUrl = call.argument<String>("baseUrl")

            // Session Status model
            val VC_ID = call.argument<Int>("VC_ID")
            val tokenID = call.argument<String>("TokenID")
            val generalId = call.argument<String>("generalId")
            val doctorId = call.argument<Int>("DoctorId")

            val sessionStatusModel = GetSessionStatusModel(VC_ID, tokenID, generalId, doctorId)


            openVideoCall(apiKey, sessionId, token, appLang, baseUrl, sessionStatusModel)

        } else {
            result.notImplemented()
        }
    }

    private fun openVideoCall(apiKey: String?, sessionId: String?, token: String?, appLang: String?, baseUrl: String?, sessionStatusModel: GetSessionStatusModel) {
        // val videoCallActivity = VideoCallActivity()

        val intent = Intent(this, VideoCallActivity::class.java)
        intent.putExtra("apiKey", apiKey)
        intent.putExtra("sessionId", sessionId)
        intent.putExtra("token", token)
        intent.putExtra("appLang", appLang)
        intent.putExtra("baseUrl", baseUrl)
        intent.putExtra("sessionStatusModel", sessionStatusModel)
        startActivityForResult(intent, LAUNCH_VIDEO)

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        var asd = "";
        if (requestCode == LAUNCH_VIDEO) {
            if (resultCode == Activity.RESULT_OK) {
                val result  : SessionStatusModel? = data?.getParcelableExtra("sessionStatusNotRespond")
                val callResponse : HashMap<String, String> = HashMap()

                val sessionStatus : HashMap<String, String> = HashMap()
                val gson = GsonBuilder().serializeNulls().create()

                callResponse["callResponse"] = "CallNotRespond"
                val jsonRes = gson.toJson(result)
                callResponse["sessionStatus"] = jsonRes

                this.result?.success(callResponse)
            }
            if (resultCode == Activity.RESULT_CANCELED) {
                val callResponse : HashMap<String, String> = HashMap()
                callResponse["callResponse"] = "CallEnd"

                result?.success(callResponse)
            }
        }
    }


}
