package com.rugvedsarika.shoplist.ui.items

import android.graphics.Paint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.rugvedsarika.shoplist.data.local.entities.ShoppingItem
import com.rugvedsarika.shoplist.databinding.ItemShoppingBinding

class ItemsAdapter(
    private val onItemChecked: (ShoppingItem, Boolean) -> Unit,
    private val onEditClicked: (ShoppingItem) -> Unit,
    private val onDeleteClicked: (ShoppingItem) -> Unit
) : ListAdapter<ShoppingItem, ItemsAdapter.ItemViewHolder>(ItemDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val binding = ItemShoppingBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ItemViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val item = getItem(position)
        holder.bind(item)
    }

    inner class ItemViewHolder(private val binding: ItemShoppingBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(item: ShoppingItem) {
            binding.tvItemName.text = item.name
            binding.tvItemDetails.text = if (item.quantity.isNotEmpty() && item.category.isNotEmpty()) {
                "${item.quantity} | ${item.category}"
            } else if (item.quantity.isNotEmpty()) {
                item.quantity
            } else {
                item.category
            }

            // Remove listener before setting state to avoid recursion/unwanted triggers
            binding.cbPurchased.setOnCheckedChangeListener(null)
            binding.cbPurchased.isChecked = item.isPurchased

            updateStrikeThrough(item.isPurchased)

            binding.cbPurchased.setOnCheckedChangeListener { _, isChecked ->
                updateStrikeThrough(isChecked)
                onItemChecked(item, isChecked)
            }

            binding.btnEditItem.setOnClickListener {
                onEditClicked(item)
            }

            binding.btnDeleteItem.setOnClickListener {
                onDeleteClicked(item)
            }
        }

        private fun updateStrikeThrough(isPurchased: Boolean) {
            if (isPurchased) {
                binding.tvItemName.paintFlags = binding.tvItemName.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
            } else {
                binding.tvItemName.paintFlags = binding.tvItemName.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
            }
        }
    }

    class ItemDiffCallback : DiffUtil.ItemCallback<ShoppingItem>() {
        override fun areItemsTheSame(oldItem: ShoppingItem, newItem: ShoppingItem): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: ShoppingItem, newItem: ShoppingItem): Boolean {
            return oldItem == newItem
        }
    }
}
