package com.proyek.akhir.kodel.ui.koperasi.berita.detailBerita

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.BeritaResponse
import com.proyek.akhir.kodel.data.response.Beritas
import com.proyek.akhir.kodel.ui.koperasi.berita.BeritaKoperasiActivity
import com.proyek.akhir.kodel.utils.ApiClient
import kotlinx.android.synthetic.main.activity_detail_berita_koperasi.*
import retrofit2.Call
import retrofit2.Response
import java.text.SimpleDateFormat
import java.util.*

class AddBeritaKoperasiActivity : AppCompatActivity() {

    lateinit var judulBerita: String
    lateinit var isiBerita: String

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

        val c: Date = Calendar.getInstance().time

        val df = SimpleDateFormat("dd/MM/yyyy")
        val formattedDate: String = df.format(c)


        progressTambahBerita.visibility = View.INVISIBLE
        btnTambahBerita.setOnClickListener {
            judulBerita = judulBeritaValue.text.toString()
            isiBerita = isiBeritaValue.text.toString()

            val beritas = Beritas(
                isiBerita,
                "",
                1,
                7,
                judulBerita,
                formattedDate
            )
            postBerita(beritas)
        }
    }

    private fun postBerita(berita: Beritas) {
        progressTambahBerita.visibility = View.VISIBLE
        val beritas = BeritaResponse(berita)

        ApiClient().postServicePeople().postBerita(beritas)
            .enqueue(object : retrofit2.Callback<BeritaResponse> {
                override fun onFailure(call: Call<BeritaResponse>, t: Throwable) {
                    progressTambahBerita.visibility = View.INVISIBLE
                    Toast.makeText(
                        this@AddBeritaKoperasiActivity,
                        "Jaringan Lemah!",
                        Toast.LENGTH_SHORT
                    ).show()
                }

                override fun onResponse(
                    call: Call<BeritaResponse>,
                    response: Response<BeritaResponse>
                ) {
                    if (response.isSuccessful) {
                        progressTambahBerita.visibility = View.INVISIBLE
                        startActivity(
                            Intent(
                                this@AddBeritaKoperasiActivity,
                                BeritaKoperasiActivity::class.java
                            )
                        )
                        finish()
                    } else {
                        progressTambahBerita.visibility = View.INVISIBLE
                        Toast.makeText(
                            this@AddBeritaKoperasiActivity,
                            "Jaringan Lemah!",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }

            })
    }


}
