package com.proyek.akhir.kodel.ui.koperasi.kupon

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.proyek.akhir.kodel.data.response.Kupon
import com.proyek.akhir.kodel.utils.ApiClient
import retrofit2.Call
import retrofit2.Response

class KuponViewModel : ViewModel() {
    private val koperasiKupon = MutableLiveData<Kupon>()

    init {
        getKupon()
    }

    private fun getKupon() {
        ApiClient().getServiceKode().getAllCupon().enqueue(object: retrofit2.Callback<Kupon>{
            override fun onFailure(call: Call<Kupon>, t: Throwable) {
                koperasiKupon.value = null
            }

            override fun onResponse(call: Call<Kupon>, response: Response<Kupon>) {
                if(response.isSuccessful){
                    koperasiKupon.value = response.body()
                }else{
                    koperasiKupon.value = null
                }
            }
        })
    }

    fun setDataKupon(): MutableLiveData<Kupon>{
        return koperasiKupon
    }
}