package es.verdnatura.presentation.view.feature.ajustes.fragment

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import es.verdnatura.R
import es.verdnatura.domain.SalixCallback
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.common.Event
import es.verdnatura.presentation.view.feature.ajustes.model.AjustesItemVO
import es.verdnatura.presentation.view.feature.ajustes.model.Printers
import es.verdnatura.presentation.view.feature.ajustes.model.PrintersList
import es.verdnatura.presentation.view.feature.ajustes.model.SectorItemVO
import es.verdnatura.presentation.view.feature.ajustes.model.SectorListVO
import es.verdnatura.presentation.view.feature.ajustes.model.Train
import es.verdnatura.presentation.view.feature.ajustes.model.TrainList
import es.verdnatura.presentation.view.feature.articulo.model.ItemPackingType
import es.verdnatura.presentation.view.feature.articulo.model.ItemPackingTypeList
import es.verdnatura.presentation.view.feature.login.model.Operator
import retrofit2.Response

class AjustesViewModel(val context: Context) : BaseViewModel(context) {

    private val _settingsItem by lazy { ArrayList<AjustesItemVO>() }
    val settingsItem: List<AjustesItemVO>
        get() = _settingsItem

    private val _sectorList by lazy { MutableLiveData<SectorListVO>() }
    val sectorList: LiveData<SectorListVO>
        get() = _sectorList

    private val _actionUpdate by lazy { MutableLiveData<String>() }
    val actionUpdate: LiveData<String>
        get() = _actionUpdate

    private val _printerList by lazy { MutableLiveData<PrintersList>() }
    val printerList: LiveData<PrintersList>
        get() = _printerList

    private val _workerAllowedResponse by lazy { MutableLiveData<Boolean>() }
    val workerAllowedResponse: LiveData<Boolean>
        get() = _workerAllowedResponse

    private val _itemspackinglist by lazy { MutableLiveData<ItemPackingTypeList>() }
    val itemspackinglist: LiveData<ItemPackingTypeList>
        get() = _itemspackinglist

    private val _trainList by lazy { MutableLiveData<TrainList>() }
    val trainList: LiveData<TrainList>
        get() = _trainList

    val loadActionUpdate: LiveData<Event<String>> = _actionUpdate.map { Event(it) }
    val loadSectorList: LiveData<Event<SectorListVO>> = _sectorList.map { Event(it) }
    val loadPrintersList: LiveData<Event<PrintersList>> = _printerList.map { Event(it) }
    val loadItemPackingList: LiveData<Event<ItemPackingTypeList>> =
        _itemspackinglist.map { Event(it) }
    val loadTrainList: LiveData<Event<TrainList>> = _trainList.map { Event(it) }

    fun inititializeDefaultAjusts(
        sectorDescrip: String,
        sectorFk: Int,
        wagoonDescrip: String,
        printerFk: Int,
        printerSelected: String,
        train: String?,
        itemPackingType: String?,
        menuDefault: String,
        holdPosition: Boolean = false
    ) {
        _settingsItem.clear()
        _settingsItem.add(
            AjustesItemVO(
                context.getString(R.string.sector), sectorDescrip, sectorFk, action = true
            )
        )
        _settingsItem.add(
            AjustesItemVO(
                context.getString(R.string.wagoons), wagoonDescrip, action = false
            )
        )
        _settingsItem.add(
            AjustesItemVO(
                context.getString(R.string.printerLabel),
                printerSelected,
                printerFk = printerFk,
                printerSelected = printerSelected,
                action = true
            )
        )
        _settingsItem.add(
            AjustesItemVO(
                context.getString(R.string.settingsTitleTrain), train, action = false
            )
        )
        _settingsItem.add(
            AjustesItemVO(
                context.getString(R.string.settingsTitleItemPacking),
                itemPackingType,
                action = false
            )
        )
        /*Tarea 5443
         _settingsItem.add(
               AjustesItemVO(
                   "Bajar línea al revisar",
                   holdPosition = holdPosition,
                   action = true
               )
           )*/

        _settingsItem.add(
            AjustesItemVO(
                context.getString(R.string.vehicleControl), action = true
            )
        )
        _settingsItem.add(
            AjustesItemVO(
                context.getString(R.string.menuByDefault), selected = menuDefault, action = true
            )
        )
        _settingsItem.add(
            AjustesItemVO(
                context.getString(R.string.runActivityStop), action = true
            )
        )
        _settingsItem.add(
            AjustesItemVO(
                context.getString(R.string.closeSession), action = true
            )
        )

    }

    fun getItemPackingTypeSalix() {
        salix.getItemPackingType().enqueue(object : SalixCallback<List<ItemPackingType>>(context) {
            override fun onSuccess(response: Response<List<ItemPackingType>>) {

                _itemspackinglist.value = response.body()?.let { ItemPackingTypeList(it) }
            }

        })
    }

    fun getSectorsSalix() {
        salix.getSectors("""{"fields":["id","description","warehouseFk"],"order":"description"}""")
            .enqueue(object : SalixCallback<List<SectorItemVO>>(context) {

                override fun onSuccess(response: Response<List<SectorItemVO>>) {
                    _sectorList.value = response.body()?.let { SectorListVO(it) }
                }

            })
    }

    fun printerGet(sectorFk: Int) {
        salix.getprinters(
            """{"fields":["id","name","sectorFk"],"where":{"sectorFk":$sectorFk,"isLabeler":{"neq":false}},"include": [ { "relation": "sector", "scope": { "fields": ["backupPrinterFk"]}}]}"""
        ).enqueue(object : SalixCallback<MutableList<Printers>>(context) {
            override fun onSuccess(response: Response<MutableList<Printers>>) {
                _printerList.value = response.body()?.let { PrintersList(it) }
            }

        })
    }

    fun trainGet() {
        salix.getTrain().enqueue(object : SalixCallback<List<Train>>(context) {
            override fun onSuccess(response: Response<List<Train>>) {
                _trainList.value = response.body()?.let { TrainList(it) }
            }

        })
    }

    fun workerUpdateOperatorSalix(entity: String, workerFk: Int, sectorFk: Int?, labelerFk: Int?) {
        salix.updateOperator(id = workerFk, Operator(sectorFk, labelerFk))
            .enqueue(object : SalixCallback<Unit>(context) {

                override fun onSuccess(response: Response<Unit>) {
                    if (entity == "sector") {
                        _actionUpdate.value = response.message()
                    }

                    super.onSuccess(response)
                }

            })
    }

    fun operatorUpdate(workerId: Int, update: HashMap<String, String>) {

        salix.updateOperator(id = workerId, params = update)
            .enqueue(object : SalixCallback<Unit>(context) {})
    }

}

