package com.proyek.akhir.kodel.ui.koperasi.kupon.addKupon

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.KuponResponse
import com.proyek.akhir.kodel.data.response.Kupons
import com.proyek.akhir.kodel.ui.koperasi.kupon.KuponKoperasiActivity
import com.proyek.akhir.kodel.utils.ApiClient
import kotlinx.android.synthetic.main.activity_tambah_kupon.*
import retrofit2.Call
import retrofit2.Response

class TambahKuponActivity : AppCompatActivity() {

    lateinit var kodeKupon: String
    lateinit var descKupon: String
    lateinit var jlhDisc: String
    lateinit var dateKupon: String

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

        swipeBarDetailProduk.setOnRefreshListener {
            swipeBarDetailProduk.isRefreshing = true
        }

        btnTambahKupon.setOnClickListener {
            kodeKupon = codeKuponValue.text.toString()
            descKupon = descKuponValue.text.toString()
            jlhDisc = valueKuponJlh.text.toString()
            dateKupon =
                dateValidasi.dayOfMonth.toString() + "/" + dateValidasi.month.toString() + "/" + dateValidasi.year.toString()

            val kuponItem = Kupons(
                kodeKupon,
                descKupon,
                1,
                dateKupon,
                jlhDisc.toInt()
            )
            postKupon(kuponItem)
        }
    }

    private fun postKupon(kupon: Kupons) {
        progressTambahKupon.visibility = View.VISIBLE
        val kupons = KuponResponse(kupon)

        ApiClient().postServicePeople().postKupon(kupons)
            .enqueue(object : retrofit2.Callback<KuponResponse> {
                override fun onFailure(call: Call<KuponResponse>, t: Throwable) {
                    progressTambahKupon.visibility = View.INVISIBLE
                    Toast.makeText(this@TambahKuponActivity, "Jaringan Lemah", Toast.LENGTH_SHORT)
                        .show()
                }

                override fun onResponse(
                    call: Call<KuponResponse>,
                    response: Response<KuponResponse>
                ) {
                    if (response.isSuccessful) {
                        progressTambahKupon.visibility = View.GONE
                        startActivity(
                            Intent(
                                this@TambahKuponActivity,
                                KuponKoperasiActivity::class.java
                            )
                        )
                        finish()
                    } else {
                        progressTambahKupon.visibility = View.INVISIBLE
                        Toast.makeText(
                            this@TambahKuponActivity,
                            "Jaringan Lemah",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }

            })
    }


}
