package com.android.kepr.ui.assets.adapter
import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.kepr.MyApplication.Companion.context
import com.android.kepr.data.domain.apiresponse.AssetDataObject
import com.android.kepr.databinding.ItemAssetsListingBinding
import com.android.kepr.utils.CommonStrings.DateFormat.MY_CUSTOM_FORMAT
import com.android.kepr.utils.Utils
import com.android.kepr.utils.Utils.Companion.underline
class
SubListingAdapter(
private var mContext: Context,
val mItemClickListener: ItemClickListener,
private var assetsArrayList: ArrayList<AssetDataObject>
) :
RecyclerView.Adapter<SubListingAdapter.MyViewHolder>() {
private var selectedPosition = -1
lateinit var layoutManager: LinearLayoutManager
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ItemAssetsListingBinding.inflate(inflater, parent, false)
return MyViewHolder(binding)
}
interface ItemClickListener {
fun onViewDetailsClick(position: Int, assetName: String)
fun onCheckBoxClick(position: Int, updatedBuildingId: String?, toString: String)
}
override fun onBindViewHolder(
holder: MyViewHolder,
@SuppressLint("RecyclerView") position: Int
) {
holder.bind(assetsArrayList[position])
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItemViewType(position: Int): Int {
return position
}
override fun getItemCount(): Int = assetsArrayList.size
inner class MyViewHolder(val binding: ItemAssetsListingBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(dataListType: AssetDataObject) {
binding.tvViewDetails.underline()
binding.tvDescription.text = dataListType.updatedDescription
if (dataListType.acqAmount != null) {
binding.tvAssetsAmount.text =
"$" + dataListType.acqAmount.toString()
}
if(assetsArrayList.size <=1){
binding.ViewDivider.visibility = View.GONE
binding.relativeLayout2.paddingBottom
val paddingDp = 15
val density = context.resources?.displayMetrics?.density
val paddingPixel = (paddingDp * density!!)
binding.relativeLayout2.setPadding(0, 0,0,paddingPixel.toInt())
} else{
binding.ViewDivider.visibility = View.VISIBLE
}
binding.tvBrand.text = dataListType.updatedManufacture.toString()
binding.tvModel.text = dataListType.updatedModel.toString()
binding.tvUpdatedSerialNo.text =
dataListType.updatedSerialNumber.toString()
binding.tvUpdatedTagNo.text =
dataListType.updatedTagNumber.toString()
binding.tvDate.text = dataListType.acqDate.toString()
binding.tvViewDetails.setOnClickListener {
mItemClickListener.onViewDetailsClick(
position,
dataListType.updatedDescription.toString()
)
}
try {
if (dataListType.acqDate != null) {
binding.tvDate.text = Utils.convertTZToLocal(
mContext,
dataListType.acqDate.toString().replace("T", " "),
MY_CUSTOM_FORMAT
)
}
} catch (e: Exception) {
Log.d("Exception", "e" + e.printStackTrace())
}
binding.cbAssets.isChecked = selectedPosition == bindingAdapterPosition
binding.cbAssets.setOnClickListener {
binding.root.performClick()
}
binding.root.setOnClickListener {
selectedPosition = if (selectedPosition != bindingAdapterPosition) {
bindingAdapterPosition
} else {
-1
}
mItemClickListener.onCheckBoxClick(
selectedPosition, assetsArrayList.get(absoluteAdapterPosition).updatedBuildingId, assetsArrayList.get(absoluteAdapterPosition).updatedRoomId.toString()
)
Handler(Looper.getMainLooper()).post {
notifyDataSetChanged()
}
}
}
}
}