package `in`.techware.carrefour.viewModels

import `in`.techware.carrefour.model.*
import android.arch.lifecycle.ViewModel

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

    var departmentSelectedPosition: Int = -1
    var departmentCode: String = ""
    var department: String = ""
    var departmentList: List<DepartmentBean> = ArrayList()
    var sectionSelectedPosition: Int = -1
    var sectionCode: String = ""
    var section: String = ""
    var sectionList: List<SectionBean> = ArrayList()
    var barcode: String = ""
    var isQuantityEnabled: Boolean = true
    var quantity: Float = 1F

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

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

    var internalSalesList: ArrayList<InternalSalesBean> = ArrayList()

    fun setData(bean: InternalSalesBean) {

        departmentSelectedPosition = bean.departmentSelectedPosition
        departmentCode = bean.departmentCode
        department = bean.department
        sectionSelectedPosition = bean.sectionSelectedPosition
        sectionCode = bean.sectionCode
        section = bean.section
        barcode = bean.barcode
        isQuantityEnabled = bean.isQuantityEnabled
        quantity = bean.quantity

        lastEntry = null
    }

    fun generateInternalSalesBean(): InternalSalesBean {
        var internalSalesBean = InternalSalesBean()

        internalSalesBean.departmentSelectedPosition = departmentSelectedPosition
        internalSalesBean.departmentCode = departmentCode
        internalSalesBean.department = department
        internalSalesBean.sectionSelectedPosition = sectionSelectedPosition
        internalSalesBean.sectionCode = sectionCode
        internalSalesBean.section = section
        internalSalesBean.barcode = barcode
        internalSalesBean.isQuantityEnabled = isQuantityEnabled
        internalSalesBean.quantity = quantity

        return internalSalesBean
    }

    fun isDataCollected(): Boolean {
        return (departmentSelectedPosition != -1 && departmentCode != ""
                && department != "" && sectionSelectedPosition != -1 && sectionCode != ""
                && section != "" && barcode != "" && quantity != 0F)

    }

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

    fun indexOf(internalSalesBean: InternalSalesBean): Int {
        for (bean in internalSalesList) {
            if (bean.id == internalSalesBean.id) {
                return internalSalesList.indexOf(bean)
            }
        }
        return -1
    }

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

}