package com.base.superfilemanager.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.base.superfilemanager.R
import com.base.superfilemanager.bean.PermissionBean
import com.base.superfilemanager.databinding.ItemPermissionBinding
import com.base.superfilemanager.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.theme_color))
            } 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()
    }
}