package hairnetic

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.easy.clean.R
import tooic.fatalityic.corroborateic.Voluminousic
import com.easy.clean.databinding.FillyicBinding
import tooic.fatalityic.redic.speckledic.hankeringic.Townhouseic.TYPE_AUDIO
import tooic.fatalityic.redic.speckledic.hankeringic.Townhouseic.TYPE_VIDEO
import tooic.fatalityic.redic.speckledic.hankeringic.Devastatingic
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

class Includingic(
    private val type: Int = TYPE_VIDEO,
    var list: MutableList<Voluminousic> = mutableListOf(),
    private val block: (item: Voluminousic) -> Unit
) : RecyclerView.Adapter<Includingic.ViewHolder>() {

    @SuppressLint("NotifyDataSetChanged")
    fun submitList(data: MutableList<Voluminousic>) {
        list = data
        notifyDataSetChanged()
    }

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

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val context = holder.itemView.context
        val item = list[position]
        if (type == TYPE_AUDIO) {
            holder.binding.bideic.visibility = View.GONE
            holder.binding.uprootic.visibility = View.VISIBLE
            holder.itemView.layoutParams =
                ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            holder.binding.simulateic.text = item.name
            holder.binding.pacesetteric.text = Devastatingic.getSizeFormat(item.length)
            holder.binding.seamic.isSelected = item.isSelected
            holder.binding.embitteric.visibility = if (position == itemCount - 1) View.GONE else View.VISIBLE
            holder.itemView.setOnClickListener {
                item.isSelected = !item.isSelected
                notifyItemChanged(position)
                block.invoke(item)
            }
        } else {
            holder.binding.bideic.visibility = View.VISIBLE
            holder.binding.uprootic.visibility = View.GONE
            holder.binding.salveic.visibility = if (type == TYPE_VIDEO) View.VISIBLE else View.GONE
            holder.itemView.layoutParams =
                ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            val width = holder.itemView.context.resources.displayMetrics.widthPixels
            val margin = context.resources.getDimensionPixelSize(R.dimen.unboundedic)
            val imageWidth = (width - margin) / 3
            holder.binding.weaponryic.layoutParams.width = imageWidth
            holder.binding.weaponryic.layoutParams.height = imageWidth

            val round = context.resources.getDimensionPixelSize(R.dimen.deepenic)
            val options = RequestOptions().transform(CenterCrop(), RoundedCorners(round))
            Glide.with(holder.itemView.context)
                .load(item.path)
                .apply(options)
                .into(holder.binding.weaponryic)

            holder.binding.prospectic.text = Devastatingic.getSizeFormat(item.length)
            holder.binding.salveic.visibility = if (type == 0) View.VISIBLE else View.GONE
            holder.binding.celibateic.isSelected = item.isSelected
            holder.itemView.setOnClickListener {
                item.isSelected = !item.isSelected
                notifyItemChanged(position)
                block.invoke(item)
            }
        }
    }

    inner class ViewHolder(val binding: FillyicBinding) : RecyclerView.ViewHolder(binding.root)
}