package com.wayne.www.waynelib.ui.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.wayne.www.waynelib.fdc.model.FuelSaleTransaction
import com.wayne.www.waynelib.fdc.model.Nozzle
import com.wayne.www.waynelib.ui.view.NozzleGridView
import com.wayne.www.waynelib.ui.view.TransactionDetailView
import org.slf4j.LoggerFactory

class LatestTrxsAdapter : BaseAdapter {
    private var latestTrxs: List<FuelSaleTransaction?>? = mutableListOf()
    private var context: Context? = null
    private var numOfNozzles: Int = 0
    private val fileLogger = LoggerFactory.getLogger(NozzleGridView::class.java)

    constructor(baseContext: Context?, trxList: List<FuelSaleTransaction?>?, numOfNozzles: Int) {
        this.context = baseContext
        if (trxList != null) {
            this.latestTrxs = trxList
        }
        this.numOfNozzles = numOfNozzles
    }

    fun setData(data: List<FuelSaleTransaction?>) {
        this.latestTrxs = data
    }

    override fun getView(position: Int, view: View?, vg: ViewGroup?): View {
        if (view == null) {
            val v = TransactionDetailView(this.context, this.latestTrxs!!.get(position)!!, numOfNozzles)
            return v
        } else {
            return TransactionDetailView(this.context, this.latestTrxs!!.get(position)!!, numOfNozzles)
        }
    }

    override fun getItem(position: Int): Any {
        return latestTrxs!!.get(position)!!
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return if (latestTrxs!!.count() <= Nozzle.LATEST_TRX_COUNT)  latestTrxs!!.count() else Nozzle.LATEST_TRX_COUNT
        //return latestTrxs!!.count()
    }
}