package com.simplecleaner.app.ui.largefile

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
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.simplecleaner.app.R
import com.simplecleaner.app.bean.FileBean
import com.simplecleaner.app.databinding.ItemLargeFileCleanBinding
import com.simplecleaner.app.utils.MineTypeUtils
import com.simplecleaner.app.utils.Utils

class LargeFileCleanAdapter(
    private var data: List<FileBean>,
    private val onItemClick: (Int) -> Unit
) : RecyclerView.Adapter<LargeFileCleanAdapter.ViewHolder>() {
    inner class ViewHolder(val binding: ItemLargeFileCleanBinding) : RecyclerView.ViewHolder(binding.root) {
        init {
            binding.root.setOnClickListener {
                val position = bindingAdapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    onItemClick(position)
                }
            }
        }
    }

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

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = data[position]
        holder.binding.ivVideo.isVisible = MineTypeUtils.isVideo(item.mineType)
        if (MineTypeUtils.isImage(item.mineType) || MineTypeUtils.isVideo(item.mineType)) {
            val round = holder.itemView.resources.getDimensionPixelSize(R.dimen.dp_4)
            val options = RequestOptions().transform(CenterCrop(), RoundedCorners(round))
            Glide.with(holder.itemView.context)
                .load(item.path)
                .apply(options)
                .into(holder.binding.ivIcon)
        } else if (MineTypeUtils.isAudio(item.mineType)) {
            holder.binding.ivIcon.setImageResource(R.drawable.icon_yinping_large)
        } else if (MineTypeUtils.isApk(item.mineType)) {
            holder.binding.ivIcon.setImageResource(R.drawable.icon_app_large)
        } else if (MineTypeUtils.isDoc(item.mineType)) {
            holder.binding.ivIcon.setImageResource(R.drawable.icon_wenjianx_large)
        } else {
            holder.binding.ivIcon.setImageResource(R.drawable.icon_yingyong_large)
        }
        holder.binding.tvName.text = item.name
        holder.binding.tvLength.text = Utils.getSizeFormat(item.length)
        holder.binding.ivSelect.isSelected = item.isSelected
        holder.binding.viewLine.visibility = if (position == itemCount - 1) View.GONE else View.VISIBLE
    }

    fun setData(newData: List<FileBean>) {
        data = newData
        notifyDataSetChanged()
    }
} 