package com.test.mydemo3.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.test.mydemo3.R
import com.test.mydemo3.databinding.CleanListItemLayoutBinding
import com.test.mydemo3.databinding.FilesItemLayoutBinding
import com.test.mydemo3.model.CleanBean
import com.test.mydemo3.model.FileBean

class CleanAdapter2 : RecyclerView.Adapter<CleanAdapter2.AAA>() {

    private val list = ArrayList<CleanBean>()

    fun Int.inflate(parent: ViewGroup, attachToRoot: Boolean = false): View {
        return LayoutInflater.from(parent.context).inflate(this, parent, attachToRoot)
    }

    class AAA(view: View) : RecyclerView.ViewHolder(view) {
        val binding = CleanListItemLayoutBinding.bind(view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AAA {
        return AAA(R.layout.clean_list_item_layout.inflate(parent))
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: AAA, position: Int) {
        val data = list[position]

        holder.binding.cleanItemName.text = data.typeName

        ///展开逻辑
        if (data.isListViewShow) {
            holder.binding.cleanItemList.visibility = View.VISIBLE
            val subAdapter = SubAdapter()
            holder.binding.cleanItemList.adapter = subAdapter
            subAdapter.setData(data.fileBeans)
        } else {
            holder.binding.cleanItemList.visibility = View.GONE
        }
        holder.binding.cleanItemUnfold.setOnClickListener {
            data.isListViewShow = !data.isListViewShow
            notifyItemChanged(position)
        }
        //选中的逻辑，先外层通过data的属性值是否选中，点击事假更新子类表选中状态下
    }

    fun setData(datas: List<CleanBean>) {
        list.clear()
        list.addAll(datas)
        notifyDataSetChanged()
    }

    inner class SubAdapter : RecyclerView.Adapter<SubAdapter.BBB>() {

        private val sibList = ArrayList<FileBean>()

        inner class BBB(view: View) : RecyclerView.ViewHolder(view) {
            val binding = FilesItemLayoutBinding.bind(view)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BBB {
            return BBB(R.layout.files_item_layout.inflate(parent))
        }

        override fun getItemCount(): Int {
            return sibList.size
        }

        override fun onBindViewHolder(holder: BBB, position: Int) {
            val data = sibList[position]
            holder.binding.fileItemName.text = data.fileName
            //todo data通过状态值显示选中状态
//            holder.binding.fileJoinToImg.isSelected=data.
        }

        fun setData(datas: List<FileBean>) {
            sibList.clear()
            sibList.addAll(datas)
            notifyDataSetChanged()
        }
    }
}