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

@SuppressLint("NotifyDataSetChanged")
class LarageFileAdapter(
    private val dataList: ArrayList<AudioBean>,
    private val context: Context,
    private val logic: Logic
) : RecyclerView.Adapter<LarageFileAdapter.ApkViewHolder>() {


    inner class ApkViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val apkIcon: AppCompatImageView = itemView.findViewById(R.id.apk_icon)
        val apkName : AppCompatTextView = itemView.findViewById(R.id.apk_name)
        val apkTime : AppCompatTextView = itemView.findViewById(R.id.apk_time)
        val apkSize : AppCompatTextView = itemView.findViewById(R.id.apk_size)
        val apkSelect : AppCompatImageView = itemView.findViewById(R.id.apk_select)
    }

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

    override fun getItemCount(): Int = dataList.size

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ApkViewHolder, position: Int) {
        val type = dataList[position]
        holder.apkIcon.setImageResource(R.drawable.home_largefiles)
        holder.apkName.text = type.audioName
        holder.apkSize.text = "  --- ${Formatter.formatFileSize(context, type.apkSize)}"
        holder.apkTime.text = type.apkTime
        holder.apkSelect.isSelected = type.isSelected

        if (type.isShow) {
            holder.apkSelect.visibility = View.VISIBLE
        } else {
            holder.apkSelect.visibility = View.GONE
        }

        holder.apkSelect.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()
    }


}