package com.adealink.weparty.order.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.frame.network.data.PageReq
import com.adealink.weparty.App
import com.adealink.weparty.order.data.CancelOrderReq
import com.adealink.weparty.order.data.CompleteOrderReq
import com.adealink.weparty.order.data.EvaluateOrderReq
import com.adealink.weparty.order.data.UserOrderData
import com.adealink.weparty.order.data.UserOrderListItemData
import com.adealink.weparty.order.data.UserOrderListReq
import com.adealink.weparty.order.datasource.remote.OrderHttpService
import com.adealink.weparty.util.PageHandler
import kotlinx.coroutines.launch

class UserOrderViewModel : BaseViewModel() {

    private val orderHttpService by lazy {
        App.instance.networkService.getHttpService(OrderHttpService::class.java)
    }
    private val orderList = mutableListOf<UserOrderListItemData>()
    private val orderIds = mutableSetOf<String>()
    private val pageHandler = PageHandler()

    val orderListRltLD = ExtMutableLiveData<Rlt<Any>>()
    val orderListLD = MutableLiveData<List<UserOrderListItemData>>()
    fun pullOrderList() {
        orderList.clear()
        orderIds.clear()
        pageHandler.reset()
        loadMoreOrderList()
    }

    fun loadMoreOrderList() {
        viewModelScope.launch {
            val rlt = orderHttpService.pullUserOrderList(
                UserOrderListReq(
                    page = PageReq(size = pageHandler.pageSize, next = pageHandler.currentPage)
                )
            )
            when (rlt) {
                is Rlt.Failed -> {
                    orderListRltLD.send(rlt)
                }

                is Rlt.Success -> {
                    pageHandler.nextPage(rlt.data.data?.next)
                    rlt.data.data?.list?.forEach {
                        if (!orderIds.contains(it.orderId)) {
                            orderList.add(UserOrderListItemData(it))
                            orderIds.add(it.orderId)
                        }
                    }
                    orderListRltLD.send(rlt)
                    orderListLD.send(orderList)
                }
            }
        }
    }

    fun cancelOrder(item: UserOrderData): LiveData<Rlt<Any>> {
        val livedata = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            livedata.send(orderHttpService.userCancelOrder(CancelOrderReq(item.orderId)))
        }
        return livedata
    }

    fun completeOrder(item: UserOrderData): LiveData<Rlt<Any>> {
        val livedata = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            livedata.send(orderHttpService.userCompleteOrder(CompleteOrderReq(item.orderId)))
        }
        return livedata
    }

    fun evaluateOrder(
        item: UserOrderData,
        star: Int,
        comment: String
    ): LiveData<Rlt<Any>> {
        val livedata = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            livedata.send(
                orderHttpService.userEvaluate(
                    EvaluateOrderReq(
                        item.orderId,
                        star,
                        comment
                    )
                )
            )
        }
        return livedata
    }

}