package com.vpn.fastestvpnservice.viewmodels

import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.vpn.fastestvpnservice.beans.DataResponse
import com.vpn.fastestvpnservice.beans.UserResponse
import com.vpn.fastestvpnservice.retrofit.RetrofitNetworkHandling
import com.vpn.fastestvpnservice.retrofit.WebServiceFactory
import retrofit2.Call
import java.util.Objects

class LoginViewModel: ViewModel() {
    var mutableLiveDataUserResponse = MutableLiveData<DataResponse<UserResponse>>()
    var liveDataUserResponse: LiveData<DataResponse<UserResponse>> = mutableLiveDataUserResponse

    var mutableLiveDataUserResponseErrorStatus = MutableLiveData<Boolean>()

    private var mutableLiveDataLoginStatus = MutableLiveData<Boolean>(false)
    var liveDataLoginStatus: LiveData<Boolean> = mutableLiveDataLoginStatus


    var mutableLiveDataFcm = MutableLiveData<DataResponse<Objects>>()
    var liveDataFcm: LiveData<DataResponse<Objects>> = mutableLiveDataFcm
    fun loginRequest(
        email: String,
        password: String,
        platform: String,
        version: String,
        app_version: String
    ) {
        WebServiceFactory.getInstance().login(
            email, password, platform, version, app_version).enqueue(
            RetrofitNetworkHandling<Any?>(object :
                RetrofitNetworkHandling.ResponseCallback<Any?> {
                override fun onSuccess(call: Call<Any?>?, response: Any?) {
                    try {
                        Log.d("test_api_response","Response login = " + response.toString())

                        val gson = Gson()
                        val jsonString = gson.toJson(response)
                        val type = object : TypeToken<DataResponse<UserResponse>>() {}.type
                        val data = gson.fromJson<DataResponse<UserResponse>>(jsonString, type)

                        Log.d("test_api_response", "login = ${data.status} ${data.message}")

                        data.let {
                            if (it?.status == true) {
                                mutableLiveDataUserResponse.value = data
                            } else {
                                mutableLiveDataUserResponse.value = data
                            }
                        }
                    } catch (ex: Exception) {
                        Log.d("test_api_response", "catch")
                    }
                }

                override fun onFail(call: Call<Any?>?, response: Any?) {
                    Log.d("test_api_response","Response onFail:")
                    mutableLiveDataUserResponseErrorStatus.value = true
                    mutableLiveDataLoginStatus.value = false
                }

                override fun onError(call: Call<Any?>?, response: Any?) {
                    Log.d("test_api_response","Response onError:")
                    mutableLiveDataUserResponseErrorStatus.value = true
                    mutableLiveDataLoginStatus.value = false
                }
            })
        )
    }

    fun setLoginStatus(status: Boolean) {
        mutableLiveDataLoginStatus.value = status
    }

    fun sendFcmToken(token: String) {
        WebServiceFactory.getInstance()
            .sendFcmToken("android", if (token.isEmpty()) "test_123" else token).enqueue(
                RetrofitNetworkHandling<Any>(object :
                    RetrofitNetworkHandling.ResponseCallback<Any> {
                    override fun onSuccess(call: Call<Any>?, response: Any?) {
                        try {
                            val gson = Gson()
                            val jsonString = gson.toJson(response)
                            val type = object : TypeToken<DataResponse<Objects>>() {}.type
                            val data = gson.fromJson<DataResponse<Objects>>(jsonString, type)
                            mutableLiveDataFcm.value = data
                        } catch (ex: Exception) {
                        }
                    }

                    override fun onFail(call: Call<Any>?, response: Any?) {
                    }

                    override fun onError(call: Call<Any>?, response: Any?) {
                    }


                })
            )
    }


}