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.module.order.OrderModule
import com.adealink.weparty.module.order.data.OrderAction
import com.adealink.weparty.module.order.data.OrderDetailData
import com.adealink.weparty.module.order.data.OrderDetailInfo
import com.adealink.weparty.module.order.data.OrderDetailReq
import com.adealink.weparty.module.order.data.OrderStatus.Companion.getNextStatus
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.UserOrderListItemData
import com.adealink.weparty.order.data.UserOrderListReq
import com.adealink.weparty.order.datasource.remote.OrderHttpService
import com.adealink.weparty.util.PageHandler
import com.adealink.weparty.util.formatStar
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 hasMoreData(): Boolean {
        return !pageHandler.isEnd
    }

    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)
                }
            }
        }
    }


    val orderDetailLD = ExtMutableLiveData<OrderDetailData>()

    fun cancelOrder(data: OrderDetailInfo): LiveData<Rlt<OrderDetailInfo>> {
        val livedata = OnceMutableLiveData<Rlt<OrderDetailInfo>>()
        viewModelScope.launch {
            val rlt = orderHttpService.userCancelOrder(CancelOrderReq(data.orderId))
            when (rlt) {
                is Rlt.Failed -> {
                    livedata.send(rlt)
                }

                is Rlt.Success -> {
                    OrderModule.queryDiscountInfo()
                    val nextData = queryOrderDetail(data.orderId)
                    if (nextData != null) {
                        orderDetailLD.send(nextData)
                        livedata.send(Rlt.Success(nextData.order))
                        return@launch
                    }
                    //兜底展示下一个状态
                    val nextStatus = nextData?.order?.status ?: let {
                        getNextStatus(data.status, OrderAction.CANCEL) ?: data.status
                    }
                    data.status = nextStatus
                    livedata.send(Rlt.Success(data))
                }
            }
        }
        return livedata
    }

    fun completeOrder(data: OrderDetailInfo): LiveData<Rlt<OrderDetailInfo>> {
        val livedata = OnceMutableLiveData<Rlt<OrderDetailInfo>>()
        viewModelScope.launch {
            val rlt = orderHttpService.userCompleteOrder(CompleteOrderReq(data.orderId))
            when (rlt) {
                is Rlt.Failed -> {
                    livedata.send(rlt)
                }

                is Rlt.Success -> {
                    val nextData = queryOrderDetail(data.orderId)
                    if (nextData != null) {
                        orderDetailLD.send(nextData)
                        livedata.send(Rlt.Success(nextData.order))
                        return@launch
                    }
                    //兜底展示下一个状态
                    val nextStatus = nextData?.order?.status ?: let {
                        getNextStatus(data.status, OrderAction.COMPLETE) ?: data.status
                    }
                    data.status = nextStatus
                    livedata.send(Rlt.Success(data))
                }
            }
        }
        return livedata
    }

    fun evaluateOrder(
        orderId: String,
        star: Float,
        comment: String
    ): LiveData<Rlt<Any>> {
        val livedata = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val rlt = orderHttpService.userEvaluate(
                EvaluateOrderReq(
                    orderId,
                    formatStar(star, "1"),
                    comment
                )
            )
            livedata.send(rlt)
        }
        return livedata
    }

    private suspend fun queryOrderDetail(orderId: String): OrderDetailData? {
        val rlt = orderHttpService.orderDetail(OrderDetailReq(orderId))
        return when (rlt) {
            is Rlt.Failed -> {
                null
            }

            is Rlt.Success -> {
                rlt.data.data
            }
        }
    }

}