package com.test.basd.smartjunkcleaner.adapter

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.test.basd.smartjunkcleaner.R
import com.test.basd.smartjunkcleaner.bean.PermissionBean
import com.test.basd.smartjunkcleaner.databinding.ItemPermissionBinding
import com.test.basd.smartjunkcleaner.view.XmlEx.inflate

class PermissionAdapter : RecyclerView.Adapter<PermissionAdapter.CCCC>() {

    private val permissionBeans = arrayListOf<PermissionBean>()

    inner class CCCC(view: View) : ViewHolder(view) {
        val binding = ItemPermissionBinding.bind(view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CCCC {
        return CCCC(R.layout.item_permission.inflate(parent))
    }

    override fun getItemCount(): Int {
        return permissionBeans.size
    }

    override fun onBindViewHolder(holder: CCCC, position: Int) {
        val context = holder.itemView.context
        val data = permissionBeans[position]

        holder.binding.tvTitle.text = context.getString(data.permissionTittle)
        holder.binding.tvDesc.text = context.getString(data.permissionDesc)

        if (data.isAuthorized) {
            holder.binding.tvAuthorized.visibility = View.VISIBLE
            if (data.isSensitive) {
                holder.binding.tvAuthorized.background = ContextCompat.getDrawable(context, R.drawable.bg_corners_granted)
                holder.binding.tvAuthorized.setTextColor(ContextCompat.getColor(context, R.color.color_fcad45))
            } else {
                holder.binding.tvAuthorized.background = ContextCompat.getDrawable(context, R.drawable.bg_corners_d9e2fd)
                holder.binding.tvAuthorized.setTextColor(ContextCompat.getColor(context, R.color.color_4772ff))
            }
        } else {
            holder.binding.tvAuthorized.visibility = View.GONE
        }

    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(permissions: List<PermissionBean>) {
        permissionBeans.clear()
        permissionBeans.addAll(permissions)
        notifyDataSetChanged()
    }
}