package com.all.pdfreader.pro.app.ui.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.all.pdfreader.pro.app.R
import com.all.pdfreader.pro.app.databinding.AdapterSelectedResultItemBinding
import com.all.pdfreader.pro.app.model.PdfSplitResultItem
import com.all.pdfreader.pro.app.util.AppUtils.dpToPx
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import java.io.File

class PdfResultAdapter(
    private val list: MutableList<PdfSplitResultItem>
) : RecyclerView.Adapter<PdfResultAdapter.PdfViewHolder>() {

    inner class PdfViewHolder(val binding: AdapterSelectedResultItemBinding) :
        RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = PdfViewHolder(
        AdapterSelectedResultItemBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
    )

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: PdfViewHolder, position: Int) {
        val item = list[position]
        if (item.thumbnailPath != null) {
            Glide.with(holder.binding.root).load(File(item.thumbnailPath))
                .transform(CenterCrop(), RoundedCorners(4.dpToPx(holder.binding.root.context)))
                .into(holder.binding.image)
        }
        if (item.isPassword) {
            holder.binding.lockLayout.visibility = View.VISIBLE
        } else {
            holder.binding.lockLayout.visibility = View.GONE
        }
        holder.binding.nameTv.text = File(item.filePath).name
        holder.binding.pathTv.text =
            holder.binding.root.context.getString(R.string.path_details, item.filePath)
        if (item.isSelected) {
            holder.binding.selectIv.visibility = View.VISIBLE
        } else {
            holder.binding.selectIv.visibility = View.INVISIBLE
        }
        holder.binding.root.setOnClickListener {
            val oldSelectedIndex = list.indexOfFirst { it.isSelected }
            val newSelectedIndex = holder.bindingAdapterPosition
            if (oldSelectedIndex != newSelectedIndex && newSelectedIndex != RecyclerView.NO_POSITION) {
                // 取消之前选中
                if (oldSelectedIndex >= 0) {
                    list[oldSelectedIndex].isSelected = false
                    notifyItemChanged(oldSelectedIndex)
                }
                // 选中当前
                list[newSelectedIndex].isSelected = true
                notifyItemChanged(newSelectedIndex)
            }
        }
    }

    override fun getItemCount(): Int = list.size

    @SuppressLint("NotifyDataSetChanged")
    fun updateAdapter() {
        notifyDataSetChanged()
    }

    fun getSelectedItem(): PdfSplitResultItem? {
        return list.firstOrNull { it.isSelected }
    }

    fun getAllFiles(): List<File> {
        return list.mapNotNull {
            val f = File(it.filePath)
            if (f.exists()) f else null
        }
    }

}
