package com.ux.video.file.filerecovery.result

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
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
import com.ux.video.file.filerecovery.R
import com.ux.video.file.filerecovery.base.BaseAdapter
import com.ux.video.file.filerecovery.databinding.ScanResultAdapterBinding
import com.ux.video.file.filerecovery.db.ResultData
import com.ux.video.file.filerecovery.db.ResultDataFiles
import com.ux.video.file.filerecovery.db.targetFile
import com.ux.video.file.filerecovery.utils.ExtendFunctions.dpToPx
import com.ux.video.file.filerecovery.utils.FileType
import com.ux.video.file.filerecovery.utils.ScanManager
import java.io.File

/**
 * 图片或者视频的扫描结果汇总适配器
 */
class ScanResultPhotoVideoAdapter(
    mContext: Context,
    var fileType: FileType,
    var onClickItem: (allFiles: ArrayList<ResultDataFiles>) -> Unit
) :
    BaseAdapter<ResultData, ScanResultAdapterBinding>(mContext) {
    override fun getViewBinding(parent: ViewGroup): ScanResultAdapterBinding =
        ScanResultAdapterBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )

    @SuppressLint("SetTextI18n")
    override fun bindItem(
        holder: VHolder<ScanResultAdapterBinding>,
        item: ResultData
    ) {

        holder.vb.run {

            val imageViews = listOf(im1, im2, im3)
            item.run {
                imageViewEnter.setOnClickListener { onClickItem(allFiles) }
                textDirNameCount.text = dirName
                textFileCounts.text = "(${allFiles.size})"
                val takeFiles = allFiles.take(3)
                imageViews.forEachIndexed { index, imageView ->
                    if (index < takeFiles.size) {
                        takeFiles[index].targetFile().let {
                            loadImageView(mContext, it, imageView)
                        }
                    } else {
                        imageView.setImageDrawable(null)
                    }
                    imageView.setOnClickListener { onClickItem(allFiles) }

                }
            }
        }


    }

    private fun loadImageView(context: Context, file: File, imageView: ImageView) {
        ScanManager.showLog(
            "加载图片",
            "-----loadImageView--path = ${file.path}"
        )
        Glide.with(context)
            .load(file)
            .apply(
                RequestOptions()
                    .transform(CenterCrop(), RoundedCorners(8.dpToPx(context)))
            )
            .error(R.drawable.photo_place_holder)
            .into(imageView)
    }
}