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.beans.Notification
import com.vpn.fastestvpnservice.retrofit.RetrofitNetworkHandling
import com.vpn.fastestvpnservice.retrofit.WebServiceFactory
import retrofit2.Call

class NotificationViewModel: ViewModel() {

    val _mutableLiveDataNotification = MutableLiveData<List<Notification>?>()
    val liveDataNotification: LiveData<List<Notification>?> = _mutableLiveDataNotification

    fun getNotifications() {
        WebServiceFactory.getInstance().getNotifications()
            .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<List<Notification>>>() {}.type
                        val data = gson.fromJson<DataResponse<List<Notification>>>(jsonString, type)

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

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

                    }


                }

                override fun onFail(call: Call<Any>?, response: Any?) {
                    _mutableLiveDataNotification.value = null
                }

                override fun onError(call: Call<Any>?, response: Any?) {
                    _mutableLiveDataNotification.value = null
                }

            }))

    }
}