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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.SilexCallback
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.base.getMessageFromAllResponse
import es.verdnatura.presentation.base.nameofFunction
import es.verdnatura.presentation.common.ResponseItemVO
import es.verdnatura.presentation.view.feature.collection.mapper.map
import es.verdnatura.presentation.view.feature.sacador.mapper.toDateFormat
import es.verdnatura.presentation.view.feature.sacador.mapper.toDateFormating
import es.verdnatura.presentation.view.feature.sacador.model.CollectionListVO
import es.verdnatura.presentation.view.feature.sacador.model.CollectionTicket
import es.verdnatura.presentation.view.feature.sacador.model.CollectionTicketList
import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO
import retrofit2.Call
import retrofit2.Response

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

    private val _collectionList by lazy { MutableLiveData<CollectionListVO>() }
    val collectionList: LiveData<CollectionListVO>
        get() = _collectionList

    private val _collectionListLocal by lazy { MutableLiveData<CollectionTicketList>() }
    val collectionListLocal: LiveData<CollectionTicketList>
        get() = _collectionListLocal

    private val _collectionTicketList by lazy { MutableLiveData<CollectionVO>() }
    val collectionTicketList: LiveData<CollectionVO>
        get() = _collectionTicketList

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

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

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

    fun collectionTicketGet(
        collectionFk: Int,
        sectorFk: String,
        print: String,
        type: String
    ) {

        //Tarea 6276, falta modificar
        silex.collection_getTickets(
            collectionFk,
            sectorFk,
            print,
            type
        ).enqueue(object :
            SilexCallback<CollectionVO>(context) {
            override fun onError(t: Throwable) {
                _collectionTicketList.value = CollectionVO(
                    0,
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

            override fun onResponse(
                call: Call<CollectionVO>,
                response: Response<CollectionVO>
            ) {
                if (response.body() != null) {
                    _collectionTicketList.value = response.body()?.let { it.map(context) }
                } else {
                    _collectionTicketList.value = CollectionVO(
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            response.message()
                        )
                    )
                }
            }
        })
    }

    fun sectorCollectionNew(
        sectorFk: Int
    ) {
        salix.sectorCollectionNew(arrayListOf(sectorFk))
            .enqueue(
                object :
                    SalixCallback<Any>(context) {
                    override fun onSuccess(response: Response<Any>) {
                        _response.value = ResponseItemVO(
                            isError = false,
                            errorMessage = "" + getMessageFromAllResponse(
                                nameofFunction(this), response.message()
                            )
                        )
                    }

                    override fun onError(t: Throwable) {
                        _response.value =
                            ResponseItemVO(
                                isError = true,
                                errorMessage = getMessageFromAllResponse(
                                    nameofFunction(this),
                                    t.message!!
                                )
                            )
                    }
                })
    }

    fun ticketPrintLabelPrevious(ticketFk: Int) {

        salix.ticketPrintLabelPrevious(arrayListOf(ticketFk))
            .enqueue(object : SalixCallback<Any>(context) {
                override fun onSuccess(response: Response<Any>) {
                    _response.value =
                        ResponseItemVO(isError = false, response = response.message()!!)
                }
            })

    }

    fun collection_assign() {
        //Tarea 6276
        //salix.collectionAssign()
        silex.collection_assign()
            .enqueue(
                object :
                    SilexCallback<Int>(context) {
                    override fun onError(t: Throwable) {
                        _response.value =
                            ResponseItemVO(
                                isError = true,
                                errorMessage = getMessageFromAllResponse(
                                    nameofFunction(this),
                                    t.message!!
                                )
                            )
                    }

                    override fun onSuccess(response: Response<Int>) {
                        _response.value =
                            ResponseItemVO(isError = false, response = response.body()!!.toString())
                    }
                })
    }

    fun collection_assignNew() {
       // salix.collectionAssign()
        silex.collection_assign().enqueue(
            object :
                SalixCallback<Int>(context) {
                override fun onError(t: Throwable) {
                    _response.value =
                        ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                t.message!!
                            )
                        )
                }

                override fun onSuccess(response: Response<Int>) {
                    if (response.body() != null) {
                        // itemShelvingSale_addByCollection(response.body()!!)
                        _response.value =
                            ResponseItemVO(isError = false, response = response.body()!!.toString())
                    } else {

                        _response.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )

                    }
                }
            })
    }

    fun itemShelvingSale_addByCollection(collection: Int) {
        salix.itemShelvingSale_addByCollection(collection).enqueue(
            object :
                SalixCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _response.value =
                        ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                t.message!!
                            )
                        )
                }

                override fun onSuccess(response: Response<Any>) {
                    if (response.body() != null) {

                        _response.value =
                            ResponseItemVO(isError = false, response = collection.toString())
                    } else {

                        _response.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )

                    }
                }
            })
    }

    fun collectionGet() {
        salix.getCollectionSalix().enqueue(object :
            SalixCallback<List<CollectionVO>>(context) {
            override fun onError(t: Throwable) {
                val listError: ArrayList<CollectionVO> = ArrayList()
                listError.add(
                    CollectionVO(
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _collectionList.value = CollectionListVO(listError)
            }

            override fun onSuccess(response: Response<List<CollectionVO>>) {
                if (response.body() != null) {
                    _collectionList.value =
                        response.body()?.let { CollectionListVO(it.toDateFormat(context)) }
                } else {
                    val listError: ArrayList<CollectionVO> = ArrayList()
                    listError.add(
                        CollectionVO(
                            0,
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _collectionList.value = CollectionListVO(listError)

                }
            }

        })
    }

    fun sectorCollectionGet() {
        salix.sectorCollectionGet().enqueue(object :
            SalixCallback<List<CollectionVO>>(context) {
            override fun onSuccess(response: Response<List<CollectionVO>>) {

                if (response.body() != null) {
                    _collectionList.value =
                        response.body()?.let { CollectionListVO(it.toDateFormat(context)) }
                } else {
                    val listError: ArrayList<CollectionVO> = ArrayList()
                    listError.add(
                        CollectionVO(
                            0,
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _collectionList.value = CollectionListVO(listError)

                }
            }

            override fun onError(t: Throwable) {
                val listError: ArrayList<CollectionVO> = ArrayList()
                listError.add(
                    CollectionVO(
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            t.message!!
                        )
                    )
                )
                _collectionList.value = CollectionListVO(listError)
            }
        })
    }

    fun collectionGetSalix() {
        salix.getCollectionSalix().enqueue(object :
            SalixCallback<List<CollectionVO>>(context) {
            override fun onSuccess(response: Response<List<CollectionVO>>) {
                _collectionList.value =
                    response.body()?.let { CollectionListVO(it.toDateFormat(context)) }
            }

        })
    }

    fun collectionGetSalixLocal() {
        salix.getCollectionSalixLocal().enqueue(object :
            SalixCallback<List<CollectionTicket>>(context) {
            override fun onError(t: Throwable) {
                val listError: ArrayList<CollectionTicket> = ArrayList()
                listError.add(
                    CollectionTicket(
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _collectionListLocal.value = CollectionTicketList(listError)
            }

            override fun onSuccess(response: Response<List<CollectionTicket>>) {

                if (response.body() != null) {
                    _collectionListLocal.value =
                            // CollectionTicketList(mutableListOf(collectionLocal))
                        response.body()?.let { CollectionTicketList(it.toDateFormating(context)) }
                } else {
                    val listError: ArrayList<CollectionTicket> = ArrayList()
                    listError.add(
                        CollectionTicket(
                            0,
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _collectionListLocal.value = CollectionTicketList(listError)

                }
            }

        })
    }
}
