package com.xm.test.myfilemaster.adapter

import android.util.Log
import android.util.SparseBooleanArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.databinding.CleanListItemLayoutBinding
import com.xm.test.myfilemaster.databinding.FilesItemLayoutBinding
import com.xm.test.myfilemaster.model.CleanBean
import com.xm.test.myfilemaster.model.FileBean

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

    private val list = ArrayList<CleanBean>()

    var mFileCheckedItemLists = ArrayList<ArrayList<FileBean>>()
    private var mCheckedBooleanArrays = ArrayList<SparseBooleanArray>()
    private var itemAdapters = ArrayList<SubAdapter>()
    var mAllSizes = ArrayList<Long>()
    var mSwitchAll = ArrayList<Boolean>()
    var mIClean : IClean

    init {
        this.mIClean = mIClean
    }

    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]
        itemAdapters.add(SubAdapter(position, mCheckedBooleanArrays[position]))
        mSwitchAll.add(true)

        holder.binding.cleanItemName.text = data.typeName

        holder.binding.cleanItemList.visibility = if (!CleanAdapter.isCleanOver) View.GONE else View.VISIBLE
        holder.binding.cleanItemUnfold.visibility = if (!CleanAdapter.isCleanOver) View.GONE else View.VISIBLE
        holder.binding.fileItemSize.visibility = if (!CleanAdapter.isCleanOver) View.GONE else View.VISIBLE
        holder.binding.cleanItemCheckBox.visibility = if (!CleanAdapter.isCleanOver) View.GONE else View.VISIBLE
        holder.binding.cleanItemMore.visibility = if (CleanAdapter.isCleanOver) View.GONE else View.VISIBLE

        var allSize: Long = 0
        for (fileBean in list[position].fileBeans){
            allSize += fileBean.fileSize
        }
        holder.binding.fileItemSize.text =  if (allSize > 1024 * 1024 * 1024){
            "${allSize / 1024 / 1024 / 1024} GB"
        }else if (allSize > 1024 * 1024){
            "${allSize / 1024 / 1024} MB"
        }else if (allSize > 1024){
            "${allSize / 1024} KB"
        }else{
            "${allSize} B"
        }
        mAllSizes.add(allSize)

        ///展开逻辑
        if (data.isListViewShow) {
            holder.binding.cleanItemList.visibility = View.VISIBLE
            holder.binding.cleanItemList.adapter = itemAdapters[position]
            itemAdapters[position].setData(data.fileBeans)
        } else {
            holder.binding.cleanItemList.visibility = View.GONE
        }
        holder.binding.cleanItemBar.setOnClickListener {
            data.isListViewShow = !data.isListViewShow
            notifyItemChanged(position)
        }
        holder.binding.cleanItemCheckBox.isChecked = mSwitchAll[position]
        selectAll(mSwitchAll[position],position)
        holder.binding.cleanItemCheckBox.setOnClickListener {
            if (CleanAdapter.isCleanOver){
                mSwitchAll[position] = !mSwitchAll[position]
                selectAll(mSwitchAll[position], position)
                notifyItemChanged(position)
            }
        }
        //选中的逻辑，先外层通过data的属性值是否选中，点击事假更新子类表选中状态下
    }

    fun selectAll(isChecked: Boolean, position: Int){
        mFileCheckedItemLists[position].clear()
        if (isChecked) mFileCheckedItemLists[position].addAll(list[position].fileBeans)
        for (i in list[position].fileBeans.indices){
            mCheckedBooleanArrays[position].put(i, isChecked)
        }
        itemAdapters[position].notifyDataSetChanged()
        changeSwitchSize()
    }

    private fun changeSwitchSize() {
        var size: Long = 0
        mFileCheckedItemLists.forEach {
            it.forEach {
                size += it.fileSize
            }
        }
        mIClean.OnChekedItemListener(size)

    }

    fun setData(datas: List<CleanBean>) {
        list.clear()
        list.addAll(datas)
        mCheckedBooleanArrays.clear()
        mFileCheckedItemLists.clear()
        for (i in datas.indices) {
            mCheckedBooleanArrays.add(SparseBooleanArray())
            mFileCheckedItemLists.add(ArrayList())
            for (j in 0..<datas[i].fileBeans.size){
                mCheckedBooleanArrays[i].put(j, false)
            }
            notifyItemChanged(i)
        }
    }

    inner class SubAdapter(itemPosition: Int, sparseBooleanArray: SparseBooleanArray) : RecyclerView.Adapter<SubAdapter.BBB>() {

        private val sibList = ArrayList<FileBean>()
        private var itemPosition: Int
        private var sparseBooleanArray: SparseBooleanArray

        init {
            this.itemPosition = itemPosition
            this.sparseBooleanArray = sparseBooleanArray
        }

        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.

            holder.binding.fileJoinToImg.visibility = View.GONE
            holder.binding.fileSelectSwitch.visibility = if (!CleanAdapter.isCleanOver) View.GONE else View.VISIBLE

            holder.binding.fileSelectSwitch.isChecked = mCheckedBooleanArrays[itemPosition][position]

            holder.binding.itemLayout.setOnClickListener {
                Log.e("yanglin", "onBindViewHolder: $itemPosition $position", )
                mCheckedBooleanArrays[itemPosition].put(position, !mCheckedBooleanArrays[itemPosition][position])
                if (mCheckedBooleanArrays[itemPosition][position]){
                    mFileCheckedItemLists[itemPosition].add(sibList[position])
                }else{
                    mFileCheckedItemLists[itemPosition].remove(sibList[position])
                }
                mSwitchAll[itemPosition] = false
                changeSwitchSize()
                notifyItemChanged(position)
//                holder.binding.fileSelectSwitch.isChecked = !holder.binding.fileSelectSwitch.isChecked
            }

//            holder.binding.fileSelectSwitch.setOnClickListener {
//                sibList[position].isChecked = !sibList[position].isChecked
//                if (holder.binding.fileSelectSwitch.isChecked){
//                    mFileCheckedItemList.add(sibList[position])
//                }else{
//                    mFileCheckedItemList.remove(sibList[position])
//                    mSwitchAll[itemPosition] = false
//                }
//                changeSwitchSize()
//                notifyItemChanged(position)
//            }

            when(itemPosition){
                0 ->{
                    holder.binding.fileItemName.text = sibList[position].fileName
                    holder.binding.fileItemNone.text = sibList[position].filePath
                }
                1 ->{
                    holder.binding.filesItemIcon.setImageDrawable(sibList[position].drawable)
                    holder.binding.fileItemName.text = sibList[position].fileName
                    holder.binding.fileItemNone.text = sibList[position].filePath

                }
                3 ->{
//                    itemViewHolder.fileIcon.setImageDrawable(dataList[position].drawable)
                    holder.binding.filesItemIcon.setImageResource(R.drawable.ic_log_file)
                    holder.binding.fileItemName.text = sibList[position].fileName
                    holder.binding.fileItemNone.text = sibList[position].hint
                }
                4 ->{
                    holder.binding.filesItemIcon.setImageResource(R.drawable.ic_log_file)
                    holder.binding.fileItemName.text = sibList[position].fileName
                    holder.binding.fileItemNone.text = sibList[position].filePath
                }
            }
        }

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

    interface IClean{
        fun OnChekedItemListener(size: Long)
    }
}