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


import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import es.verdnatura.R
import es.verdnatura.domain.GetAjustesUserCase
import es.verdnatura.domain.GetItemCardUserCase
import es.verdnatura.domain.GetLoginUserCase
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.base.getMessageFromAllResponse
import es.verdnatura.presentation.base.nameofFunction
import es.verdnatura.presentation.common.Event
import es.verdnatura.presentation.common.ResponseItemNumber
import es.verdnatura.presentation.common.ResponseItemVO
import es.verdnatura.presentation.view.feature.ajustes.model.*
import es.verdnatura.presentation.view.feature.articulo.model.ItemPackingType
import es.verdnatura.presentation.view.feature.articulo.model.ItemPackingTypeList
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


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

    private val contextApp = context

    private val getAjustesUserCase: GetAjustesUserCase = GetAjustesUserCase(context)
    private val getLoginUserCase: GetLoginUserCase = GetLoginUserCase(context)
    private val getItemCardUserCase: GetItemCardUserCase = GetItemCardUserCase(context)

    val version: String = "5.0.0"

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

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

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

    private val _workerupdatePrinter by lazy { MutableLiveData<ResponseItemVO>() }
    val workerupdatePrinter: LiveData<ResponseItemVO>
        get() = _workerupdatePrinter

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

    private val _operatorGetNumberOfWagons by lazy { MutableLiveData<ResponseItemNumber>() }
    val operatorGetNumberOfWagons: LiveData<ResponseItemNumber>
        get() = _operatorGetNumberOfWagons

    private val _operatorGetTrainResponse by lazy { MutableLiveData<ResponseItemVO>() }
    val operatorGetTrainResponse: LiveData<ResponseItemVO>
        get() = _operatorGetTrainResponse

    private val _operatorGetItemPackingType by lazy { MutableLiveData<ResponseItemVO>() }
    val operatorGetItemPackingType: LiveData<ResponseItemVO>
        get() = _operatorGetItemPackingType

    private val _workerAllowedResponse by lazy { MutableLiveData<ResponseItemVO>() }
    val workerAllowedResponse: LiveData<ResponseItemVO>
        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 loadSectorList = Transformations.map(_sectorList) { Event(it) }
    val loadPrintersList = Transformations.map(_printerList) { Event(it) }
    val loadItemPackingList = Transformations.map(_itemspackinglist) { Event(it) }
    val loadTrainList = Transformations.map(_trainList) { Event(it) }

    fun inititializeDefaultAjusts(
        sectorDescrip: String,
        sectorFk: Int,
        warehouseFk: Int,
        carroDescrip: String,
        TypeTags: String,
        printerFk: Int,
        printerSelected: String,
        train: String?,
        itemPackingType: String?,
        actionIsAllowed: Boolean
    ) {
        _ajustesitem.clear()
        _ajustesitem.add(
            AjustesItemVO(
                0,
                contextApp.getString(R.string.sector),
                sectorDescrip,
                sectorFk,
                warehouseFk,
                0,
                printerSelected,
                true
            )
        )


        _ajustesitem.add(
            AjustesItemVO(
                1,
                contextApp.getString(R.string.wagoons),
                carroDescrip,
                0,
                0,
                0, contextApp.getString(R.string.printerWithout),
                false
            )
        )
        _ajustesitem.add(
            AjustesItemVO(
                2,
                contextApp.getString(R.string.printerLabel),
                printerSelected,
                0,
                0,
                printerFk,
                printerSelected,
                true
            )
        )
        _ajustesitem.add(
            AjustesItemVO(
                6,
                contextApp.getString(R.string.settingsTitleTrain),
                train,
                0,
                0,
                0,
                action=actionIsAllowed
            )
        )
        _ajustesitem.add(
            AjustesItemVO(
                7,
                contextApp.getString(R.string.settingsTitleItemPacking),
                itemPackingType,
                0,
                0,
                0,
                action=actionIsAllowed
            )
        )
        /*  _ajustesitem.add(
              AjustesItemVO(
                  3,
                  contextApp.getString(R.string.labelType),
                  TypeTags,
                  0,
                  0,
                  0, contextApp.getString(R.string.printerWithout)
              )
          )*/
        _ajustesitem.add(
            AjustesItemVO(
                4,
                contextApp.getString(R.string.vehicleControl),
                "",
                0,
                0,
                0, contextApp.getString(R.string.printerWithout),
                true
            )
        )
        _ajustesitem.add(
            AjustesItemVO(
                5,
                contextApp.getString(R.string.closeSession),
                "",
                0,
                0,
                0, contextApp.getString(R.string.printerWithout),
                true
            )
        )

    }

    /* fun getSectors(user: String, password: String) {
         getAjustesUserCase.getSectors(user, password)
             .enqueue(object : Callback<List<SectorItemVO>> {
                 override fun onFailure(call: Call<List<SectorItemVO>>, t: Throwable) {
                     val listError: ArrayList<SectorItemVO> = ArrayList()
                     listError.add(SectorItemVO(0, "", 0, true, t.message!!))
                     _sectorList.value = SectorListVO(listError)
                 }

                 override fun onResponse(
                     call: Call<List<SectorItemVO>>,
                     response: Response<List<SectorItemVO>>
                 ) {
                     if (response.body() != null) {
                         _sectorList.value = response.body()?.let { SectorListVO(it) }
                     } else {
                         val listError: ArrayList<SectorItemVO> = ArrayList()
                         listError.add(
                             SectorItemVO(
                                 0,
                                 "",
                                 0,
                                 true,
                                 "Error en la llamada sector_get"
                             )
                         )
                         _sectorList.value = SectorListVO(listError)
                     }
                 }

             })
     }*/

    fun getItemPackingTypeSalix(token: String) {
        getItemCardUserCase.getItemPackingTypeSalix(token).enqueue(object :
            Callback<List<ItemPackingType>> {
            override fun onFailure(call: Call<List<ItemPackingType>>, t: Throwable) {
                val listError: ArrayList<ItemPackingType> = ArrayList()
                listError.add(
                    ItemPackingType(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _itemspackinglist.value = ItemPackingTypeList(listError)
            }

            override fun onResponse(
                call: Call<List<ItemPackingType>>,
                response: Response<List<ItemPackingType>>
            ) {
                if (response.body() != null) {
                    _itemspackinglist.value = response.body()?.let { ItemPackingTypeList(it) }

                } else {
                    val listError: ArrayList<ItemPackingType> = ArrayList()
                    listError.add(
                        ItemPackingType(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _itemspackinglist.value = ItemPackingTypeList(listError)
                }
            }

        })
    }

    fun getSectorsSalix(token: String) {
        getAjustesUserCase.getSectorsSalix(token).enqueue(object : Callback<List<SectorItemVO>> {
            override fun onFailure(call: Call<List<SectorItemVO>>, t: Throwable) {
                val listError: ArrayList<SectorItemVO> = ArrayList()
                listError.add(SectorItemVO(0, "", 0, true, t.message!!))
                _sectorList.value = SectorListVO(listError)
            }

            override fun onResponse(
                call: Call<List<SectorItemVO>>,
                response: Response<List<SectorItemVO>>
            ) {


                if (response.body() != null) {
                    _sectorList.value = response.body()?.let { SectorListVO(it) }
                } else {
                    val listError: ArrayList<SectorItemVO> = ArrayList()
                    listError.add(
                        SectorItemVO(
                            0, "", 0, true,
                            getMessageFromAllResponse(nameofFunction(this), response.message())
                        )
                    )
                    _sectorList.value = SectorListVO(listError)
                }
            }

        })
    }

    fun printer_get(user: String, password: String) {
        getAjustesUserCase.printer_get(user, password).enqueue(object : Callback<List<Printers>> {
            override fun onFailure(call: Call<List<Printers>>, t: Throwable) {
                val listError: ArrayList<Printers> = ArrayList()
                listError.add(Printers(0, "", true, t.message!!))
                _printerList.value = PrintersList(listError)

            }

            override fun onResponse(
                call: Call<List<Printers>>,
                response: Response<List<Printers>>
            ) {


                if (response.body() != null) {
                    _printerList.value = response.body()?.let { PrintersList(it) }
                } else {
                    val listError: ArrayList<Printers> = ArrayList()
                    listError.add(
                        Printers(
                            0, "", true,
                            getMessageFromAllResponse(nameofFunction(this), response.message())
                        )
                    )
                    _printerList.value = PrintersList(listError)
                }
            }

        })
    }

    fun train_get(user: String, password: String) {
        getAjustesUserCase.train_get(user, password).enqueue(object : Callback<List<Train>> {
            override fun onFailure(call: Call<List<Train>>, t: Throwable) {
                val listError: ArrayList<Train> = ArrayList()
                listError.add(Train(0, "", true, t.message!!))
                _trainList.value = TrainList(listError)

            }

            override fun onResponse(
                call: Call<List<Train>>,
                response: Response<List<Train>>
            ) {


                if (response.body() != null) {
                    _trainList.value = response.body()?.let { TrainList(it) }
                } else {
                    val listError: ArrayList<Train> = ArrayList()
                    listError.add(
                        Train(
                            0, "", true,
                            getMessageFromAllResponse(nameofFunction(this), response.message())
                        )
                    )
                    _trainList.value = TrainList(listError)
                }
            }

        })
    }

    fun worker_updateSector(user: String, password: String, sectorFk: String) {

        getAjustesUserCase.worker_updateSector(user, password, sectorFk)
            .enqueue(object : Callback<Unit> {
                override fun onFailure(call: Call<Unit>, t: Throwable) {

                    _actionUpdate.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onResponse(
                    call: Call<Unit>,
                    response: Response<Unit>
                ) {

                    if (!response.isSuccessful) {
                        _actionUpdate.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {
                        _actionUpdate.value = ResponseItemVO(
                            response = response.message(),
                            isError = false,
                            errorMessage = ""

                        )
                    }


                }

            })
    }

    fun operator_updateItemPackingType(user: String, password: String, sectorFk: String) {

        getAjustesUserCase.operator_updateItemPackingType(user, password, sectorFk)
            .enqueue(object : Callback<Unit> {
                override fun onFailure(call: Call<Unit>, t: Throwable) {

                    _actionUpdate.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onResponse(
                    call: Call<Unit>,
                    response: Response<Unit>
                ) {

                    if (!response.isSuccessful) {
                        _actionUpdate.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {
                        _actionUpdate.value = ResponseItemVO(
                            response = response.message(),
                            isError = false,
                            errorMessage = ""

                        )
                    }


                }

            })
    }

    fun operator_getNumberOfWagons(user: String, password: String) {

        getLoginUserCase.operator_getNumberOfWagons(user, password)
            .enqueue(object : Callback<Int?> {
                override fun onFailure(call: Call<Int?>, t: Throwable) {

                    _operatorGetNumberOfWagons.value = ResponseItemNumber(
                        null,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onResponse(
                    call: Call<Int?>,
                    response: Response<Int?>
                ) {

                    if (!response.isSuccessful) {
                        _operatorGetNumberOfWagons.value = ResponseItemNumber(
                            null, true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {

                        if (response.body() != null)
                            _operatorGetNumberOfWagons.value =
                                ResponseItemNumber(response.body(), isError = false)


                    }

                }

            })
    }

    fun operator_getTrain(user: String, password: String) {

        getAjustesUserCase.operator_getTrain(user, password)
            .enqueue(object : Callback<String?> {
                override fun onFailure(call: Call<String?>, t: Throwable) {

                    _operatorGetTrainResponse.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onResponse(
                    call: Call<String?>,
                    response: Response<String?>
                ) {

                    if (!response.isSuccessful) {
                        _operatorGetTrainResponse.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {

                        _operatorGetTrainResponse.value =
                            ResponseItemVO(response.body().toString(), false)

                    }


                }
            })
    }

    fun operator_getItemPackingType(user: String, password: String) {

        getAjustesUserCase.operator_getItemPackingType(user, password)
            .enqueue(object : Callback<String?> {
                override fun onFailure(call: Call<String?>, t: Throwable) {

                    _operatorGetItemPackingType.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onResponse(
                    call: Call<String?>,
                    response: Response<String?>
                ) {

                    if (!response.isSuccessful) {
                        _operatorGetItemPackingType.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {

                        _operatorGetItemPackingType.value =
                            ResponseItemVO(response.body().toString(), false)

                    }


                }
            })
    }

    fun worker_isF11Allowed(user: String, password: String) {

        getAjustesUserCase.worker_isF11Allowed(user, password)
            .enqueue(object : Callback<Boolean> {
                override fun onFailure(call: Call<Boolean>, t: Throwable) {

                    _workerAllowedResponse.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onResponse(
                    call: Call<Boolean>,
                    response: Response<Boolean>
                ) {

                    if (!response.isSuccessful) {
                        _workerAllowedResponse.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {

                        _workerAllowedResponse.value =
                            ResponseItemVO(response.body().toString(), false)

                    }


                }
            })
    }

    fun worker_updatePrinter(user: String, password: String, printerFk: String) {
        //d("La impresora marcada  " + printerFk)
        getAjustesUserCase.worker_updatePrinter(user, password, printerFk)
            .enqueue(object : Callback<Unit> {
                override fun onFailure(call: Call<Unit>, t: Throwable) {

                    _workerupdatePrinter.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onResponse(
                    call: Call<Unit>,
                    response: Response<Unit>
                ) {

                    if (!response.isSuccessful) {
                        _workerupdatePrinter.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {
                        _workerupdatePrinter.value = ResponseItemVO(
                            response = response.message(),
                            isError = false,
                            errorMessage = ""
                        )

                    }

                }

            })
    }

    fun operator_updateTrain(user: String, password: String, trainFk: String) {

        getAjustesUserCase.operator_updateTrain(user, password, trainFk)
            .enqueue(object : Callback<Unit> {
                override fun onFailure(call: Call<Unit>, t: Throwable) {

                    _actionUpdate.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onResponse(
                    call: Call<Unit>,
                    response: Response<Unit>
                ) {

                    if (!response.isSuccessful) {
                        _actionUpdate.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {
                        _actionUpdate.value = ResponseItemVO(
                            response = response.message(),
                            isError = false,
                            errorMessage = ""

                        )
                    }


                }

            })
    }
}
