package com.zxhyis.toolss.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.zxhyis.toolss.R
import com.zxhyis.toolss.logic.AppBean
import com.zxhyis.toolss.logic.Logic
import com.zxhyis.toolss.logic.setOnClick

class AppAdapter(
    private val dataList: ArrayList<AppBean>,
    private val logic: Logic
) : RecyclerView.Adapter<AppAdapter.AppVH>() {

    inner class AppVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val appIcon: AppCompatImageView = itemView.findViewById(R.id.app_icon)
        val appName: AppCompatTextView = itemView.findViewById(R.id.app_name)
        val appStatus: AppCompatImageView = itemView.findViewById(R.id.app_status)
        val cl: ConstraintLayout = itemView.findViewById(R.id.cl_item_app)
    }

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

    override fun getItemCount(): Int = dataList.size

    override fun onBindViewHolder(holder: AppVH, position: Int) {
        val type = dataList[position]
        holder.appIcon.setImageDrawable(type.icIcon)
        holder.appName.text = type.appName
        holder.appStatus.isSelected = type.isSelected

        holder.cl.setOnClick(javaClass.simpleName) {
            type.isSelected = !type.isSelected
            notifyItemChanged(position)
            logic.doLogic()
        }

    }


}