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

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

class TarikSaldoActivity : AppCompatActivity() {
    lateinit var saldoUser: String
    lateinit var namaUser: String
    lateinit var idAnggota: String

    private var valueTarikSaldoTemp: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tarik_saldo)
        saldoUser = intent.getStringExtra("saldoUser")
        namaUser = intent.getStringExtra("nameUser")
        idAnggota = intent.getStringExtra("idAnggota")

        progressTarikSaldo.visibility = View.INVISIBLE

        btnTarikSaldo.setOnClickListener {
            progressTarikSaldo.visibility = View.VISIBLE
            if (valueTarikSaldoTemp.toInt() > saldoUser.toInt()) {
                progressTarikSaldo.visibility = View.INVISIBLE
                Toast.makeText(this, "Saldo Anda Kurang", Toast.LENGTH_SHORT).show()
            } else {
                postTarikSaldo()
            }
        }

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

    private fun postTarikSaldo() {
        val tarikSaldo = TarikSaldos(
            1,
            idAnggota.toInt(),
            valueTarikSaldoTemp.toInt(),
            namaUser,
            statusTarik = false
        )

        val tarik = TarikSaldoResponse(tarikSaldo)

        ApiClient().postServicePeople().postTarikSaldo(tarik)
            .enqueue(object : Callback<TarikSaldoResponse> {
                override fun onFailure(call: Call<TarikSaldoResponse>, t: Throwable) {
                    progressTarikSaldo.visibility = View.INVISIBLE
                    Toast.makeText(
                        applicationContext,
                        "Gagal melakukan Pinjaman",
                        Toast.LENGTH_SHORT
                    ).show()
                }

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