package com.zxhyis.toolss.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.text.format.Formatter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
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.ImageAndVideoBean
import com.zxhyis.toolss.logic.Logic
import com.zxhyis.toolss.logic.setOnClick

class RecentAdapter(
    private val dataList: ArrayList<ImageAndVideoBean>,
    private val context: Context,
    private val logic: Logic
) : RecyclerView.Adapter<RecentAdapter.RecentVH>() {

    inner class RecentVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val image: RoundImageView = itemView.findViewById(R.id.recent_icon)
        val videoFlag: AppCompatImageView = itemView.findViewById(R.id.video_flag)
        val recentName: AppCompatTextView = itemView.findViewById(R.id.recent_name)
        val recentTime: AppCompatTextView = itemView.findViewById(R.id.recent_time)
        val fileSize: AppCompatTextView = itemView.findViewById(R.id.file_size)
        val fileSelect: AppCompatImageView = itemView.findViewById(R.id.file_select)
    }

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

    override fun getItemCount(): Int = dataList.size

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: RecentVH, position: Int) {
        val oneData = dataList[position]
        Glide.with(holder.itemView.context)
            .load(oneData.path)
            .into(holder.image)
        if (oneData.type == 1) {
            holder.videoFlag.visibility = View.GONE
        } else {
            holder.videoFlag.visibility = View.VISIBLE
        }
        holder.recentName.text = oneData.fileName
        holder.fileSize.text = "  --- ${Formatter.formatFileSize(context, oneData.fileSize)}"
        holder.recentTime.text = oneData.recentTime
        holder.fileSelect.isSelected = oneData.isSelected

        if (oneData.isShow) {
            holder.fileSelect.visibility = View.VISIBLE
        } else {
            holder.fileSelect.visibility = View.GONE
        }

        holder.fileSelect.setOnClick(javaClass.simpleName) {
            dataList[position].isSelected = !dataList[position].isSelected
            logic.doLogic()
            notifyItemChanged(position)
        }


    }

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

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

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


}