package com.base.filerecoveryrecyclebin.adapter

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.filerecoveryrecyclebin.R
import com.base.filerecoveryrecyclebin.bean.ScreenPhotoBean
import com.base.filerecoveryrecyclebin.bean.ScreenshotBean
import com.base.filerecoveryrecyclebin.databinding.ItemScreenPhotoBinding
import com.base.filerecoveryrecyclebin.databinding.ItemScreenshotBinding
import com.base.filerecoveryrecyclebin.view.XmlEx.inflate
import com.bumptech.glide.Glide


class ScreenShotAdapter(
    val select: (size: Int) -> Unit
) : RecyclerView.Adapter<ScreenShotAdapter.SSS>() {

    var beans = arrayListOf<ScreenshotBean>()

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


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

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

    @SuppressLint("NotifyDataSetChanged")
    override fun onBindViewHolder(holder: SSS, position: Int) {
        val binding = ItemScreenshotBinding.bind(holder.itemView)
        val context = holder.itemView.context
        val bean = beans[position]
        binding.tvSelectAll.text = if (bean.isSelect) "UNSELECT ALL" else "SELECT ALL"
        binding.tvSelectAll.setOnClickListener {
            bean.isSelect = !bean.isSelect
            bean.screenPhotoBean.forEach { subBean -> subBean.isSelect = bean.isSelect }
            notifyItemChanged(position)
            select.invoke(getSelectDataSize())
        }
        binding.tvTime.text = bean.time
        binding.group.removeAllViews()

        bean.screenPhotoBean.forEach { screenPhotoBean ->

            val itemView = R.layout.item_screen_photo.inflate(binding.group)

            val photoBinding = ItemScreenPhotoBinding.bind(itemView)

            Glide.with(context).load(screenPhotoBean.file).centerCrop().into(photoBinding.iv)
            photoBinding.ivSelector.isSelected = screenPhotoBean.isSelect
            arrayOf(photoBinding.root, photoBinding.ivSelector).forEach {
                it.setOnClickListener {
                    screenPhotoBean.isSelect = !screenPhotoBean.isSelect
                    photoBinding.ivSelector.isSelected = screenPhotoBean.isSelect
                    bean.isSelect = bean.screenPhotoBean.all { it.isSelect }
                    notifyDataSetChanged()
                    select.invoke(getSelectDataSize())
                }
            }
            binding.group.addView(itemView)
        }

        if (bean.screenPhotoBean.size < 3) {
            repeat(2) {
                val itemView = R.layout.item_screen_photo.inflate(binding.group)
                itemView.visibility = View.INVISIBLE
                binding.group.addView(itemView)
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(screenshotBean: List<ScreenshotBean>) {
        beans.clear()
        beans.addAll(screenshotBean)
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun toggleSelect(selected: Boolean) {
        beans.forEach {
            it.isSelect = selected
            it.screenPhotoBean.forEach {
                it.isSelect = selected
            }
        }
        notifyDataSetChanged()
    }

    fun isAllSelect(): Boolean {
        return beans.all { bean -> bean.screenPhotoBean.all { it.isSelect } }
    }

    fun getSelectDataSize(): Int {
        return beans.sumOf { bean -> bean.screenPhotoBean.filter { it.isSelect }.size }
    }

    fun getSelectData(): List<ScreenPhotoBean> {
        return beans.flatMap { bean ->
            bean.screenPhotoBean.filter { it.isSelect }
        }
    }
    fun getAllData(): List<ScreenPhotoBean> {
        return beans.flatMap { bean ->
            bean.screenPhotoBean
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun removeData(files: List<ScreenPhotoBean>) {
        beans.forEach { screenshotBean ->
            screenshotBean.screenPhotoBean.removeAll(files.toSet())
        }
        beans.removeIf { it.screenPhotoBean.isEmpty() }
        notifyDataSetChanged()
    }

}