package com.vpn.fastestvpnservice.viewmodels

import android.util.Log
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.retrofit.RetrofitNetworkHandling
import com.vpn.fastestvpnservice.retrofit.WebServiceFactory
import retrofit2.Call
import java.util.Objects

class AccountViewModel: ViewModel() {
    var mutableLiveDataLogout = MutableLiveData<Boolean>()
    var liveDataLogout: LiveData<Boolean> = mutableLiveDataLogout

    var mutableLiveDataLogoutStatus = MutableLiveData<Boolean>(false)
    var liveDataLogoutStatus: LiveData<Boolean> = mutableLiveDataLogoutStatus

    var mutableLiveDataDelete = MutableLiveData<DataResponse<Objects>?>()
    var liveDataDelete: LiveData<DataResponse<Objects>?> = mutableLiveDataDelete

    var mutableLiveDataDeleteStatus = MutableLiveData<Boolean>(false)
    var liveDataDeleteStatus: LiveData<Boolean> = mutableLiveDataDeleteStatus

    fun logout(){
        WebServiceFactory.getInstance().logout().enqueue(
            RetrofitNetworkHandling<Any>(
            object : RetrofitNetworkHandling.ResponseCallback<Any> {
                override fun onSuccess(call: Call<Any>?, response: Any?) {
                    try {
                        Log.d("test_api_response","Logout onSuccess: ${response.toString()}")
                        val gson = Gson()
                        val jsonString = gson.toJson(response)
                        val type = object : TypeToken<DataResponse<Object>>() {}.type
                        val data = gson.fromJson<DataResponse<Object>>(jsonString, type)
                        mutableLiveDataLogout.value = data.status

                        Log.d("test_api_response","Logout After onSuccess: ${response.toString()}")
                    } catch (ex: Exception) {

                    }

                }

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

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

    fun setLogoutStatus(status: Boolean) {
        mutableLiveDataLogoutStatus.value = status
    }

    fun deleteAccount() {
        WebServiceFactory.getInstance().deleteAccount().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)
                        Log.d("test_api_response","Delete try ${data.status}:")

                        mutableLiveDataDelete.value = data
                    } catch (ex: Exception) {

                    }
                }

                override fun onFail(call: Call<Any>?, response: Any?) {
                    Log.d("test_api_response","Delete onFail:")
                    mutableLiveDataDelete.value = DataResponse(false)
                }

                override fun onError(call: Call<Any>?, response: Any?) {
                    Log.d("test_api_response","Delete onError:")
                    mutableLiveDataDelete.value = DataResponse(false)
                }
            })
        )
    }

    fun setDeleteStatus(status: Boolean) {
        mutableLiveDataDeleteStatus.value = status
    }

}