package com.zxhyis.toolss.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.text.format.Formatter
import android.view.LayoutInflater
import android.view.ViewGroup
import com.zxhyis.toolss.R
import com.zxhyis.toolss.databinding.JunkChildBinding
import com.zxhyis.toolss.databinding.JunkParentBinding
import com.zxhyis.toolss.logic.Logic
import com.zxhyis.toolss.logic.ParentBean
import com.zxhyis.toolss.logic.setOnClick
import pokercc.android.expandablerecyclerview.ExpandableAdapter

class CleanAdapter(
    private val parentList: List<ParentBean>,
    private val context: Context,
    private val logic: Logic
): ExpandableAdapter<ExpandableAdapter.ViewHolder>() {

    internal class ParentViewHolder(val binding: JunkParentBinding) :
        ViewHolder(binding.root)

    internal class ChildViewHolder(val binding: JunkChildBinding) :
        ViewHolder(binding.root)

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

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

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

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

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

    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onBindGroupViewHolder(
        holder: ViewHolder,
        groupPosition: Int,
        expand: Boolean,
        payloads: List<Any>
    ) {

        val viewHolder = holder as? ParentViewHolder ?: return
        val parentType=parentList[groupPosition]
        viewHolder.binding.parentName.text=parentType.title
        parentType.expanded=expand
        viewHolder.binding.groupSize.text=Formatter.formatFileSize(context,parentType.parentSize)

        if (groupPosition!=2){
            if (parentType.expanded){
                viewHolder.binding.ivExpanded.setImageResource(R.drawable.xiala_n)
            }else{
                viewHolder.binding.ivExpanded.setImageResource(R.drawable.xiala_s)
            }
            viewHolder.binding.icSelect.isSelected=parentType.isParentSelected

            viewHolder.binding.icSelect.setOnClick(javaClass.simpleName) {
                viewHolder.binding.icSelect.isSelected =
                    !viewHolder.binding.icSelect.isSelected
                parentType.isParentSelected=!parentType.isParentSelected
                if (parentType.isParentSelected){
                    for (app in parentType.childItem){
                        app.isChildSelected=true
                        notifyDataSetChanged()
                        logic.doLogic()
                    }
                }else{
                    for (app in parentType.childItem){
                        app.isChildSelected=false
                        logic.doLogic()
                        notifyDataSetChanged()
                    }
                }
            }

        }


    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun onBindChildViewHolder(
        holder: ViewHolder,
        groupPosition: Int,
        childPosition: Int,
        payloads: List<Any>
    ) {
        val viewHolder = holder as? ChildViewHolder ?: return
        val childType= parentList[groupPosition].childItem[childPosition]
//        viewHolder.binding.childName.text = childType.type
//        viewHolder.binding.childIcon.setImageResource(childType.image)
        viewHolder.binding.childSelect.isSelected = childType.isChildSelected
        viewHolder.binding.childSize.text = " -- ${Formatter.formatFileSize(context, childType.childSize)}"
        viewHolder.binding.childSelect.setOnClickListener {
            viewHolder.binding.childSelect.isSelected =
                !viewHolder.binding.childSelect.isSelected
            childType.isChildSelected = !childType.isChildSelected
            if (!childType.isChildSelected){
                parentList[groupPosition].isParentSelected=false
                notifyDataSetChanged()
            }
            logic.doLogic()
        }
        viewHolder.itemView.setOnClickListener {
            when (childPosition) {
                childPosition -> {
                    viewHolder.binding.childSelect.isSelected =
                        !viewHolder.binding.childSelect.isSelected
                    childType.isChildSelected = !childType.isChildSelected
                    if (!childType.isChildSelected){
                        parentList[groupPosition].isParentSelected=false
                        notifyDataSetChanged()
                    }
                    logic.doLogic()
                }
            }
        }


    }


}