package com.base.pdfreaderallpdfreader.ui.pdf

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.pdfreaderallpdfreader.R
import com.base.pdfreaderallpdfreader.ads.admob.AdmobNativeUtils
import com.base.pdfreaderallpdfreader.bean.DocumentBean
import com.base.pdfreaderallpdfreader.bean.DocumentBean.Companion.TYPE_EXCEL
import com.base.pdfreaderallpdfreader.bean.DocumentBean.Companion.TYPE_PDF
import com.base.pdfreaderallpdfreader.bean.DocumentBean.Companion.TYPE_PPT
import com.base.pdfreaderallpdfreader.bean.DocumentBean.Companion.TYPE_WORD
import com.base.pdfreaderallpdfreader.databinding.ItemAdBinding
import com.base.pdfreaderallpdfreader.databinding.ItemDocumentPdfBinding
import com.base.pdfreaderallpdfreader.utils.KotlinExt.toFormatSize
import com.base.pdfreaderallpdfreader.utils.KotlinExt.toFormatTime
import com.base.pdfreaderallpdfreader.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter
import java.io.File

class DocumentPdfAdapter() : BaseQuickAdapter<DocumentBean, DocumentPdfAdapter.DocumentPdfViewHolder>() {

    private val TAG = "DocumentAdapter"

    inner class DocumentPdfViewHolder(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

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: DocumentPdfViewHolder, position: Int, item: DocumentBean?) {
        if (item == null) return
        if (item.isAd) {
            val binding = ItemAdBinding.bind(holder.itemView)
            AdmobNativeUtils.showNativeAd(null, binding.flAd, R.layout.layout_admob_document)
        } else {
            val binding = ItemDocumentPdfBinding.bind(holder.itemView)
            changeIcon(item, binding)
            val file = File(item.path)
            runCatching {
                binding.tvName.text = file.name
                binding.tvTime.text = file.lastModified().toFormatTime()
                binding.tvSize.text = file.length().toFormatSize()
            }
//            LogEx.logDebug(TAG, "uiType=${item.uiType}")
            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.pdf_bookmark_s)
                    } else {
                        binding.ivBookmark.setImageResource(R.mipmap.pdf_bookmark_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)
                    }
                }
            }
        }

    }

    fun changeIcon(item: DocumentBean, binding: ItemDocumentPdfBinding) {
        if (item.type == TYPE_PDF) {
            if (item.state == 0) {
                binding.iv.setImageResource(R.mipmap.r_pdf)
            }
            if (item.state == 1) {
                binding.iv.setImageResource(R.mipmap.rv_pdf_lock)
            }
        }
        if (item.type == TYPE_WORD) {
            binding.iv.setImageResource(R.mipmap.rv_word)
        }
        if (item.type == TYPE_EXCEL) {
            binding.iv.setImageResource(R.mipmap.rv_excel)
        }
        if (item.type == TYPE_PPT) {
            binding.iv.setImageResource(R.mipmap.r_ppt)
        }
    }

    override fun getItemViewType(position: Int, list: List<DocumentBean>): Int {
        val item = list[position]
        return if (item.isAd) 1 else 0
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): DocumentPdfViewHolder {
        return if (viewType == 0) {
            DocumentPdfViewHolder(R.layout.item_document_pdf.inflate(parent))
        } else {
            DocumentPdfViewHolder(R.layout.item_ad.inflate(parent))
        }
    }
}