package com.proyek.akhir.kodel.ui.user.dashboard.isiSaldo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.proyek.akhir.kodel.R
import com.proyek.akhir.kodel.data.response.IsiSaldoResponse
import com.proyek.akhir.kodel.data.response.IsiSaldos
import com.proyek.akhir.kodel.ui.user.dashboard.success.SuccessActivity
import com.proyek.akhir.kodel.utils.ApiClient
import kotlinx.android.synthetic.main.activity_isi_saldo.*
import retrofit2.Call
import retrofit2.Response

class IsiSaldoActivity : AppCompatActivity() {

    lateinit var namaUser: String
    lateinit var idUser: String
    lateinit var imgUser: String

    private var valueTarikSaldoTemp: String = ""

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

        idUser = intent.getStringExtra("idAnggota")
        namaUser = intent.getStringExtra("nameAnggota")
        imgUser = intent.getStringExtra("imgUser")

        btnIsiSaldo.setOnClickListener {
            progressIsiSaldo.visibility = View.VISIBLE
            postIsiSaldo()
        }

        btn0.setOnClickListener {

            valueTarikSaldoTemp += "0"
            valueIsiSaldo.text = valueTarikSaldoTemp
        }
        btn1.setOnClickListener {
            valueTarikSaldoTemp += "1"
            valueIsiSaldo.text = valueTarikSaldoTemp
        }
        btn2.setOnClickListener {
            valueTarikSaldoTemp += "2"
            valueIsiSaldo.text = valueTarikSaldoTemp
        }
        btn3.setOnClickListener {
            valueTarikSaldoTemp += "3"
            valueIsiSaldo.text = valueTarikSaldoTemp
        }
        btn4.setOnClickListener {
            valueTarikSaldoTemp += "4"
            valueIsiSaldo.text = valueTarikSaldoTemp
        }
        btn5.setOnClickListener {
            valueTarikSaldoTemp += "5"
            valueIsiSaldo.text = valueTarikSaldoTemp
        }
        btn6.setOnClickListener {
            valueTarikSaldoTemp += "6"
            valueIsiSaldo.text = valueTarikSaldoTemp
        }
        btn7.setOnClickListener {
            valueTarikSaldoTemp += "7"
            valueIsiSaldo.text = valueTarikSaldoTemp
        }
        btn8.setOnClickListener {
            valueTarikSaldoTemp += "8"
            valueIsiSaldo.text = valueTarikSaldoTemp
        }
        btn9.setOnClickListener {
            valueTarikSaldoTemp += "9"
            valueIsiSaldo.text = valueTarikSaldoTemp
        }
        btn00.setOnClickListener {
            valueTarikSaldoTemp += "00"
            valueIsiSaldo.text = valueTarikSaldoTemp
        }
        btnDelete.setOnClickListener {
            valueTarikSaldoTemp.dropLast(1)
            valueIsiSaldo.text = valueTarikSaldoTemp
        }
    }

    private fun postIsiSaldo() {
        val isiSaldo = IsiSaldos(
            1,
            idUser.toInt(),
            imgUser,
            valueTarikSaldoTemp.toInt(),
            namaUser,
            false
        )

        val isi = IsiSaldoResponse(isiSaldo)

        ApiClient().postServicePeople().postIsiSaldo(isi)
            .enqueue(object : retrofit2.Callback<IsiSaldoResponse> {
                override fun onFailure(call: Call<IsiSaldoResponse>, t: Throwable) {
                    progressIsiSaldo.visibility = View.INVISIBLE
                    Toast.makeText(
                        applicationContext,
                        "Permintaan Gagal",
                        Toast.LENGTH_SHORT
                    ).show()
                }

                override fun onResponse(
                    call: Call<IsiSaldoResponse>,
                    response: Response<IsiSaldoResponse>
                ) {
                    if (response.isSuccessful) {
                        progressIsiSaldo.visibility = View.INVISIBLE
                        startActivity(Intent(this@IsiSaldoActivity, SuccessActivity::class.java))
                        finish()
                    } else {
                        progressIsiSaldo.visibility = View.INVISIBLE
                        Toast.makeText(
                            applicationContext,
                            response.errorBody().toString(),
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }

            })
    }

}
