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

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.proyek.akhir.kodel.R
import com.proyek.akhir.kodel.data.response.IsiSaldoItem
import com.proyek.akhir.kodel.data.response.IsiSaldoResponse
import com.proyek.akhir.kodel.data.response.IsiSaldos
import com.proyek.akhir.kodel.ui.koperasi.beranda.HomeViewModel
import com.proyek.akhir.kodel.utils.ApiClient
import kotlinx.android.synthetic.main.activity_isi_saldo2.*
import retrofit2.Call
import retrofit2.Response

class IsiSaldoActivity : AppCompatActivity() {
    private lateinit var IsiSaldoViewModel: HomeViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_isi_saldo2)

        rvIsiSaldo.setHasFixedSize(true)
        rvIsiSaldo.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)

        refreshIsiSaldo.setOnRefreshListener {
            refreshIsiSaldo.isRefreshing = true
            showRecycler()
        }
        refreshIsiSaldoKoperasi.visibility = View.INVISIBLE
        refreshIsiSaldo.isRefreshing = true
        showRecycler()
    }

    private fun showRecycler() {
        IsiSaldoViewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
        IsiSaldoViewModel.getAnggotaIsiSaldo()
        IsiSaldoViewModel.setDataIsiSaldo().observe(this, Observer { isiSaldo ->
            if (isiSaldo != null) {
                refreshIsiSaldo.isRefreshing = false
                showData(isiSaldo)
            } else {
                refreshIsiSaldo.isRefreshing = false
                Toast.makeText(this, "Koneksi Lemah, Refresh!", Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun showData(item: List<IsiSaldoItem>) {
        rvIsiSaldo.adapter = IsiSaldoAdapter(item)
    }

    fun postIsiSaldo(isinya: IsiSaldos){
        refreshIsiSaldoKoperasi.visibility = View.VISIBLE
        val isi = IsiSaldoResponse(isinya)
        ApiClient().postServicePeople().editIsiSaldo(isi).enqueue(object:retrofit2.Callback<IsiSaldoResponse>{
            override fun onFailure(call: Call<IsiSaldoResponse>, t: Throwable) {
                refreshIsiSaldoKoperasi.visibility = View.INVISIBLE
                Toast.makeText(
                    applicationContext,
                    "Mengisi Saldo...",
                    Toast.LENGTH_SHORT
                ).show()
            }

            override fun onResponse(
                call: Call<IsiSaldoResponse>,
                response: Response<IsiSaldoResponse>
            ) {
                if(response.isSuccessful){
                    showRecycler()
                    refreshIsiSaldoKoperasi.visibility = View.INVISIBLE
                }else{
                    refreshIsiSaldoKoperasi.visibility = View.INVISIBLE
                    Toast.makeText(
                        applicationContext,
                        "Mengisi Saldo..",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }

        })
    }
}
