package com.doverfuelingsolutions.issp.view.adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.doverfuelingsolutions.issp.R
import com.doverfuelingsolutions.issp.utils.StringUtil
import com.wayne.www.waynelib.fdc.message.DeviceClass
import kotlinx.android.synthetic.main.adapter_order_item.view.*

class OrderListAdapter(
    private val orderList: List<DeviceClass>,
    private val handler: (dc: DeviceClass) -> Unit
) : RecyclerView.Adapter<OrderListAdapter.OrderListViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OrderListViewHolder {
        val view = View.inflate(parent.context, R.layout.adapter_order_item, null)
        return OrderListViewHolder(view)
    }

    override fun onBindViewHolder(holder: OrderListViewHolder, position: Int) {
        val order = orderList[position]
        holder.itemView.oilName.text = order.productName
        holder.itemView.nozzleName.text = StringUtil.get(R.string.nozzle_name, order.nozzleNo)
        holder.itemView.fuelTime.text = order.endTimeStamp.substring(10)
        holder.itemView.amount.text = StringUtil.get(R.string.amount_rmb, order.amount.toString())
        holder.itemView.volume.text = StringUtil.get(R.string.volume_l, order.volume.toString())
        holder.itemView.orderBox.setOnClickListener {
            handler(order)
        }

        when {
            order.isLock -> {
                holder.itemView.iconStatus.setImageResource(R.drawable.ic_lock)
            }
            order.releaseTokenElement == null || order.releaseTokenAttribute == null -> {
                holder.itemView.iconStatus.setImageResource(R.drawable.ic_error)
            }
            else -> {
                holder.itemView.iconStatus.visibility = View.GONE
            }
        }
    }

    override fun getItemCount() = orderList.size

    class OrderListViewHolder(view: View) : RecyclerView.ViewHolder(view)
}