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

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.proyek.akhir.kodel.R
import com.proyek.akhir.kodel.data.response.PinjamResponse
import com.proyek.akhir.kodel.data.response.Pinjams
import com.proyek.akhir.kodel.utils.ApiClient
import com.proyek.akhir.kodel.utils.Constans
import kotlinx.android.synthetic.main.activity_ajuan_pinjaman.*
import retrofit2.Call
import retrofit2.Response
import java.text.SimpleDateFormat
import java.util.*


class AjuanPinjamanActivity : AppCompatActivity() {

    private var id = 5
    private var idAnggota = Constans.ID_ANGGOTA
    private lateinit var jlhPinjam: String
    private var statusPinjam = false

    private var date: Date = Calendar.getInstance().time
    private val inputFormatter = SimpleDateFormat("dd/MM/yyyy", Locale.US)
    private val resultDate = inputFormatter.format(date)

    lateinit var namaPengguna: String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ajuan_pinjaman)

        backButtonAjuanPinjaman.setOnClickListener {
            onBackPressed()
        }
        namaPengguna = intent.getStringExtra("nameUser")

        btnAjuanPinjaman.setOnClickListener {
            jlhPinjam = pinjamanInputMasuk.text.toString()
            refreshAjuanPinjaman.isRefreshing = true
            postPinjamanData(jlhPinjam, namaPengguna)
            refreshAjuanPinjaman.isRefreshing = false
        }
        progressBarPinjaman.visibility = View.INVISIBLE
    }

    private fun postPinjamanData(saldo: String, name: String) {

        progressBarPinjaman.visibility = View.VISIBLE

        val permohonanPinjam = Pinjams(
            id,
            idAnggota.toInt(),
            saldo.toInt(),
            name,
            statusPinjam,
            resultDate
        )
        val pinjam = PinjamResponse(permohonanPinjam)

        ApiClient().postServicePeople()
            .postAjuanPinjaman(pinjam)
            .enqueue(object : retrofit2.Callback<PinjamResponse> {
                override fun onFailure(call: Call<PinjamResponse>, t: Throwable) {
                    progressBarPinjaman.visibility = View.INVISIBLE
                    Toast.makeText(
                        applicationContext,
                        "Gagal melakukan Pinjaman",
                        Toast.LENGTH_SHORT
                    ).show()
                }

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