package com.rugvedsarika.toolbox

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

class HomeFragment : Fragment() {

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

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

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

        val favoriteIds = preferenceManager.getFavoriteToolIds()
        val tools = ToolList.allTools.sortedByDescending { favoriteIds.contains(it.id.toString()) }

        binding.recyclerViewTools.apply {
            layoutManager = GridLayoutManager(requireContext(), 2)
            adapter = ToolAdapter(tools, favoriteIds) { tool ->
                when (tool.id) {
                    1 -> findNavController().navigate(R.id.action_homeFragment_to_standardCalculatorFragment)
                    2 -> findNavController().navigate(R.id.action_homeFragment_to_autoTotalCalculatorFragment)
                    3 -> findNavController().navigate(R.id.action_homeFragment_to_cashCounterFragment)
                    4 -> findNavController().navigate(R.id.action_homeFragment_to_areaConverterFragment)
                    5 -> findNavController().navigate(R.id.action_homeFragment_to_lengthConverterFragment)
                    6 -> findNavController().navigate(R.id.action_homeFragment_to_weightConverterFragment)
                    7 -> findNavController().navigate(R.id.action_homeFragment_to_notesFragment)
                    8 -> findNavController().navigate(R.id.action_homeFragment_to_checklistFragment)
                    9 -> findNavController().navigate(R.id.action_homeFragment_to_unitTablesFragment)
                    10 -> findNavController().navigate(R.id.action_homeFragment_to_emiCalculatorFragment)
                    11 -> findNavController().navigate(R.id.action_homeFragment_to_ageCalculatorFragment)
                    12 -> findNavController().navigate(R.id.action_homeFragment_to_passwordGeneratorFragment)
                    13 -> findNavController().navigate(R.id.action_homeFragment_to_discountCalculatorFragment)
                    14 -> findNavController().navigate(R.id.action_homeFragment_to_gstCalculatorFragment)
                    15 -> findNavController().navigate(R.id.action_homeFragment_to_temperatureConverterFragment)
                }
            }
        }

        binding.toolbar.inflateMenu(R.menu.menu_home)
        binding.toolbar.setOnMenuItemClickListener { menuItem ->
            when (menuItem.itemId) {
                R.id.action_settings -> {
                    findNavController().navigate(R.id.action_homeFragment_to_settingsFragment)
                    true
                }
                else -> false
            }
        }
    }

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

class ToolAdapter(
    private val tools: List<ToolItem>,
    private val favoriteIds: Set<String>,
    private val onToolClick: (ToolItem) -> Unit
) : RecyclerView.Adapter<ToolAdapter.ToolViewHolder>() {

    class ToolViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val icon: ImageView = view.findViewById(R.id.tool_icon)
        val name: TextView = view.findViewById(R.id.tool_name)
        val favoriteStar: ImageView = view.findViewById(R.id.img_favorite_star)
    }

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

    override fun onBindViewHolder(holder: ToolViewHolder, position: Int) {
        val tool = tools[position]
        holder.name.text = tool.name
        holder.icon.setImageResource(tool.iconRes)
        holder.favoriteStar.visibility = if (favoriteIds.contains(tool.id.toString())) View.VISIBLE else View.GONE
        holder.itemView.setOnClickListener { onToolClick(tool) }
    }

    override fun getItemCount() = tools.size
}
