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.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.ResponseItemVO
import es.verdnatura.presentation.view.feature.ajustes.model.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import timber.log.Timber.d


class AjustesViewModel(context: Context) : BaseViewModel() {
    
    private val contextApp=context

    private val getAjustesUserCase: GetAjustesUserCase = GetAjustesUserCase(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 _workerupdateSector by lazy { MutableLiveData<ResponseItemVO>() }
    val workerupdateSector: LiveData<ResponseItemVO>
        get() = _workerupdateSector

    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


    val loadSectorList = Transformations.map(_sectorList) { Event(it) }
    val loadprintersList = Transformations.map(_printerList) { Event(it) }


    fun inititializeDefaultAjusts(
        sectorDescrip: String,
        sectorFk: Int,
        warehouseFk: Int,
        carroDescrip: String,
        TypeTags: String,
        printerFk: Int,
        printerSelected: String
    ) {
        _ajustesitem.add(
            AjustesItemVO(
                0,
                contextApp.getString(R.string.sector),
                sectorDescrip,
                sectorFk,
                warehouseFk,
                0,
                printerSelected
            )
        )


        _ajustesitem.add(
            AjustesItemVO(
                1,
                contextApp.getString(R.string.wagoons),
                carroDescrip,
                0,
                0,
                0, contextApp.getString(R.string.printerWithout)
            )
        )
        _ajustesitem.add(
            AjustesItemVO(
                2,
                contextApp.getString(R.string.printerLabel),
                printerSelected,
                0,
                0,
                printerFk, printerSelected
            )
        )
        _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)
            )
        )
        _ajustesitem.add(
            AjustesItemVO(
                5,
                contextApp.getString(R.string.closeSession),
                "",
                0,
                0,
                0, contextApp.getString(R.string.printerWithout)
            )
        )

    }

   /* 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 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 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) {

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

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

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

                        )
                    }

                    /*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 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 = "")

                    }

                }

            })
    }
}
