package com.divya.hardinfo.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.navArgs
import androidx.navigation.fragment.findNavController
import com.divya.hardinfo.R
import com.divya.hardinfo.databinding.FragmentDetailBinding
import com.divya.hardinfo.viewmodel.HardwareViewModel

class HardwareDetailFragment : Fragment() {

    private var _binding: FragmentDetailBinding? = null
    private val binding get() = _binding!!
    private val viewModel: HardwareViewModel by viewModels()
    private val args: HardwareDetailFragmentArgs by navArgs()

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

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

        // Hide global Action Bar
        (activity as? androidx.appcompat.app.AppCompatActivity)?.supportActionBar?.hide()

        // Setup local Toolbar
        binding.toolbar.setNavigationIcon(androidx.appcompat.R.drawable.abc_ic_ab_back_material)
        binding.toolbar.setNavigationOnClickListener {
            findNavController().navigateUp()
        }

        viewModel.loadItemById(args.itemId)
        viewModel.selectedItem.observe(viewLifecycleOwner) { item ->
            if (item != null) {
                binding.textTitle.text = item.name
                binding.textDescription.text = item.detailedDescription
                binding.textFunctions.text = item.functions
                binding.textSpecs.text = item.specs
                binding.textUseCases.text = item.useCases
                
                updateFavoriteIcon(item.isFavorite)

                val formattedName = item.imageRes.replace("@drawable/", "").replace(".png", "")
                val resId = binding.root.context.resources.getIdentifier(formattedName, "drawable", binding.root.context.packageName)
                if (resId != 0) {
                    binding.imgDetail.setImageResource(resId)
                }
                
                binding.fabFavorite.setOnClickListener {
                    viewModel.toggleFavorite(item)
                    updateFavoriteIcon(item.isFavorite) // Optimistic update
                }
            }
        }
    }
    
    private fun updateFavoriteIcon(isFavorite: Boolean) {
        val iconRes = if (isFavorite) android.R.drawable.star_on else android.R.drawable.star_off
        binding.fabFavorite.setImageResource(iconRes)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        // Restore global Action Bar
        (activity as? androidx.appcompat.app.AppCompatActivity)?.supportActionBar?.show()
        _binding = null
    }
}
