package com.adealink.weparty.order.viewmodel

import androidx.lifecycle.LiveData
import com.adealink.frame.base.CommonDataNullError
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.ExtLiveData
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.CreateOrderRes
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.QueryUnCompleteOrderReq
import com.adealink.weparty.module.order.viewmodel.IOrderViewModel
import com.adealink.weparty.order.data.CreateOrderReq
import com.adealink.weparty.order.datasource.remote.OrderHttpService
import com.adealink.weparty.order.manager.discountManager
import kotlinx.coroutines.launch

class OrderViewModel : BaseViewModel(), IOrderViewModel {

    private val orderHttpService by lazy {
        App.instance.networkService.getHttpService(OrderHttpService::class.java)
    }

    override suspend fun queryUnCompleteOrder(uid: String): List<OrderDetailInfo> {
        val rlt = orderHttpService.queryUnCompleteOrder(
            QueryUnCompleteOrderReq(
                uid,
                PageReq(size = 20, null)
            )
        )
        return when (rlt) {
            is Rlt.Failed -> {
                emptyList()
            }

            is Rlt.Success -> {
                rlt.data.data?.list ?: emptyList()
            }
        }
    }

    override val onOrderCreatedLD: ExtLiveData<CreateOrderRes> = ExtMutableLiveData()

    override fun createOrder(
        playmateId: String,
        count: Int,
        remark: String?,
        source: Int
    ): LiveData<Rlt<CreateOrderRes>> {
        val liveData = OnceMutableLiveData<Rlt<CreateOrderRes>>()
        viewModelScope.launch {
            val rlt = orderHttpService.createOrder(
                CreateOrderReq(
                    playmateId, count, remark, source
                )
            )
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

                is Rlt.Success -> {
                    val data = rlt.data.data
                    discountManager.queryDiscountInfo()
                    if (data == null) {
                        liveData.send(Rlt.Failed(CommonDataNullError()))
                    } else {
                        onOrderCreatedLD.send(data)
                        liveData.send(Rlt.Success(data))
                    }
                }
            }
        }
        return liveData
    }

    override fun refuse(order: OrderDetailInfo): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            liveData.send(
                orderHttpService.playmateOrderOp(
                    OrderPlaymateOpReq(
                        order.orderId,
                        OrderPlaymateOpReq.OP_REFUSE
                    )
                )
            )
        }
        return liveData
    }

    override fun accept(order: OrderDetailInfo): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            liveData.send(
                orderHttpService.playmateOrderOp(
                    OrderPlaymateOpReq(
                        order.orderId,
                        OrderPlaymateOpReq.OP_ACCEPT
                    )
                )
            )
        }
        return liveData
    }

    override fun startService(order: OrderDetailInfo): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            liveData.send(
                orderHttpService.playmateOrderOp(
                    OrderPlaymateOpReq(
                        order.orderId,
                        OrderPlaymateOpReq.OP_START_SERVICE
                    )
                )
            )
        }
        return liveData
    }

    override fun completeService(order: OrderDetailInfo): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            liveData.send(
                orderHttpService.playmateOrderOp(
                    OrderPlaymateOpReq(
                        order.orderId,
                        OrderPlaymateOpReq.OP_END_SERVICE
                    )
                )
            )
        }
        return liveData
    }

    fun loadOrderDetail(orderId: String): LiveData<Rlt<OrderDetailData>> {
        val liveData = OnceMutableLiveData<Rlt<OrderDetailData>>()
        viewModelScope.launch {
            val rlt = orderHttpService.orderDetail(OrderDetailReq(orderId))
            when (rlt) {
                is Rlt.Failed -> {
                    liveData.send(rlt)
                }

                is Rlt.Success -> {
                    val data = rlt.data.data
                    if (data == null) {
                        liveData.send(Rlt.Failed(CommonDataNullError()))
                    } else {
                        liveData.send(Rlt.Success(data))
                    }
                }
            }
        }
        return liveData
    }
}