package com.base.pdfviewerscannerwhite.ui.main

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.ads.admob.AdmobNativeUtils
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.ItemAdBinding
import com.base.pdfviewerscannerwhite.databinding.ItemDocumentBinding
import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatSize
import com.base.pdfviewerscannerwhite.utils.KotlinExt.toFormatTime
import com.base.pdfviewerscannerwhite.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter
import java.io.File

class DocumentAdapter(val activity: Activity) : 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

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: DocumentViewHolder, position: Int, item: DocumentBean?) {
        if (item == null) return
        if (item.isAd) {
            val binding = ItemAdBinding.bind(holder.itemView)
            AdmobNativeUtils.showNativeAd(activity, binding.flAd, R.layout.layout_admob_document)
        } else {
            val binding = ItemDocumentBinding.bind(holder.itemView)
            changeIcon(item, binding)
            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)
                    }
                }
            }
        }

    }

    fun changeIcon(item: DocumentBean, binding: ItemDocumentBinding) {
        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)
        }
    }

    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): DocumentViewHolder {
        return if (viewType == 0) {
            DocumentViewHolder(R.layout.item_document.inflate(parent))
        } else {
            DocumentViewHolder(R.layout.item_ad.inflate(parent))
        }
    }
}