package com.amz.genie.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.amz.genie.R
import com.amz.genie.helpers.Utils.formatDateTime
import com.amz.genie.helpers.Utils.isFemale
import com.amz.genie.models.GeneralThreadItem
import com.amz.genie.models.OfficeTrnAksiKepada
import com.amz.genie.models.OfficeTrnReaksiKepada
import com.google.android.material.imageview.ShapeableImageView

class GeneralAdapter(
    private val kodePegawai: String,
    private val items: MutableList<GeneralThreadItem> = mutableListOf(),
    private val onItemClick: ((GeneralThreadItem) -> Unit)? = null
) : RecyclerView.Adapter<GeneralAdapter.VH>() {

    fun submitList(newItems: List<GeneralThreadItem>) {
        items.clear()
        items.addAll(newItems)
        notifyDataSetChanged()
    }

    inner class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val img: ShapeableImageView = itemView.findViewById(R.id.siv_employee_item_inbox)
        private val tvName: TextView = itemView.findViewById(R.id.tv_employeename_item_inbox)
        private val tvDateTime: TextView = itemView.findViewById(R.id.tv_datetime_item_inbox)
        private val tvJobDesk: TextView = itemView.findViewById(R.id.tv_jobdesk_item_inbox)
        private val tvDesc: TextView = itemView.findViewById(R.id.tv_description_item_inbox)
        private val tvUnread: TextView = itemView.findViewById(R.id.tv_unread_item_inbox)

        fun bind(row: GeneralThreadItem) {
            val tipe = row.tipe.uppercase()
            val aksi = row.aksi
            val reaksi = row.reaksi

            fun countUnreadAksi(kepada: List<OfficeTrnAksiKepada>?): Int {
                return kepada?.count { it.is_aktif == 0 && it.kode_kepada == kodePegawai } ?: 0
            }

            fun countUnreadReaksi(kepada: List<OfficeTrnReaksiKepada>?): Int {
                return kepada?.count { it.is_aktif == 0 && it.kode_kepada == kodePegawai } ?: 0
            }

            if (tipe == "REAKSI" && reaksi != null) {
                tvName.text = reaksi.pembuat.nama
                tvDateTime.text = formatDateTime(reaksi.waktu_buat)
                tvJobDesk.text = reaksi.pembuat.jabatan?.nama
                tvDesc.text = reaksi.uraian?.takeIf { it.isNotBlank() } ?: "-"

                val female = isFemale(reaksi.pembuat.id_kelamin)
                img.setImageResource(if (female) R.drawable.ic_woman else R.drawable.ic_man)

            } else {
                tvName.text = aksi.pembuat.nama
                tvDateTime.text = formatDateTime(aksi.waktu_buat)
                tvJobDesk.text = "${aksi.pembuat.jabatan?.nama} - ${aksi.pembuat.outlet?.nama}"
                tvDesc.text = aksi.aksi_komunikasi_teks
                    ?.nilai
                    ?.takeIf { it.isNotBlank() }
                    ?: aksi.uraian

                val female = isFemale(aksi.pembuat.id_kelamin)
                img.setImageResource(if (female) R.drawable.ic_woman else R.drawable.ic_man)
            }

            val totalUnread = row.unread_count
            if (totalUnread > 0) {
                tvUnread.visibility = View.VISIBLE
                tvUnread.text = totalUnread.toString()
            } else {
                tvUnread.visibility = View.GONE
                tvUnread.text = ""
            }

            itemView.setOnClickListener { onItemClick?.invoke(row) }
        }
    }

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

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

    override fun getItemCount(): Int = items.size
}