package com.example.tabbuy.ui.katalog

import android.net.Uri
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.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.example.tabbuy.MainActivity
import com.example.tabbuy.data.local.entity.ProdukEntity
import com.example.tabbuy.databinding.FragmentDetailProdukBinding
import com.example.tabbuy.ui.MainViewModel
import java.text.NumberFormat
import java.util.Locale

class DetailProdukFragment : Fragment() {
    private var _binding: FragmentDetailProdukBinding? = null
    private val binding get() = _binding!!
    private val viewModel: MainViewModel by activityViewModels()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        _binding = FragmentDetailProdukBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // Hanya gunakan ViewModel sebagai sumber data tunggal
        viewModel.selectedProduk.observe(viewLifecycleOwner) { produk ->
            if (produk != null) {
                setupUI(produk)
            } else {
                findNavController().popBackStack()
            }
        }

        binding.toolbar.setNavigationOnClickListener {
            findNavController().popBackStack()
        }
    }

    private fun setupUI(produk: ProdukEntity) {
        binding.apply {
            // HAPUS: collapsingToolbar.title = produk.nama (Agar tidak duplikat dengan tvDetailNama)
            tvDetailNama.text = produk.nama
            tvDetailDeskripsi.text = produk.deskripsi
            
            val localeID = Locale("in", "ID")
            val formatRupiah = NumberFormat.getCurrencyInstance(localeID)
            formatRupiah.maximumFractionDigits = 0
            tvDetailHarga.text = formatRupiah.format(produk.harga)

            // Memuat gambar dengan Glide
            if (produk.gambar.isNotEmpty()) {
                val uri = Uri.parse(produk.gambar)
                Glide.with(this@DetailProdukFragment)
                    .load(uri)
                    .diskCacheStrategy(DiskCacheStrategy.ALL) // Gunakan cache agar lebih cepat
                    .placeholder(android.R.drawable.ic_menu_gallery)
                    .error(android.R.drawable.ic_menu_report_image)
                    .into(ivDetailProduk)
            }

            btnAddToCartDetail.setOnClickListener {
                viewModel.addToCart(produk)
                (activity as? MainActivity)?.sendNotification("Berhasil!", "${produk.nama} masuk keranjang.")
                Toast.makeText(requireContext(), "Ditambahkan!", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
