package es.verdnatura.presentation.view.feature.packaging.fragment.PackagingViewModel

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
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.packaging.model.*
import okhttp3.MultipartBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

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


    private val _itemSupplierList by lazy { MutableLiveData<itemSupplierList>() }
    val itemSupplierList: LiveData<itemSupplierList>
        get() = _itemSupplierList

    private val _supplierList by lazy { MutableLiveData<SupplierList>() }
    val supplierList: LiveData<SupplierList>
        get() = _supplierList

    private val _entry by lazy { MutableLiveData<EntrySalix>() }
    val entry: LiveData<EntrySalix>
        get() = _entry

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

    fun getSuppliers() {
        salix.getSuppliers()
            .enqueue(object :
                SilexCallback<List<Supplier>>(context) {
                override fun onError(t: Throwable) {
                    val listError: ArrayList<Supplier> = ArrayList()
                    listError.add(
                        Supplier(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                t.message!!
                            )
                        )
                    )
                    _supplierList.value = SupplierList(listError)
                }

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

                    }
                }

            })
    }

    fun supplier_getItems(supplierFk: Int) {

        silex.supplier_getItems(supplierFk)
            .enqueue(object : SilexCallback<List<ItemSupplier>>(context) {
                override fun onError(t: Throwable) {
                    val listError: ArrayList<ItemSupplier> = ArrayList()
                    listError.add(
                        ItemSupplier(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                t.message!!
                            )
                        )
                    )
                    _itemSupplierList.value = itemSupplierList(listError)
                }

                override fun onSuccess(response: Response<List<ItemSupplier>>) {
                    if (response.body() != null) {
                        _itemSupplierList.value =
                            response.body()?.let { itemSupplierList(it) }
                    } else {
                        val listError: ArrayList<ItemSupplier> = ArrayList()
                        listError.add(ItemSupplier(null, null))
                        _itemSupplierList.value = itemSupplierList(listError)
                    }
                }

            })
    }

    fun entry_addSalix(entry: EntrySalix) {
        salix.Entries(entry = entry)
            .enqueue(object :
                SilexCallback<EntrySalix> (context){
                override fun onError(t: Throwable) {
                    _entry.value = EntrySalix(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            t.message!!
                        )
                    )
                }

                override fun onSuccess(response: Response<EntrySalix>) {
                    if (response.body() != null) {
                        _entry.value = response.body()
                    } else {
                        _entry.value = EntrySalix(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    }

                }
            })
    }

    fun entry_uploadPhotoSalix(id: Number, warehouseId: Number, companyId: Number, dmsTypeId: Number, reference: String, description: String, hasFile: Boolean, file: MultipartBody.Part) {
         salix.uploadEntryPhoto(id, warehouseId, companyId, dmsTypeId, reference, description, hasFile, file)
            .enqueue(object : SilexCallback<Any>(context){})
    }

    fun entry_addFromBuy(id: Number, item: Number, printedStickers: Number) {
        salix.addFromBuy(id, item, printedStickers)
            .enqueue(object : SilexCallback<Any>(context){})
    }
}
