package com.covalima.rekomendasaunaihan.ui.ingredientes import android.content.Intent import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager import com.covalima.rekomendasaunaihan.databinding.FragmentListIngredientesBinding import com.covalima.rekomendasaunaihan.model.Aihan import com.covalima.rekomendasaunaihan.model.ApiResponse import com.covalima.rekomendasaunaihan.model.Ingredientes import com.covalima.rekomendasaunaihan.network.ApiClient import com.covalima.rekomendasaunaihan.ui.aihan.AihanFiltruActivity import com.covalima.rekomendasaunaihan.ui.detail.DetailAihanActivity import retrofit2.Call import retrofit2.Callback import retrofit2.Response class ListIngredientesFragment : Fragment() { private var _binding: FragmentListIngredientesBinding? = null private val binding get() = _binding!! private lateinit var adapter: IngredientesAdapter override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { _binding = FragmentListIngredientesBinding.inflate(inflater, container, false) return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) adapter = IngredientesAdapter(emptyList()) { ingredientes -> bukaAihanBaIngredientes(ingredientes) } binding.rvIngredientes.layoutManager = LinearLayoutManager(requireContext()) binding.rvIngredientes.adapter = adapter binding.swipeRefresh.setOnRefreshListener { loadData() } loadData() } /** * Wainhira user hili ingredientes ida, sistema buka aihan sira ne'ebe * hanesan ho ingredientes ne'e. Se de'it AI-HAN IDA de'it hetan, liu * direta ba pájina Detalhe (hatudu foto resep + deskrisaun kompletu). * Se AI-HAN BARAK hetan (sr: Ikan -> Tukir + Saboko), hatudu grid atu * user hili. */ private fun bukaAihanBaIngredientes(ingredientes: Ingredientes) { binding.progressBar.visibility = View.VISIBLE ApiClient.apiService.getListAihanTuirFiltru(idIngredientes = ingredientes.idIngredientes) .enqueue(object : Callback>> { override fun onResponse( call: Call>>, response: Response>> ) { binding.progressBar.visibility = View.GONE val data = response.body()?.data ?: emptyList() when { data.isEmpty() -> { Toast.makeText(requireContext(), "Seidauk iha ai-han ba ingredientes ne'e", Toast.LENGTH_SHORT).show() } data.size == 1 -> { // Ai-han ida de'it -> liu direta ba Detalhe (hatudu FOTO RESEP + deskrisaun) val intent = Intent(requireContext(), DetailAihanActivity::class.java) intent.putExtra("id_aihan", data[0].idAihan) intent.putExtra("uza_foto_resep", true) startActivity(intent) } else -> { // Ai-han barak (sr: Ikan) -> hatudu grid atu user hili val intent = Intent(requireContext(), AihanFiltruActivity::class.java) intent.putExtra("id_ingredientes", ingredientes.idIngredientes) intent.putExtra("naran_grupu", ingredientes.naranIngredientes) intent.putExtra("tipu_grupu", "Ingredientes") startActivity(intent) } } } override fun onFailure(call: Call>>, t: Throwable) { binding.progressBar.visibility = View.GONE Toast.makeText(requireContext(), "Konesaun falha: ${t.message}", Toast.LENGTH_SHORT).show() } }) } private fun loadData() { binding.progressBar.visibility = View.VISIBLE ApiClient.apiService.getListIngredientes().enqueue(object : Callback>> { override fun onResponse( call: Call>>, response: Response>> ) { binding.progressBar.visibility = View.GONE binding.swipeRefresh.isRefreshing = false val body = response.body() if (body?.status == "success" && body.data != null) { adapter.updateData(body.data) } else { Toast.makeText(requireContext(), "Dadus la hetan", Toast.LENGTH_SHORT).show() } } override fun onFailure(call: Call>>, t: Throwable) { binding.progressBar.visibility = View.GONE binding.swipeRefresh.isRefreshing = false Toast.makeText(requireContext(), "Konesaun falha: ${t.message}", Toast.LENGTH_SHORT).show() } }) } override fun onDestroyView() { super.onDestroyView() _binding = null } }