package com.example.defenseapplication.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.defenseapplication.R
import com.example.defenseapplication.bean.FamilyBean
import com.example.defenseapplication.databinding.ItemFamilyBinding

class FamilyAdapter(
    private val onItemClick: (String) -> Unit
) : RecyclerView.Adapter<FamilyAdapter.FamilyViewHolder>() {

    private val items = mutableListOf<FamilyBean>()
    private var selectedFamily: String? = null

    fun submitList(familyList: List<FamilyBean>, currentFamily: String) {
        items.clear()
        items.addAll(familyList)
        selectedFamily = currentFamily
        notifyDataSetChanged()
    }

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

    override fun onBindViewHolder(holder: FamilyViewHolder, position: Int) {
        holder.bind(items[position])
    }

    override fun getItemCount(): Int = items.size

    inner class FamilyViewHolder(private val binding: ItemFamilyBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(item: FamilyBean) {
            val isAdmin = item.role == "appadmin"
            if (isAdmin) {
                binding.tvFamilyName.text = binding.root.context.getString(R.string.my_family)
            } else {
                binding.tvFamilyName.text = "${item.parentName}${binding.root.context.getString(R.string.family_with_normal)}"
            }
            binding.tvFamilyRole.text = getRoleText(item.role)

            val isSelected = item.parentName == selectedFamily
            updateSelectedState(isSelected)

            binding.root.setOnClickListener { onItemClick(item.parentName) }
        }

        private fun getRoleText(role: String): String {
            return when (role) {
                "appadmin" -> binding.root.context.getString(R.string.admin_user)
                else -> binding.root.context.getString(R.string.normal_user)
            }
        }

        private fun updateSelectedState(isSelected: Boolean) {
            if (isSelected) {
                binding.layoutFamilyItem.setBackgroundResource(R.drawable.bg_family_selected)
                binding.tvFamilyName.setTextColor(binding.root.context.getColor(R.color.white))
                binding.tvFamilyRole.setTextColor(binding.root.context.getColor(R.color.white))
                binding.ivCheck.visibility = View.VISIBLE
                binding.ivCheck.setImageResource(R.drawable.ic_check_white)
            } else {
                binding.layoutFamilyItem.setBackgroundResource(0)
                binding.tvFamilyName.setTextColor(binding.root.context.getColor(R.color.black))
                binding.tvFamilyRole.setTextColor(binding.root.context.getColor(R.color.gray))
                binding.ivCheck.visibility = View.GONE
            }
        }
    }
}