package com.zxhy.fastfilemanagerpro.kokoInternalstorage

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.Paint
import android.os.Environment
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.zxhy.fastfilemanagerpro.R
import com.zxhy.fastfilemanagerpro.databinding.ItemKokoListFileBinding
import com.zxhy.fastfilemanagerpro.kokoadapter.CommonAdapter
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako.Companion.koFileDatako
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako.Companion.loadImageIcon
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako.Companion.toFile
import com.zxhy.fastfilemanagerpro.kokotools.XmlEx.inflate
import java.io.File


class KoInternalkoAdapter(
    private val context: Context,
    private val allSelect: (flag: Boolean) -> Unit,
    private val pageCount: ((count: Int) -> Unit)? = null
) : CommonAdapter<KoInternalkoAdapter.KoInternalkoViewHolder>() {

    private var selectMode = false
    private var currentDir = Environment.getExternalStorageDirectory()
    private var cannotFiles = arrayListOf<String>()
    private var canPage = true

    fun setCanPage(canPage: Boolean) {
        this.canPage = canPage
    }

    fun getCurrentDir(): File {
        return currentDir
    }

    @SuppressLint("NotifyDataSetChanged")
    fun showSelectMode(show: Boolean, refresh: Boolean = true) {
        selectMode = show
        if (refresh) {
            notifyDataSetChanged()
        }
    }

    class KoInternalkoViewHolder(view: View) : ViewHolder(view) {
        val binding = ItemKokoListFileBinding.bind(view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): KoInternalkoViewHolder {
        return KoInternalkoViewHolder(R.layout.item_koko_list_file.inflate(parent))
    }

    override fun onBindViewHolder(holder: KoInternalkoViewHolder, position: Int) {

    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: KoInternalkoViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        val context = holder.binding.root.context
        val data = fileList[position]
        if (payloads.isEmpty()) {
            holder.binding.apply {

                data.loadImageIcon(context, ivIcon)

                if (cannotFiles.contains(data.toFile().absolutePath)) {
                    vOverly.visibility = View.VISIBLE
                    root.setOnClickListener { }
                } else {
                    vOverly.visibility = View.GONE
                    root.setOnClickListener {
                        if (canPage && data.toFile().isDirectory) {
                            pageNext(context, data)
                        }
                    }
                }

                tvName.text = data.name
                tvInfo.text = "${data.timeE}  ${data.sizeF}"
                if (selectMode) {
                    ivSelector.visibility = View.VISIBLE
                    ivSelector.isSelected = data.isSelect
                    ivSelector.setOnClickListener {
                        data.isSelect = !data.isSelect
                        ivSelector.isSelected = data.isSelect
                        notifyItemChanged(position, "局部刷洗")
                        allSelect.invoke(fileList.all { it.isSelect })
                    }
                } else {
                    ivSelector.visibility = View.GONE
                }
            }
        } else {
            holder.binding.apply {
                ivSelector.isSelected = data.isSelect
            }
            super.onBindViewHolder(holder, position, payloads)
        }
    }

    fun pageRoot() {
        currentDir = Environment.getExternalStorageDirectory()
        val list = currentDir.listFiles()?.map { it.koFileDatako(context) } ?: listOf()
        setData(list)
    }

    private fun pageNext(context: Context, data: KoFileDatako) {
        val list = data.toFile().listFiles()?.map { it.koFileDatako(context) } ?: listOf()
        setData(list)
        currentDir = data.toFile()
        pageCount?.invoke(list.size)
    }

    /**
     * @return 是否是根目录
     */
    fun pageBefore(): Boolean {

        return if (currentDir.absolutePath == Environment.getExternalStorageDirectory().absolutePath) {
            true
        } else {
            val parentDir = currentDir.parentFile ?: File("")
            if (parentDir.exists()) {
                val data = parentDir.listFiles()?.map { it.koFileDatako(context) } ?: listOf()
                setData(data)
                currentDir = parentDir
                pageCount?.invoke(data.size)
            }
            false
        }
    }

    fun notifyCurrentDir() {
        val data = currentDir.listFiles()?.map { it.koFileDatako(context) } ?: listOf()
        setData(data)
        pageCount?.invoke(itemCount)
    }


    @SuppressLint("NotifyDataSetChanged")
    fun setCannotDir(dirs: List<File>) {
        cannotFiles.addAll(dirs.map { it.absolutePath })
    }

    fun clearCannotDir() {
        cannotFiles.clear()
    }

    fun aa(view: View, context: Context) {
        val paint = Paint()
        val cm = ColorMatrix()
        cm.setSaturation(0f)
        paint.setColorFilter(ColorMatrixColorFilter(cm))
        view.setLayerType(View.LAYER_TYPE_HARDWARE, paint)
    }
}