package com.base.appzxhy.ui.photocompression

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.base.appzxhy.R
import com.base.appzxhy.bean.FileBean
import com.base.appzxhy.databinding.ItemCleanDetailBinding
import com.base.appzxhy.utils.MineTypeUtils.TYPE_AUDIO
import com.base.appzxhy.utils.MineTypeUtils.TYPE_VIDEO
import com.base.appzxhy.utils.Utils
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 CleanDetailAdapter(
    private val type: Int = TYPE_VIDEO,
    var list: MutableList<FileBean> = mutableListOf(),
    private val block: (item: FileBean) -> Unit
) : RecyclerView.Adapter<CleanDetailAdapter.ViewHolder>() {

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemCleanDetailBinding.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.clVideoImage.visibility = View.GONE
            holder.binding.clAudio.visibility = View.VISIBLE
            holder.itemView.layoutParams =
                ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            holder.binding.tvAudioName.text = item.name
            holder.binding.tvAudioSize.text = Utils.getSizeFormat(item.length)
            holder.binding.ivAudioSelect.isSelected = item.isSelected
            holder.binding.viewLine.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.clVideoImage.visibility = View.VISIBLE
            holder.binding.clAudio.visibility = View.GONE
            holder.binding.ivVideo.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.dp_45)
            val imageWidth = (width - margin) / 3
            holder.binding.ivImage.layoutParams.width = imageWidth
            holder.binding.ivImage.layoutParams.height = imageWidth

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

            holder.binding.tvSize.text = Utils.getSizeFormat(item.length)
            holder.binding.ivVideo.visibility = if (type == 0) View.VISIBLE else View.GONE
            holder.binding.ivSelect.isSelected = item.isSelected
            holder.itemView.setOnClickListener {
                item.isSelected = !item.isSelected
                notifyItemChanged(position)
                block.invoke(item)
            }
        }
    }

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