package com.base.datarecovery.adapter

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.datarecovery.R
import com.base.datarecovery.bean.FolderBean
import com.base.datarecovery.bean.RecoveryBean
import com.base.datarecovery.databinding.ItemMediaBinding
import com.base.datarecovery.help.KotlinExt.toFormatSize
import com.base.datarecovery.view.XmlEx.inflate
import com.bumptech.glide.Glide
import java.io.File

class FileMediaColumnsAdapter(
    val columns: Int,
    val select: (all: Boolean, size: Int) -> Unit,
) : RecyclerView.Adapter<FileMediaColumnsAdapter.MCV>() {

    private val beanList = arrayListOf<FolderBean>()
    private var hideThumbnails: Boolean = false

    class MCV(view: View) : ViewHolder(view)

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

    override fun getItemCount(): Int {
        return beanList.size
    }

    override fun onBindViewHolder(holder: MCV, position: Int) {
        val binding = ItemMediaBinding.bind(holder.itemView)
        val context = holder.itemView.context
        val bean = beanList[position]

        binding.tvTime.text = bean.time
        binding.ivSelector.isSelected = bean.isSelect


        val subAdapter = MediaColumnsAdapter()
        binding.rvMediaColumns.layoutManager = GridLayoutManager(context, columns)
        binding.rvMediaColumns.adapter = subAdapter
        val list = if (hideThumbnails) bean.recoveryList.filter { !it.isThumbnails } else bean.recoveryList
        if (list.isEmpty()) {
            binding.flTittle.visibility = View.GONE
        } else {
            binding.flTittle.visibility = View.VISIBLE
            subAdapter.setData(list)
        }

        binding.ivSelector.setOnClickListener {
            bean.isSelect = !bean.isSelect
            binding.ivSelector.isSelected = bean.isSelect
            notifyItemChanged(position)
            itemClick(bean.isSelect, subAdapter)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(data: List<FolderBean>) {
        beanList.clear()
        beanList.addAll(data)
        notifyDataSetChanged()
    }

    inner class MediaColumnsAdapter : RecyclerView.Adapter<MediaColumnsAdapter.CS>() {
        private val list = arrayListOf<RecoveryBean>()

        inner class CS(view: View) : ViewHolder(view) {
            val iv = view.findViewById<ImageView>(R.id.iv)
            val ivSelector = view.findViewById<ImageView>(R.id.iv_selector)
            val tvSize = view.findViewById<TextView>(R.id.tv_size)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CS {
            val layout = when (columns) {
                2 -> R.layout.item_media_columns2
                3 -> R.layout.item_media_columns3
                4 -> R.layout.item_media_columns4
                else -> R.layout.item_media_columns2
            }
            return CS(layout.inflate(parent))
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: CS, position: Int) {
        }

        override fun onBindViewHolder(holder: CS, position: Int, payloads: MutableList<Any>) {

            val context = holder.itemView.context
            val data = list[position]

            if (payloads.isEmpty()) {
                Glide.with(context).asBitmap().load(data.path).centerCrop().into(holder.iv)
                holder.iv.tag = data.path
                holder.ivSelector.isSelected = data.isSelect
                holder.ivSelector.setOnClickListener {
                    data.isSelect = !data.isSelect
                    holder.ivSelector.isSelected = data.isSelect
                    notifyItemChanged(position, "changeIv")

                    subItemClick(data)
                }
                val file = File(data.path)
                holder.tvSize.text = file.length().toFormatSize()
            } else {
                holder.ivSelector.isSelected = data.isSelect
                super.onBindViewHolder(holder, position, payloads)
            }
        }

        @SuppressLint("NotifyDataSetChanged")
        fun setData(bean: List<RecoveryBean>) {
            list.clear()
            list.addAll(bean)
            notifyDataSetChanged()
        }

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


    }

    @SuppressLint("NotifyDataSetChanged")
    fun subItemClick(data: RecoveryBean) {
        val folderBean = beanList.find { it.recoveryList.contains(data) }

        folderBean?.let { folder ->
            val select = folder.recoveryList.all { it.isSelect }
            if (folder.isSelect != select) {
                folder.isSelect = select
                notifyDataSetChanged()
            }
        }
        val all = beanList.all { it.isSelect }
        val size = beanList.sumOf { it.recoveryList.filter { bean -> bean.isSelect }.size }
        select.invoke(all, size)
    }

    private fun itemClick(isSelect: Boolean, subAdapter: MediaColumnsAdapter) {
        subAdapter.toggleSelect(isSelect)

        val all = beanList.all { it.isSelect }
        val size = beanList.sumOf { it.recoveryList.filter { bean -> bean.isSelect }.size }
        select.invoke(all, size)
    }

    @SuppressLint("NotifyDataSetChanged")
    fun toggleThumbnails(hideThumbnails: Boolean) {
        this.hideThumbnails = hideThumbnails
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun toggleAllSelect(selected: Boolean) {
        beanList.forEach {
            it.isSelect = selected
            it.recoveryList.forEach { bean -> bean.isSelect = selected }
        }
        notifyDataSetChanged()
    }

}