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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.formatWithQuotes
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.common.Event
import es.verdnatura.presentation.view.feature.roadmap.model.RoadMapTruck
import es.verdnatura.presentation.view.feature.roadmap.model.RoadMapTruckList
import es.verdnatura.presentation.view.feature.roadmap.model.RoadStopMapTruck
import es.verdnatura.presentation.view.feature.roadmap.model.RoadStopMapTruckList
import es.verdnatura.presentation.view.feature.roadmap.model.StopMapTruckPallet
import es.verdnatura.presentation.view.feature.roadmap.model.StopMapTruckPalletList
import retrofit2.Response

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

    private val _roadMapTruckList by lazy { MutableLiveData<RoadMapTruckList>() }
    val loadRoadMapTruckList: LiveData<Event<RoadMapTruckList>> =
        _roadMapTruckList.map { Event(it) }

    private val _stopMapTruckList by lazy { MutableLiveData<RoadStopMapTruckList>() }
    val loadStopMapTruckList: LiveData<Event<RoadStopMapTruckList>> =
        _stopMapTruckList.map { Event(it) }

    private val _stopMapTruckPalletList by lazy { MutableLiveData<StopMapTruckPalletList>() }
    val loadStopMapTruckPalletList: LiveData<Event<StopMapTruckPalletList>> =
        _stopMapTruckPalletList.map { Event(it) }

    private val _responseState by lazy { MutableLiveData<Boolean>() }
    val loadResponseState: LiveData<Event<Boolean>> =
        _responseState.map { Event(it) }

    private val _responseUpdate by lazy { MutableLiveData<Boolean>() }
    var responseUpdate: LiveData<Boolean> = _responseUpdate

    fun roadMapTruckList(filter: String) {
        salix.roadmapTruckGet(
            filter
        ).enqueue(object : SalixCallback<List<RoadMapTruck>>(context) {
            override fun onSuccess(response: Response<List<RoadMapTruck>>) {
                _roadMapTruckList.value =
                    response.body()?.let { RoadMapTruckList(it) }
            }
        })
    }

    fun roadMapUpdate(roadmapFk: Int, params: Any) {
        salix.roadMapUpdate(
            id = roadmapFk,
            params = params
        ).enqueue(object : SalixCallback<Any>(context) {
            override fun onSuccess(response: Response<Any>) {
                _responseUpdate.value = true
                super.onSuccess(response)

            }
        })
    }

    fun getStopMapTruckList(roadMap: Int) {

        salix.getStopMapTruckList(
            """{ "where": { "roadmapFk": $roadMap }, "fields": ["id", "eta", "description", "addressFk"], 
                |"include": [ { "relation":"address", "scope":{ "fields":["addressFk"], 
                    |"include":{ "relation": "address", "scope": { "fields": ["id","nickname", "street", "city", "postalCode", "latitude", "longitude", "mobile", "phone", "address", "clientFk"], 
                        |"include": [ { "relation": "client", "scope": { "fields": ["phone"] } } ] } }}} ] }""".trimMargin()
           /* """{ "where":{"roadMapFk":$roadMap},
                | "fields": [ "id","eta","description","addressFk"],
                | "include": [ { "relation": "address", 
                |   "scope":{ "fields": ["nickname","street","city","postalCode","latitude","longitude","mobile","phone","address","clientFk"],
                |        "include":[{ "relation":"client", 
                |        "scope":{ "fields": ["phone"] } } ]} } ] } } ] }""".trimMargin()*/
            // """{"where": { "roadMapFk":$roadMap},"order": "eta ASC"}"""
            /*   """{"where": { "eta": {
           "between": ["${LocalDate.now().atTime(LocalTime.MIN)}",
           "${
                   LocalDate.now().atTime(LocalTime.MAX)
               }"]}},"order": "eta ASC"}"""*/

        ).enqueue(object : SalixCallback<List<RoadStopMapTruck>>(context) {
            override fun onSuccess(response: Response<List<RoadStopMapTruck>>) {
                _stopMapTruckList.value =
                    response.body()?.let { RoadStopMapTruckList(it) }
            }
        })
    }

    fun getStopMapTruckPalletList(vTruckFk: Int) {
        salix.getStopMapTruckPalletList(
            arrayListOf(vTruckFk)
        ).enqueue(object : SalixCallback<List<StopMapTruckPallet>>(context) {
            override fun onSuccess(response: Response<List<StopMapTruckPallet>>) {
                _stopMapTruckPalletList.value =
                    response.body()?.let { StopMapTruckPalletList(it) }
            }
        })
    }

    fun getPalletMatchState(truckFk: Int, state: String) {
        salix.getPalletMatchState(
            roadMapStopFk = truckFk,
            state = state
        ).enqueue(object : SalixCallback<List<StopMapTruckPallet>>(context) {
            override fun onSuccess(response: Response<List<StopMapTruckPallet>>) {
                _stopMapTruckPalletList.value =
                    response.body()?.let { StopMapTruckPalletList(it) }
            }
        })
    }

    fun expeditionStateAddByPallet(
        vPalletFk: Int,
        vStateCode: String,
    ) {
        salix.expeditionStateAddByPallet(
            arrayListOf(vPalletFk, vStateCode).formatWithQuotes()
        )
            .enqueue(object : SalixCallback<Unit>(context) {
                override fun onSuccess(response: Response<Unit>) {
                    _responseState.value = response.isSuccessful
                }

            })
    }

}
