package com.vpn.fastestvpnservice.viewmodels

import android.content.Context
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.vpn.fastestvpnservice.beans.DataResponseServers
import com.vpn.fastestvpnservice.beans.ServerData
import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
import com.vpn.fastestvpnservice.retrofit.RetrofitNetworkHandling
import com.vpn.fastestvpnservice.retrofit.WebServiceFactory
import retrofit2.Call

class SplashViewModel constructor(context: Context): ViewModel() {
    val mutableLiveDataServerData = MutableLiveData<DataResponseServers<ArrayList<ServerData>>>()
    var preferenceHelper: BasePreferenceHelper

    init {
        preferenceHelper = BasePreferenceHelper(context)
    }
    fun serverDataApi() {
        WebServiceFactory.getInstance().serverDataApi().enqueue(
            RetrofitNetworkHandling<Any>(object :
                RetrofitNetworkHandling.ResponseCallback<Any> {
                override fun onSuccess(call: Call<Any>?, response: Any?) {
                    try {
                        Log.d("test_api_response", "server try = $response")
                        val gson = Gson()
                        val jsonString = gson.toJson(response)
                        val type = object : TypeToken<DataResponseServers<ArrayList<ServerData>>>() {}.type
                        val data = gson.fromJson<DataResponseServers<ArrayList<ServerData>>>(jsonString, type)
                        Log.d("test_api_response", "server try After = $response")

                        if (data?.status == true) {
                            Log.d("test_fav_response", "Splash Res: ${data.message}")

                            mutableLiveDataServerData.value = data
                            data.data?.let { preferenceHelper.saveServerData(it)
                            }
                            data.wireguard?.let {
                                preferenceHelper.saveWireGuard(it)
                            }
                            data.product?.let {
//                            Log.d("test_api_response", it.productName.toString())
                                preferenceHelper.saveProduct(it)
                            }
                            data.available_protocols.let {
                                preferenceHelper.saveAvailableProtocols(it)
                            }
                            data.enabled_protocols.let {
                                preferenceHelper.saveEnabledProtocols(it)
                            }
//                        Log.d("test_api_response", data.data.toString())
//                        Log.d("test_api_response", data.enabled_protocols.toString())
//                        Log.d("test_api_response", data.available_protocols.toString())
                        }
                    } catch (ex: Exception) {
                        Log.d("test_api_response", "server catch")
                    }
                }

                override fun onFail(call: Call<Any>?, response: Any?) {
                    Log.d("test_api_response", "server onFail = $response")
                }

                override fun onError(call: Call<Any>?, response: Any?) {
                    Log.d("test_api_response", "server onError = $response")
                }

            }
            )
        )

    }

}