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

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.proyek.akhir.kodel.data.response.Anggota
import com.proyek.akhir.kodel.data.response.IsiSaldo
import com.proyek.akhir.kodel.data.response.TarikSaldo
import com.proyek.akhir.kodel.data.response.Utang
import com.proyek.akhir.kodel.utils.ApiClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class HomeViewModel : ViewModel() {
    private val anggotaKoperasi = MutableLiveData<Anggota>()
    private val anggotaIsiSaldo = MutableLiveData<IsiSaldo>()
    private val anggotaTarikSaldo = MutableLiveData<TarikSaldo>()

    private val _text = MutableLiveData<String>().apply {
        value = "This is beranda Fragment"
    }
    val text: LiveData<String> = _text

    fun getAnggotaKoperasi() {
        ApiClient().getServiceDel().getAllAnggota().enqueue(object : Callback<Anggota> {
            override fun onFailure(call: Call<Anggota>, t: Throwable) {
                anggotaKoperasi.value = null
            }

            override fun onResponse(call: Call<Anggota>, response: Response<Anggota>) {
                if (response.isSuccessful) {
                    anggotaKoperasi.value = response.body()

                } else {
                    anggotaKoperasi.value = null
                }
            }
        })
    }

    fun getAnggotaIsiSaldo() {
        ApiClient().getServiceDel().getAllIsiSaldo().enqueue(object : Callback<IsiSaldo> {
            override fun onFailure(call: Call<IsiSaldo>, t: Throwable) {
                anggotaIsiSaldo.value = null
            }

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

    fun getTarikSaldo() {
        ApiClient().getServiceDel().getAllTarikSaldo().enqueue(object : Callback<TarikSaldo> {
            override fun onFailure(call: Call<TarikSaldo>, t: Throwable) {
                anggotaTarikSaldo.value = null
            }

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

        })
    }

    fun setDataAnggota(): MutableLiveData<Anggota> {
        return anggotaKoperasi
    }

    fun setDataIsiSaldo(): MutableLiveData<IsiSaldo> {
        return anggotaIsiSaldo
    }

    fun setDataTarikSaldo(): MutableLiveData<TarikSaldo> {
        return anggotaTarikSaldo
    }
}