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.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.OrderPlaymateOpReq
import com.adealink.weparty.module.order.data.OrderStatus.Companion.getNextStatus
import com.adealink.weparty.order.data.PlaymateOrderListItemData
import com.adealink.weparty.order.data.PlaymateOrderListReq
import com.adealink.weparty.order.datasource.remote.OrderHttpService
import com.adealink.weparty.util.PageHandler
import kotlinx.coroutines.launch

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

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

    fun hasMoreData(): Boolean {
        return !pageHandler.isEnd
    }

    fun loadMoreOrderList() {
        viewModelScope.launch {
            val rlt = orderHttpService.pullPlaymateOrderList(
                PlaymateOrderListReq(
                    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(PlaymateOrderListItemData(it))
                            orderIds.add(it.orderId)
                        }
                    }
                    orderListRltLD.send(rlt)
                    orderListLD.send(orderList)
                }
            }
        }
    }


    fun accept(data: OrderDetailInfo): LiveData<Rlt<OrderDetailInfo>> {
        val liveData = OnceMutableLiveData<Rlt<OrderDetailInfo>>()
        viewModelScope.launch {
            val rlt = orderHttpService.playmateOrderOp(
                OrderPlaymateOpReq(
                    data.orderId,
                    OrderPlaymateOpReq.OP_ACCEPT
                )
            )
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

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

    fun refuse(data: OrderDetailInfo): LiveData<Rlt<OrderDetailInfo>> {
        val liveData = OnceMutableLiveData<Rlt<OrderDetailInfo>>()
        viewModelScope.launch {
            val rlt = orderHttpService.playmateOrderOp(
                OrderPlaymateOpReq(
                    data.orderId,
                    OrderPlaymateOpReq.OP_REFUSE
                )
            )
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

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

    fun startService(data: OrderDetailInfo): LiveData<Rlt<OrderDetailInfo>> {
        val liveData = OnceMutableLiveData<Rlt<OrderDetailInfo>>()
        viewModelScope.launch {
            val rlt = orderHttpService.playmateOrderOp(
                OrderPlaymateOpReq(
                    data.orderId,
                    OrderPlaymateOpReq.OP_START_SERVICE
                )
            )
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

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

    fun complete(data: OrderDetailInfo): LiveData<Rlt<OrderDetailInfo>> {
        val liveData = OnceMutableLiveData<Rlt<OrderDetailInfo>>()
        viewModelScope.launch {
            val rlt = orderHttpService.playmateOrderOp(
                OrderPlaymateOpReq(
                    data.orderId,
                    OrderPlaymateOpReq.OP_END_SERVICE
                )
            )
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

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

}