package com.pan_american.android.data.model.common.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.pan_american.android.R
import com.pan_american.android.data.model.common.entity.ServiceResourceListItem

class ServiceResourceAdapter(private val listItem: List<ServiceResourceListItem>): RecyclerView.Adapter<ServiceResourceAdapter.ViewHolder>() {

    lateinit var onRecyclerViewItemClick: OnRecyclerViewItemClick<ServiceResourceListItem>

    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val city: TextView = view.findViewById(R.id.service_resource_city)
        val deleteIcon:ImageView = view.findViewById(R.id.delete)
        val name: TextView = view.findViewById(R.id.supplier_name)
        val scope: TextView = view.findViewById(R.id.supplier_scope)
        val phone: TextView = view.findViewById(R.id.supplier_phone)
        val email: TextView = view.findViewById(R.id.supplier_email)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_service_resource_list, parent, false)
        val viewHolder = ViewHolder(view)
        val position = viewHolder.absoluteAdapterPosition

        viewHolder.itemView.setOnClickListener {
            onRecyclerViewItemClick.onItemClick(position)
        }
        viewHolder.deleteIcon.setOnClickListener {
            onRecyclerViewItemClick.onItemDelete(position)
        }
        return viewHolder
    }

    override fun getItemCount() = listItem.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = listItem[position]
        holder.city.text = item.city
        holder.deleteIcon.visibility = View.VISIBLE
        holder.name.text = item.name
        holder.scope.text = item.scope
        holder.phone.text = item.phone
        holder.email.text = item.email
    }

    interface OnRecyclerViewItemClick<T> {
        fun onItemClick(position: Int)
        fun onItemDelete(position: Int)
    }
}