package com.vpn.fastestvpnservice.viewmodels

import android.content.Context
import android.util.Log
import androidx.compose.ui.platform.LocalContext
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.IpInfo
import com.vpn.fastestvpnservice.beans.TokenResponse
import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
import com.vpn.fastestvpnservice.retrofit.RetrofitNetworkHandling
import com.vpn.fastestvpnservice.retrofit.WebServiceFactory
import retrofit2.Call

class HomeViewModel constructor(context: Context): ViewModel() {
    var prefHelper: BasePreferenceHelper? = null
    val mutableLiveDataIpInfo = MutableLiveData<IpInfo?>()

    var _mutableLiveDataValidate = MutableLiveData<DataResponse<TokenResponse>>()
    var liveDataValidate : LiveData<DataResponse<TokenResponse>> = _mutableLiveDataValidate
    var mutableLiveDataValidateError = MutableLiveData<String>()

    init {
        prefHelper = BasePreferenceHelper(context)
    }
    fun getIp() {
        WebServiceFactory.getInstance().getIp()
            .enqueue(RetrofitNetworkHandling<Any>(object :
                RetrofitNetworkHandling.ResponseCallback<Any> {
                override fun onSuccess(call: Call<Any>?, response: Any?) {
                    Log.d("test_api_response", "ip = onSuccess")
                    try {
                        val gson = Gson()
                        val jsonString = gson.toJson(response)
                        val type = object : TypeToken<IpInfo>() {}.type
                        val data = gson.fromJson<IpInfo>(jsonString, type)
                        mutableLiveDataIpInfo.value = data
                        if (data != null) {
                            prefHelper?.saveIpInfo(data)
                        }
                    Log.d("test_api_response", "ip = " + data?.country.toString() + " " + data?.countryCode)
                    } catch (ex: Exception) { }
                }

                override fun onFail(call: Call<Any>?, response: Any?) {
                    Log.d("test_api_response", "ip: = onFail")
                    mutableLiveDataIpInfo.value = null
                }

                override fun onError(call: Call<Any>?, response: Any?) {
                    Log.d("test_api_response", "ip: = onError")
                    mutableLiveDataIpInfo.value = null
                }
            }))
    }

    fun validatePassword(
        email: String,
        password: String,
        platform: String,
        version: String
    ) {

        WebServiceFactory.getInstance().validatePassword(email, password, platform, version)?.enqueue(
            RetrofitNetworkHandling<Any>(object :
                RetrofitNetworkHandling.ResponseCallback<Any> {
                override fun onSuccess(call: Call<Any>?, response: Any?) {
                    try {
                        Log.d("test_api_response", "validatePassword onSuccess: try")
                        val gson = Gson()
                        val jsonString = gson.toJson(response)
                        val type = object : TypeToken<DataResponse<TokenResponse?>?>() {}.type
                        val data = gson.fromJson<DataResponse<TokenResponse>>(jsonString, type)
                        _mutableLiveDataValidate.value = data

                    } catch (ex: Exception) {
                        Log.d("test_api_response", "validatePassword onSuccess: catch")
                    }

                }

                override fun onFail(call: Call<Any>?, response: Any?) {
                    Log.d("test_api_response", "validatePassword onFail")
                    mutableLiveDataValidateError.value = "Something went wrong. Please try again"
                }

                override fun onError(call: Call<Any>?, response: Any?) {
                    Log.d("test_api_response", "validatePassword onError")
                    mutableLiveDataValidateError.value = "Something went wrong. Please try again"
                }
            })
        )
    }
}