package `in`.techware.carrefour.viewModels

import `in`.techware.carrefour.model.DepartmentBean
import `in`.techware.carrefour.model.InternalSalesCorrectionBean
import `in`.techware.carrefour.model.MenuBean
import `in`.techware.carrefour.model.SectionBean
import android.arch.lifecycle.ViewModel

/**
 * Created by Jemsheer K D on 23 July, 2018.
 * Package `in`.techware.carrefour.viewModels
 * Project Carrefour
 */
class InternalSalesCorrectionViewModel : 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: InternalSalesCorrectionBean? = null
    var isModifyEnabled: Boolean = false
    var modifiedID: Long = 0

    var internalSalesCorrectionList: ArrayList<InternalSalesCorrectionBean> = ArrayList()

    fun setData(bean: InternalSalesCorrectionBean) {

        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 generateInternalSalesCorrectionBean(): InternalSalesCorrectionBean {
        var internalSalesCorrectionBean = InternalSalesCorrectionBean()

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

        return internalSalesCorrectionBean
    }

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

    }

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

    fun indexOf(internalSalesCorrectionBean: InternalSalesCorrectionBean): Int {
        for (bean in internalSalesCorrectionList) {
            if (bean.id == internalSalesCorrectionBean.id) {
                return internalSalesCorrectionList.indexOf(bean)
            }
        }
        return -1
    }

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

}