package `in`.techware.carrefour.viewModels

import `in`.techware.carrefour.model.MenuBean
import `in`.techware.carrefour.model.OriginBean
import `in`.techware.carrefour.model.WasteBean
import android.arch.lifecycle.ViewModel

/**
 * Created by Jemsheer K D on 23 July, 2018.
 * Package `in`.techware.carrefour.viewModels
 * Project Carrefour
 */
class WasteViewModel : ViewModel() {

    var originTypeSelectedPosition: Int = -1
    var originTypeCode: String = ""
    var originType: String = ""
    var originTypeList: List<OriginBean> = ArrayList()
    var barcode: String = ""
    var isQuantityEnabled: Boolean = true
    var quantity: Float = 1F

    var selectedMenuBean: MenuBean? = null
    var selectedSubMenuBean: MenuBean? = null

    var lastEntry: WasteBean? = null
    var isModifyEnabled: Boolean = false
    var modifiedID: Long = 0

    var wasteList: ArrayList<WasteBean> = ArrayList()

    fun setData(bean: WasteBean) {

        originTypeSelectedPosition = bean.originTypeSelectedPosition
        originTypeCode = bean.originTypeCode
        originType = bean.originType
        barcode = bean.barcode
        isQuantityEnabled = bean.isQuantityEnabled
        quantity = bean.quantity

        lastEntry = null
    }

    fun generateWasteBean(): WasteBean {
        var wasteBean = WasteBean()

        wasteBean.originTypeSelectedPosition = originTypeSelectedPosition
        wasteBean.originTypeCode = originTypeCode
        wasteBean.originType = originType
        wasteBean.barcode = barcode
        wasteBean.isQuantityEnabled = isQuantityEnabled
        wasteBean.quantity = quantity

        return wasteBean
    }

    fun isDataCollected(): Boolean {
        return (originTypeSelectedPosition != -1 && originTypeCode != ""
                && originType != "" && barcode != "" && quantity != 0F)

    }

    fun clearWaste() {
        barcode = ""
//        isQuantityEnabled = true
        quantity = 1F
    }

    fun indexOf(wasteBean: WasteBean): Int {
        for (bean in wasteList) {
            if (bean.id == wasteBean.id) {
                return wasteList.indexOf(bean)
            }
        }
        return -1
    }

    fun lastIndex(): Int {
        return wasteList.size - 1
    }

}