package com.xm.test.myfilemaster.adapter

import android.util.SparseBooleanArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
import androidx.recyclerview.widget.RecyclerView
import com.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.activity.CleanActivity
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 mFileCheckedItemList = ArrayList<FileBean>()

    private var sparseBooleanArray = 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]
        sparseBooleanArray.add(SparseBooleanArray())
        itemAdapters.add(SubAdapter(position))
        mSwitchAll.add(false)

        holder.binding.cleanItemName.text = data.typeName

        holder.binding.cleanItemCheckBox.isChecked = mSwitchAll[position]


        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.setOnClickListener {
            if (CleanAdapter.isCleanOver){
                mSwitchAll[position] = !mSwitchAll[position]
                list[position].fileBeans.forEach {
                    it.isChecked = holder.binding.cleanItemCheckBox.isChecked
                }
                if (holder.binding.cleanItemCheckBox.isChecked){
                    mFileCheckedItemList.addAll(list[position].fileBeans)
                }else{
                    mFileCheckedItemList.removeAll(list[position].fileBeans)
                }
                itemAdapters[position].setData(list[position].fileBeans)
                notifyItemChanged(position)
                changeSwitchSize()
            }
        }
        //选中的逻辑，先外层通过data的属性值是否选中，点击事假更新子类表选中状态下
    }

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

    }

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

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

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

        init {
            this.itemPosition = itemPosition
        }

        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 = sibList[position].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)
    }
}