package com.test.mydemo3.adapter

import android.content.Context
import android.util.Log
import android.util.SparseBooleanArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.GridView
import androidx.recyclerview.widget.RecyclerView
import com.cloud.cleanjunksdk.similar.PicInfo
import com.cloud.cleanjunksdk.similar.PicSimilarInfo
import com.test.mydemo3.R
import com.test.mydemo3.activity.SimilarImagesActivity
import com.test.mydemo3.databinding.ImageItemLayoutBinding
import com.test.mydemo3.util.FileUtil

class SimilarBeanAdapter(context: Context) : RecyclerView.Adapter<SimilarBeanAdapter.AA>() {
    private val list = arrayListOf<   PicSimilarInfo>()
    private var context: Context
    var mCheckedLists: MutableList<PicInfo> = mutableListOf<PicInfo>()
    private var mItemCheckedArray =  ArrayList<SparseBooleanArray>()
    val mGridAdapters = ArrayList<SimilarGridAdapter>()
    val holders = ArrayList<AA>()
    init {
        this.context = context
    }

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


    class AA(view: View) : RecyclerView.ViewHolder(view) {
        val binding = ImageItemLayoutBinding.bind(view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AA {
        val root = R.layout.image_item_layout.inflate(parent)
        return AA(root)
    }

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

    override fun onBindViewHolder(holder: AA, position: Int) {
        holders.add(holder)
        if (mItemCheckedArray.size != list.size){
            mItemCheckedArray.add(SparseBooleanArray())
//            itemAdapter.add(ItemAdapter(mDataList[position], mContext, mItemCheckedArray[position], position))
        }
        mGridAdapters.add(SimilarGridAdapter(list[position].getmList(), context, mItemCheckedArray[position], position))

//        val data = list[position]
        holder.binding.similarImageNumText.text =
            String.format(context.getString(R.string.similar, list[position].getmList().size.toString()))
        val layoutParams = holder.binding.imageItemGrid.layoutParams

        if (list[position].getmList().size.toFloat() / 4.0 == 1.0){
            layoutParams.height = FileUtil.dpToPx(83*(list[position].getmList().size / 4), context)
        }else{
            layoutParams.height = FileUtil.dpToPx(83*(list[position].getmList().size / 4 + 1 ), context)
        }

        holder.binding.imageItemGrid.setOnItemClickListener { parent, view, itemPosition, id ->
            if (SimilarImagesActivity.isClickEditBtn){
                println("yanglin positioin $position  itemPostion $itemPosition")
                updateCheckBoxStatus(position, view, itemPosition, holder.binding.imageItemGrid, list[position].getmList())
            }
        }



        holder.binding.imageItemGrid.adapter = mGridAdapters[position]
    }

    fun updateCheckBoxStatus(position: Int, view: View, itemPosition: Int, lv: GridView, fileBeanList: MutableList<PicInfo>){
        val viewHolder = view.tag as ImageItemAdapter.ItemAdapter.ViewHolder
        viewHolder.checkBox.toggle()
        lv.setItemChecked(itemPosition, viewHolder.checkBox.isChecked)
        mItemCheckedArray[position].put(itemPosition, viewHolder.checkBox.isChecked)
        if (viewHolder.checkBox.isChecked){
            mCheckedLists.add(fileBeanList[itemPosition])
        }else{
            mCheckedLists.remove(fileBeanList[itemPosition])
        }
        mGridAdapters[position].notifyDataSetChanged()
    }

    fun setData(data: List<PicSimilarInfo>) {
        list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }

    fun selectAll(isChecked: Boolean){
        Log.e("yanglin", "selectAll: $isChecked", )
        mCheckedLists.clear()
        if (!isChecked){
            for (i in 0..< list.size){
                mItemCheckedArray[i].clear()
            }
        }else{
            for (i in 0..< list.size){
                mCheckedLists.clear()

                setStateCheckedMap(i, isChecked)
                mCheckedLists.addAll(list[i].getmList())
                mGridAdapters[i].notifyDataSetChanged()
            }
        }
    }
    fun setStateCheckedMap(position: Int, isChecked: Boolean){
        for(i in list[position].getmList().indices){
            mItemCheckedArray[position].put(i, isChecked)
            holders[position].binding.imageItemGrid.setItemChecked(i, isChecked)
        }
    }

}