package com.base.scanqrclear.luma

import android.media.tv.TvTrackInfo.TYPE_AUDIO
import android.media.tv.TvTrackInfo.TYPE_VIDEO
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.base.scanqrclear.R
import com.base.scanqrclear.bean.FileBean
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.base.scanqrclear.databinding.ItemCleanDetailBinding
import com.base.scanqrclear.luma.AppHelps.dpToPx
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.request.RequestOptions

class CleanDetailAdapter(
    var data: List<FileBean>,
    private val type: Int = TYPE_VIDEO
) : BaseAdapter<FileBean, ItemCleanDetailBinding>(data) {

    constructor(type: Int = TYPE_VIDEO) : this(emptyList(), type)

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup
    ): ItemCleanDetailBinding {
        return ItemCleanDetailBinding.inflate(layoutInflater, parent, false)
    }

    override fun bind(
        holder: BaseViewHolder<ItemCleanDetailBinding>,
        position: Int,
        item: FileBean
    ) {
        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.getSize(item.length)
            setSelection(holder.binding.ivAudioSelect, item)
            holder.binding.viewLine.visibility = if (position == itemCount - 1) View.GONE else View.VISIBLE
            holder.itemView.setOnClickListener {
                item.isSelected = !item.isSelected
                setSelection(holder.binding.ivAudioSelect, item)
                callback?.onClicked(it, position, 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 = holder.itemView.context.dpToPx(44).toInt()
            val imageWidth = (width - margin) / 3
            holder.binding.ivImage.layoutParams.width = imageWidth
            holder.binding.ivImage.layoutParams.height = imageWidth

            val round = holder.itemView.context.dpToPx(8).toInt()
            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.getSize(item.length)
            holder.binding.ivVideo.visibility = if (type == 0) View.VISIBLE else View.GONE
            setSelection(holder.binding.ivSelect, item)
            holder.itemView.setOnClickListener {
                item.isSelected = !item.isSelected
                setSelection(holder.binding.ivSelect, item)
                callback?.onClicked(it, position, item)
            }
        }
    }

    private fun setSelection(imageView: ImageView, item: FileBean) {
        imageView.setImageResource(if (item.isSelected) R.mipmap.ic_check_s else R.mipmap.ic_check_n)
    }
}