package com.zxhyis.tools.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.tools.R
import com.zxhyis.tools.logic.ImageInfoBean
import com.zxhyis.tools.logic.Logic
import com.zxhyis.tools.logic.setOnClick
import com.zxhyis.tools.logic.startActivity
import com.zxhyis.tools.ui.`fun`.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]
        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 {
            dataList[position].isSelected = !dataList[position].isSelected
            logic.doLogic()
            notifyItemChanged(position)
        }

        holder.itemView.setOnClick {
            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()
    }


}