package com.base.pdfviewerscannerwhite.ui.main

import android.annotation.SuppressLint
import android.content.Context
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.DocumentBean
import com.base.pdfviewerscannerwhite.bean.DocumentBean.Companion.TYPE_EXCEL
import com.base.pdfviewerscannerwhite.bean.DocumentBean.Companion.TYPE_PDF
import com.base.pdfviewerscannerwhite.bean.DocumentBean.Companion.TYPE_PPT
import com.base.pdfviewerscannerwhite.bean.DocumentBean.Companion.TYPE_WORD
import com.base.pdfviewerscannerwhite.databinding.ItemDocumentBinding
import com.base.pdfviewerscannerwhite.ui.document.pdf.PdfBoxUtils
import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatSize
import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime
import com.base.pdfviewerscannerwhite.utils.LogEx
import com.base.pdfviewerscannerwhite.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter
import java.io.File
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit

class DocumentAdapter : BaseQuickAdapter<DocumentBean, DocumentAdapter.DocumentViewHolder>() {

    private val TAG = "DocumentAdapter"

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


    var itemClick: ((item: DocumentBean) -> Unit)? = null
    var bookmarkAction: ((addRemove: Boolean, path: String) -> Unit)? = null
    var moreAction: ((item: DocumentBean) -> Unit)? = null
    var selectAction: ((size: Int) -> Unit)? = null

    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: DocumentViewHolder, position: Int, item: DocumentBean?) {
        if (item == null) return
        val binding = ItemDocumentBinding.bind(holder.itemView)
        if (item.type == TYPE_PDF ) {
            if (item.state==0){
                binding.iv.setImageResource(R.mipmap.h_pdfiocn)
            }
            if (item.state==1){
                binding.iv.setImageResource(R.mipmap.suoding)
            }
        }
        if (item.type == TYPE_WORD) {
            binding.iv.setImageResource(R.mipmap.h_wordfiocn)
        }
        if (item.type == TYPE_EXCEL) {
            binding.iv.setImageResource(R.mipmap.h_exceliocn)
        }
        if (item.type == TYPE_PPT) {
            binding.iv.setImageResource(R.mipmap.h_pptfiocn)
        }

        val file = File(item.path)
        runCatching {
            binding.tvName.text = file.name
            binding.tvInfo.text = file.lastModified().toFormatTime() + " " + file.length().toFormatSize()
        }

        when (item.uiType) {
            0 -> {//首页
                binding.flSelect.visibility = View.GONE
                binding.flMore.visibility = View.VISIBLE
                binding.flBookmark.visibility = View.VISIBLE

                if (item.isBookmarked) {
                    binding.ivBookmark.setImageResource(R.mipmap.h_soucang_s)
                } else {
                    binding.ivBookmark.setImageResource(R.mipmap.h_soucang_n)
                }

                binding.flBookmark.setOnClickListener {
                    if (item.isBookmarked) {
                        bookmarkAction?.invoke(false, item.path)
                    } else {
                        bookmarkAction?.invoke(true, item.path)
                    }
                    item.isBookmarked = !item.isBookmarked
                    notifyItemChanged(position, "aaaa")
                }
                binding.flMore.setOnClickListener {
                    moreAction?.invoke(item)
                }
                binding.root.setOnClickListener {
                    itemClick?.invoke(item)
                }
            }

            1 -> {//合并选择
                binding.flMore.visibility = View.INVISIBLE
                binding.flBookmark.visibility = View.INVISIBLE
                binding.flSelect.visibility = View.VISIBLE
                binding.ivSelector.isSelected = item.isSelect
                binding.flSelect.setOnClickListener {
                    item.isSelect = !item.isSelect
                    notifyItemChanged(position, "aaa")
                    selectAction?.invoke(items.filter { it.isSelect }.size)
                }
                binding.root.setOnClickListener {
                    item.isSelect = !item.isSelect
                    notifyItemChanged(position, "aaa")
                    selectAction?.invoke(items.filter { it.isSelect }.size)
                }
            }

            2 -> {//拆分选择
                binding.flBookmark.visibility = View.GONE
                binding.flMore.visibility = View.INVISIBLE
                binding.root.setOnClickListener {
                    itemClick?.invoke(item)
                }
            }

            3 -> {//解锁模式选择
                binding.flBookmark.visibility = View.GONE
                binding.flMore.visibility = View.INVISIBLE
                binding.root.setOnClickListener {
                    itemClick?.invoke(item)
                }
            }

            4 -> {//搜索选择
                binding.flBookmark.visibility = View.GONE
                binding.flMore.setOnClickListener {
                    moreAction?.invoke(item)
                }
                binding.root.setOnClickListener {
                    itemClick?.invoke(item)
                }
            }
        }
    }



    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): DocumentViewHolder {
        return DocumentViewHolder(R.layout.item_document.inflate(parent))
    }
}