package com.ux.video.file.filerecovery.sort

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.ux.video.file.filerecovery.base.BaseAdapter
import com.ux.video.file.filerecovery.databinding.PhotoDisplayDateAdapterBinding
import com.ux.video.file.filerecovery.db.ResultDataFiles
import com.ux.video.file.filerecovery.db.isThumbnail

import com.ux.video.file.filerecovery.utils.FileType

/**
 * 排序页面显示
 */
class SortDisplayAdapter(
    mContext: Context,
    var fileType: FileType,
    var mColumns: Int,
    var viewModel: SortingViewModel,
    var onSelectedUpdate: (resultDataFiles: ResultDataFiles, isAdd: Boolean) -> Unit,
    var clickItem: (item: ResultDataFiles) -> Unit
) :
    BaseAdapter<Pair<String, List<ResultDataFiles>>, PhotoDisplayDateAdapterBinding>(mContext) {
    private var allSelected: Boolean? = null

    override fun getViewBinding(parent: ViewGroup): PhotoDisplayDateAdapterBinding =
        PhotoDisplayDateAdapterBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )


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

    }

    /**
     * activity页面上点击全选按钮执行
     */
    fun setAllSelected(allSelect: Boolean) {
        allSelected = allSelect
        data.forEach {
            it.second.forEach { item ->
                onSelectedUpdate(item, 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<ResultDataFiles>>
    ) {
        holder.vb.run {
            item.run {
                val (date, files) = item
                val childAdapter = SortDisplayChildAdapter(
                    mContext,
                    fileType,
                    mColumns,
                    viewModel,
                    { resultPhotosFiles, addOrRemove, isDateAllSelected ->
                        //点击当前Adapter某一天的全选或者子Item上的选中都会回调到这里
                        tvDayAllSelect.isSelected = isDateAllSelected
                        onSelectedUpdate(resultPhotosFiles, addOrRemove)
                    }, { updateHideThumbnails ->

                        tvDayAllSelect.isSelected = updateHideThumbnails

                    }, 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 = when (fileType) {
                        FileType.AUDIO, FileType.DOCUMENT -> {
                            LinearLayoutManager(context)
                        }

                        else -> {
                            GridLayoutManager(context, mColumns)
                        }
                    }

                    adapter = childAdapter
                    isNestedScrollingEnabled = false
                }

            }
        }
    }

    object ItemDiffCallback : DiffUtil.ItemCallback<Pair<String, List<ResultDataFiles>>>() {
        override fun areItemsTheSame(
            oldItem: Pair<String, List<ResultDataFiles>>,
            newItem: Pair<String, List<ResultDataFiles>>
        ): Boolean {
            return oldItem.first == newItem.first
        }

        override fun areContentsTheSame(
            oldItem: Pair<String, List<ResultDataFiles>>,
            newItem: Pair<String, List<ResultDataFiles>>
        ): Boolean {
            return oldItem == newItem
        }
    }


}