package com.rugvedsarika.toolbox

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.rugvedsarika.toolbox.databinding.FragmentFavoriteToolsBinding

class FavoriteToolsFragment : Fragment() {

    private var _binding: FragmentFavoriteToolsBinding? = null
    private val binding get() = _binding!!
    private lateinit var preferenceManager: PreferenceManager

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        preferenceManager = PreferenceManager(requireContext())

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

        binding.recyclerViewFavoriteSelection.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = FavoriteSelectionAdapter(ToolList.allTools, preferenceManager)
        }
    }

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

class FavoriteSelectionAdapter(
    private val tools: List<ToolItem>,
    private val preferenceManager: PreferenceManager
) : RecyclerView.Adapter<FavoriteSelectionAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val icon: ImageView = view.findViewById(R.id.tool_icon)
        val name: TextView = view.findViewById(R.id.tool_name)
        val checkbox: CheckBox = view.findViewById(R.id.checkbox_favorite)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_favorite_selection, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val tool = tools[position]
        holder.name.text = tool.name
        holder.icon.setImageResource(tool.iconRes)
        
        // Remove listener before setting checked state to avoid triggering it
        holder.checkbox.setOnCheckedChangeListener(null)
        holder.checkbox.isChecked = preferenceManager.isFavorite(tool.id.toString())
        
        holder.checkbox.setOnCheckedChangeListener { _, isChecked ->
            preferenceManager.toggleFavorite(tool.id.toString())
        }

        holder.itemView.setOnClickListener {
            holder.checkbox.isChecked = !holder.checkbox.isChecked
        }
    }

    override fun getItemCount() = tools.size
}
