package com.test.basd.fastcleanerjunk.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.bumptech.glide.Glide
import com.test.basd.fastcleanerjunk.R
import com.test.basd.fastcleanerjunk.bean.ScreenPhotoBean
import com.test.basd.fastcleanerjunk.bean.ScreenshotBean
import com.test.basd.fastcleanerjunk.databinding.ItemScreenPhotoBinding
import com.test.basd.fastcleanerjunk.databinding.ItemScreenshotBinding
import com.test.basd.fastcleanerjunk.view.XmlEx.inflate
import java.io.File
import java.text.SimpleDateFormat
import java.util.ArrayList
import java.util.Locale

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

    private val 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 photoBinding = ItemScreenPhotoBinding.bind(R.layout.item_screen_photo.inflate(binding.group))
            Glide.with(context).load(screenPhotoBean.file).centerCrop().into(photoBinding.iv)
            photoBinding.ivSelector.isSelected = screenPhotoBean.isSelect
            photoBinding.ivSelector.setOnClickListener {
                screenPhotoBean.isSelect = !screenPhotoBean.isSelect
                photoBinding.ivSelector.isSelected = screenPhotoBean.isSelect
                bean.isSelect = bean.screenPhotoBean.all { it.isSelect }
                notifyDataSetChanged()
                select.invoke(getSelectDataSize())
            }
            binding.group.addView(photoBinding.root)
        }

    }

    @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 getSelectDataSize(): Int {
        return beans.sumOf { bean -> bean.screenPhotoBean.filter { it.isSelect }.size }
    }

    fun getSelectData(): List<File> {
        return beans.flatMap { bean -> bean.screenPhotoBean.filter { it.isSelect }.map { s -> s.file } }
    }

    fun removeData(files: List<File>) {
        beans.forEach {

        }
    }
}