package com.base.pdfviewerscannerwhite.ui.document.pdf

import android.annotation.SuppressLint
import android.content.Context
import android.net.Uri
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.bean.PdfPageBean
import com.base.pdfviewerscannerwhite.databinding.ItemPdfPagerBinding
import com.base.pdfviewerscannerwhite.databinding.ItemPdfPagerSplitBinding
import com.base.pdfviewerscannerwhite.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit


class PdfPagerAdapter(
    val pdfPath: String,
    val uri: String? = null,
    val itemLayout: Int = R.layout.item_pdf_pager,
) : BaseQuickAdapter<PdfPageBean, PdfPagerAdapter.PdfPagerViewHolder>() {

    var mPassword: String? = null
    var selectAction: ((enable: Boolean, allSelect: Boolean) -> Unit)? = null
    var clickAction: ((pageIndex: Int) -> Unit)? = null

    inner class PdfPagerViewHolder(view: View) : ViewHolder(view)

    var corePoolSize = 4 // 核心线程数
    var maximumPoolSize = 10 // 最大线程数
    var keepAliveTime: Long = 120 // 非核心线程空闲存活时间
    var threadPoolExecutor = ThreadPoolExecutor(
        corePoolSize,
        maximumPoolSize,
        keepAliveTime,
        TimeUnit.SECONDS,
        LinkedBlockingQueue()
    )

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: PdfPagerViewHolder, position: Int, item: PdfPageBean?) {
        if (item == null) return
        when (itemLayout) {
            R.layout.item_pdf_pager -> {
                val binding = ItemPdfPagerBinding.bind(holder.itemView)
                val context = holder.itemView.context
                binding.tvPagerIndex.isSelected = item.isSelect
                binding.tvPagerIndex.text = (item.pageIndex + 1).toString()
                binding.flBorder.isSelected = item.isSelect

                if (item.pageDrawable != null) {
                    binding.ivPager.setImageDrawable(item.pageDrawable)
                } else {
                    loadPagerDrawable(context, item, binding.root, binding.ivPager)
                }
                binding.root.setOnClickListener {
                    clickAction?.invoke(item.pageIndex)
                }
            }

            R.layout.item_pdf_pager_split -> {
                val binding = ItemPdfPagerSplitBinding.bind(holder.itemView)
                binding.ivSelector.isSelected = item.isSelect
                if (item.pageDrawable != null) {
                    binding.ivPager.setImageDrawable(item.pageDrawable)
                } else {
                    loadPagerDrawable(context, item, binding.root, binding.ivPager, 1.5f)
                }
                binding.ivSelector.setOnClickListener {
                    item.isSelect = !item.isSelect
                    notifyItemChanged(position, "aaa")
                    selectAction?.invoke(items.any { it.isSelect }, items.all { it.isSelect })
                }
            }
        }


    }

    private fun loadPagerDrawable(
        context: Context,
        item: PdfPageBean,
        itemView: View,
        iv: ImageView,
        scale: Float = 1f
    ) {
        threadPoolExecutor.execute {
            runCatching {
                val drawable = PdfBoxUtils.getPdfDrawablePage(context, pdfPath, mPassword, uri, item.pageIndex, scale)
                item.pageDrawable = drawable
                itemView.post {
                    item.pageDrawable?.let {
                        iv.setImageDrawable(it)
                    }
                }
            }
        }
    }


    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): PdfPagerViewHolder {
        return PdfPagerViewHolder(itemLayout.inflate(parent))
    }

    @SuppressLint("NotifyDataSetChanged")
    fun toggleSelect(select: Boolean) {
        items.forEach { it.isSelect = select }
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun changeSelectPager(page: Int) {
        runCatching {
            items.forEach { it.isSelect = false }
            items[page].isSelect = true
        }
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setPassword(password: String) {
        mPassword = password
        notifyDataSetChanged()
    }
}