package com.cloudsolutions.HMGPatientApp.penguin

import android.content.Context
import com.google.gson.Gson
import com.peng.pennavmap.PlugAndPlaySDK
import com.peng.pennavmap.connections.ApiController
import com.peng.pennavmap.interfaces.RefIdDelegate
import com.peng.pennavmap.models.TokenModel
import com.peng.pennavmap.models.postmodels.PostToken
import com.peng.pennavmap.utils.AppSharedData
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import android.util.Log


class PenguinNavigator() {

    fun navigateTo(mContext: Context, refID: String, delegate: RefIdDelegate,clientID : String,clientKey : String ) {
        val postToken = PostToken(clientID, clientKey)
        getToken(mContext, postToken, object : RefIdDelegate {
            override fun onRefByIDSuccess(PoiId: String?) {
                Log.e("navigateTo", "PoiId is+++++++ $refID")

                PlugAndPlaySDK.navigateTo(mContext, refID, object : RefIdDelegate {
                    override fun onRefByIDSuccess(PoiId: String?) {
                        Log.e("navigateTo", "PoiId 2is+++++++ $PoiId")

                        delegate.onRefByIDSuccess(refID)

                    }

                    override fun onGetByRefIDError(error: String?) {
                        delegate.onRefByIDSuccess(error)
                    }

                })


            }

            override fun onGetByRefIDError(error: String?) {
                delegate.onRefByIDSuccess(error)
            }

        })

    }

    fun getToken(mContext: Context, postToken: PostToken?, apiTokenCallBack: RefIdDelegate) {
        try {
            // Create the API call
            val purposesCall: Call<ResponseBody> = ApiController.getInstance(mContext)
                .apiMethods
                .getToken(postToken)

            // Enqueue the call for asynchronous execution
            purposesCall.enqueue(object : Callback<ResponseBody?> {
                override fun onResponse(
                    call: Call<ResponseBody?>,
                    response: Response<ResponseBody?>
                ) {
                    if (response.isSuccessful() && response.body() != null) {
                        try {
                            response.body()?.use { responseBody ->
                                val responseBodyString: String = responseBody.string() // Use `string()` to get the actual response content
                                if (responseBodyString.isNotEmpty()) {
                                    val tokenModel = Gson().fromJson(responseBodyString, TokenModel::class.java)
                                    if (tokenModel != null && tokenModel.token != null) {
                                        AppSharedData.apiToken = tokenModel.token
                                        apiTokenCallBack.onRefByIDSuccess(tokenModel.token)
                                    } else {
                                        apiTokenCallBack.onGetByRefIDError("Failed to parse token model")
                                    }
                                } else {
                                    apiTokenCallBack.onGetByRefIDError("Response body is empty")
                                }
                            }
                        } catch (e: Exception) {
                            apiTokenCallBack.onGetByRefIDError("An error occurred: ${e.message}")
                        }
                    } else {
                        apiTokenCallBack.onGetByRefIDError("Unsuccessful response: " + response.code())
                    }
                }

                override fun onFailure(call: Call<ResponseBody?>, t: Throwable) {
                    apiTokenCallBack.onGetByRefIDError(t.message)
                }
            })
        } catch (error: Exception) {
            apiTokenCallBack.onGetByRefIDError("Exception during API call: $error")
        }
    }

}