package com.ux.video.file.filerecovery.photo

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.ux.video.file.filerecovery.base.BaseAdapter
import com.ux.video.file.filerecovery.databinding.PhotoDisplayDateAdapterBinding
import com.ux.video.file.filerecovery.utils.Common
import com.ux.video.file.filerecovery.utils.GridSpacingItemDecoration
import com.ux.video.file.filerecovery.utils.ScanRepository

class PhotoDisplayDateAdapter(
    mContext: Context,
    var mColumns: Int,
    var viewModel: ScanRepository,
    var onSelectedUpdate: (updatePath: String, isAdd: Boolean) -> Unit,
    var clickItem:(item:ResultPhotosFiles)-> Unit
) :
    BaseAdapter<Pair<String, List<ResultPhotosFiles>>, PhotoDisplayDateAdapterBinding>(mContext) {

    private var allSelected: Boolean? = null
    override fun getViewBinding(parent: ViewGroup): PhotoDisplayDateAdapterBinding =
        PhotoDisplayDateAdapterBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )

    /**
     * 返回所有嵌套的数据量总数
     */
    fun getTotalChildCount(): Int {
        return data.sumOf { it.second.size }
    }

    /**
     * activity页面上点击全选按钮执行
     */
    fun setAllSelected(allSelect: Boolean) {
        allSelected = allSelect
        data.forEach {
            it.second.forEach { item ->
                onSelectedUpdate(item.path.toString(),allSelect)
            }
        }
        notifyDataSetChanged()
        allSelected = null
    }

    fun  resetAllValue(b: Boolean?){
        allSelected = b
    }

    fun setColumns(int: Int) {
        mColumns = int
        notifyDataSetChanged()
    }




    @SuppressLint("SetTextI18n")
    override fun bindItem(
        holder: VHolder<PhotoDisplayDateAdapterBinding>,
        item: Pair<String, List<ResultPhotosFiles>>
    ) {
        holder.vb.run {

            item.run {
                val (date, files) = item
                val childAdapter = PhotoDisplayDateChildAdapter(
                    mContext,
                    mColumns,
                    viewModel,
                 { path, addOrRemove, isDateAllSelected ->
                    //点击当前Adapter某一天的全选或者子Item上的选中都会回调到这里
                    tvDayAllSelect.isSelected = isDateAllSelected
                    onSelectedUpdate(path.toString(),addOrRemove)
                },clickItem).apply { setData(files) }

                allSelected?.let {
                    childAdapter.setAllSelected(it)
                }
                tvDayAllSelect.setOnClickListener {
                    it.isSelected = !it.isSelected
                    childAdapter.setAllSelected(it.isSelected)
                }

                textDate.text = date
                textChildCounts.text = "(${files.size})"
                recyclerChild.apply {
                    layoutManager = GridLayoutManager(context, mColumns)
                    val gridSpacingItemDecoration =
                        GridSpacingItemDecoration(mColumns, Common.itemSpacing, Common.horizontalSpacing)
                    Common.showLog("---------mColumns=${mColumns}")
//                    resetItemDecorationOnce(gridSpacingItemDecoration)
                    adapter = childAdapter
                    isNestedScrollingEnabled = false
                }

            }
        }
    }


}