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.Logic
import com.zxhyis.toolss.logic.VideoBean
import com.zxhyis.toolss.logic.setOnClick

@SuppressLint("NotifyDataSetChanged")
class VideoAdapter(
    private val dataList: ArrayList<VideoBean>,
    private val context: Context,
    private val logic: Logic
) : RecyclerView.Adapter<VideoAdapter.VideoViewHolder>() {


    inner class VideoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val firstImage: RoundImageView = itemView.findViewById(R.id.first_image)
        val ivSelect : AppCompatImageView = itemView.findViewById(R.id.iv_select)
    }

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

    override fun getItemCount(): Int = dataList.size

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: VideoViewHolder, position: Int) {
        val type = dataList[position]
        Glide.with(holder.itemView.context)
            .load(type.videoUri)
            .into(holder.firstImage)
        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
            logic.doLogic()
            notifyItemChanged(position)
        }

    }

    //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()
    }


}