package com.zxhyis.toolss.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.cbman.roundimageview.RoundImageView
import com.zxhyis.toolss.R
import com.zxhyis.toolss.logic.ImageInfoBean
import com.zxhyis.toolss.logic.Logic
import com.zxhyis.toolss.logic.setOnClick
import com.zxhyis.toolss.logic.startActivity
import com.zxhyis.toolss.logic.testLog
import com.zxhyis.toolss.ui.page.PictureDetailActivity

@SuppressLint("NotifyDataSetChanged")
class ImageAdapter(
    private val dataList: ArrayList<ImageInfoBean>,
    private val context: Context,
    private val logic: Logic
) : RecyclerView.Adapter<ImageAdapter.ImageViewHolder>() {


    inner class ImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val apkIcon: RoundImageView = itemView.findViewById(R.id.iv_image)
        val ivSelect : AppCompatImageView = itemView.findViewById(R.id.iv_select)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_image_info, parent, false)
        return ImageViewHolder(view)
    }

    override fun getItemCount(): Int = dataList.size

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ImageViewHolder, position: Int) {
        val type = dataList[position]
        if (type.imagePath != ""){
            Glide.with(holder.itemView.context)
                .load(type.imagePath)
                .into(holder.apkIcon)
        }
        holder.ivSelect.isSelected = type.isSelected
        if (type.isShow) {
            holder.ivSelect.visibility = View.VISIBLE
        } else {
            holder.ivSelect.visibility = View.GONE
        }

        holder.ivSelect.setOnClick(javaClass.simpleName) {
            dataList[position].isSelected = !dataList[position].isSelected
            testLog("dataList[position].isSelected:${dataList[position].isSelected}")
            logic.doLogic()
            notifyItemChanged(position)
        }

        holder.itemView.setOnClickListener {
            when(position){
                position ->{
                    if (!type.isShow){
                        context.startActivity<PictureDetailActivity>("image_path" to type.imagePath)
                    }
                }
            }
        }

    }

    //select all
    fun selectAll() {
        dataList.forEach {
            it.isSelected = true
        }
        notifyDataSetChanged()
    }

    fun deselectAll() {
        dataList.forEach {
            it.isSelected = false
        }
        notifyDataSetChanged()
    }

    fun setShow() {
        dataList.forEach {
            it.isShow=true
        }
        notifyDataSetChanged()
    }


}