package com.base.scanqrclear.luma

import android.animation.ObjectAnimator
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.base.scanqrclear.R
import com.base.scanqrclear.databinding.ItemChildCleanJunkBinding
import com.base.scanqrclear.databinding.ItemGroupCleanJunkBinding
import pokercc.android.expandablerecyclerview.ExpandableAdapter

class CleanJunkAdapter(
    var group: List<FileBean>,
    var child: List<List<FileBean>>
) : ExpandableAdapter<ExpandableAdapter.ViewHolder>() {
    private class ChildViewHolder(val binding: ItemChildCleanJunkBinding) :
        ExpandableAdapter.ViewHolder(binding.root)

    private class GroupViewHolder(val binding: ItemGroupCleanJunkBinding) :
        ExpandableAdapter.ViewHolder(binding.root)
    constructor() : this(emptyList(), emptyList())

    private val _selectionData = MutableLiveData<List<FileBean>>()
    val selectionData: LiveData<List<FileBean>> = _selectionData

    private var isInit = true

    fun setData(group: List<FileBean>, child: List<List<FileBean>>) {
        this.group = group
        this.child = child
        notifyDataSetChanged()
    }

    override fun getChildCount(groupPosition: Int): Int {
        return child[groupPosition].size
    }

    override fun getGroupCount(): Int {
        return group.size
    }

    override fun onCreateChildViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(viewGroup.context)
        val view = ItemChildCleanJunkBinding.inflate(inflater, viewGroup, false)
        return ChildViewHolder(view)
    }

    override fun onCreateGroupViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(viewGroup.context)
        val view = ItemGroupCleanJunkBinding.inflate(inflater, viewGroup, false)
        return GroupViewHolder(view)
    }

    override fun onGroupViewHolderExpandChange(
        holder: ViewHolder,
        groupPosition: Int,
        animDuration: Long,
        expand: Boolean
    ) {
    }

    override fun onBindGroupViewHolder(
        holder: ViewHolder,
        groupPosition: Int,
        expand: Boolean,
        payloads: List<Any>
    ) {
        holder as GroupViewHolder
        val data = group[groupPosition]
        holder.binding.tvName.text = data.name
        holder.binding.tvLength.text = Utils.getSize(data.length)
        holder.binding.ivIcon.setImageResource(data.icon)
        holder.binding.ivIndicator.setImageResource(if (expand) R.mipmap.icon_up_setting else R.mipmap.icon_down_setting)
        holder.binding.ivSelect.setImageResource(if (data.isSelected) R.mipmap.icon_yixuan_s else R.mipmap.icon_yixuan_n)
        holder.binding.clItem.setBackgroundResource(if (expand && child[groupPosition].isNotEmpty()) R.drawable.white_background_top else R.drawable.white_background)
        holder.binding.ivSelect.setOnClickListener {
            data.isSelected = !data.isSelected
            child[groupPosition].forEach { it.isSelected = data.isSelected }
            _selectionData.value = child.flatten()
            notifyGroupChange(groupPosition)
            for (childPosition in 0 until child[groupPosition].size) {
                notifyChildChange(groupPosition, childPosition)
            }
        }

        if (isInit) {
            val translationX =
                holder.itemView.context.resources.displayMetrics.widthPixels.toFloat()
            holder.itemView.translationX = -translationX
            ObjectAnimator.ofFloat(holder.itemView, "translationX", -translationX, 0f).apply {
                duration = 1000
                startDelay = (groupPosition + 1) * 300L
                start()
            }
        }
        if (groupPosition == group.size - 1) isInit = false
    }

    override fun onBindChildViewHolder(
        holder: ViewHolder,
        groupPosition: Int,
        childPosition: Int,
        payloads: List<Any>
    ) {
        holder as ChildViewHolder
        val data = child[groupPosition][childPosition]
        val groupData = group[groupPosition]
        holder.binding.tvName.text = data.name
        holder.binding.tvLength.text = Utils.getSize(data.length)
        holder.binding.ivSelect.setImageResource(if (data.isSelected) R.mipmap.icon_yuan_junk_s else R.mipmap.icon_yuan_n)
        holder.itemView.setBackgroundResource(if (childPosition == child[groupPosition].size - 1) R.drawable.white_background_bottom else R.color.white)
        holder.itemView.setOnClickListener {
            data.isSelected = !data.isSelected
            notifyChildChange(groupPosition, childPosition)
            val isSelected = child[groupPosition].stream().allMatch(FileBean::isSelected)
            if (groupData.isSelected != isSelected) {
                groupData.isSelected = isSelected
                notifyGroupChange(groupPosition)
            }
            _selectionData.value = child.flatten()
        }
    }
}

