package com.b2lmobitech.fieldcloudhrms

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.b2lmobitech.fieldcloudhrms.databinding.ItemAttachmentBinding
import android.content.Intent
import android.net.Uri

class TimesheetAttachmentsAdapter(
    private val list: List<TimesheetAttachment>
) : RecyclerView.Adapter<TimesheetAttachmentsAdapter.AttachmentVH>() {

    inner class AttachmentVH(val binding: ItemAttachmentBinding)
        : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AttachmentVH {
        val binding = ItemAttachmentBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return AttachmentVH(binding)
    }

    override fun onBindViewHolder(holder: AttachmentVH, position: Int) {

        val item = list[position]

        holder.binding.txtFileName.text = if (item.original_file_name != null) item.original_file_name else item.doc_path.substringAfterLast("/")

        holder.itemView.setOnClickListener {
            // OPEN FILE USING BROWSER
            val url = "https://processflow.in/HRMS/" + item.doc_path
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
            holder.itemView.context.startActivity(intent)
        }
    }

    override fun getItemCount() = list.size
}
