package com.cheng.blzb.ui.fragment.guide.vip

import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.GuideUpdateEntity
import com.cheng.blzb.bean.OrderEntity
import com.cheng.blzb.bean.OrderPayEntity
import com.cheng.blzb.bean.VipGoodsEntity
import com.cheng.blzb.net.ApiFactory
import com.example.base.extensions.toast
import com.example.base.utils.L
import com.example.base.viewmodel.BaseViewModel
import com.google.gson.JsonObject
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import okhttp3.RequestBody.Companion.toRequestBody

class GuideVipViewModel: BaseViewModel() {
    var queryOrderJob: Job? = null

    val goodsListLiveData = MutableLiveData<List<VipGoodsEntity>>()
    val createOrderLiveData = MutableLiveData<OrderPayEntity>()
    val orderInfoLiveData = MutableLiveData<OrderEntity>()
    val payTipsLiveData = MutableLiveData<List<String>>()
    val updateLiveData = MutableLiveData<List<GuideUpdateEntity>>()

    fun getGoodsList() {
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getGoodsList()
            if (response.status) {
                goodsListLiveData.postValue(response.data)
            } else toast(response.message, true)
        }, {
            setError(it)
            L.d(it)
        })
    }

    fun payCreateOrder(goodsId: String, payType: String) {
        showDialog()
        launchOnUiTryCatch({
            val json = JsonObject()
            json.addProperty("goods_id", goodsId)
            json.addProperty("pay_type", payType)
            json.addProperty("source", "bootpage")
            json.addProperty("pay_source", "app")

            val response = ApiFactory.apiService.payCreateOrder(json.toString().toRequestBody())
            if (response.status) {
                createOrderLiveData.postValue(response.data)
            } else {
                toast(response.message, true)
            }
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun getOrderInfo(orderId: String) {
        setMessage("支付结果查询中", true)
        cancelJob()
        queryOrderJob = launchOnUiTryCatch({
            repeat(10) {
                val response = ApiFactory.apiService.getOrderInfo(orderId)
                if (response.status) {
                    orderInfoLiveData.postValue(response.data)
                    if (response.data.status == "2") {
                        cancel()
                    }
                    delay(2000)
                } else {
                    toast(response.message, true)
                    cancel()
                }
            }
            dismissDialog()
        }, {
            dismissDialog()
            L.d(it)
        })
    }

    fun getPayUserTips() {
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getPayUserTips()
            if (response.status) {
                payTipsLiveData.postValue(response.data)
            }
        }, {
            setError(it)
            L.d(it)
        })
    }

    fun getUpdateNum() {
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getUpdateNum("7")
            if (response.status) {
                updateLiveData.postValue(response.data)
            } else toast(response.message, true)
        }, {
            setError(it)
            L.d(it)
        })
    }

    private fun cancelJob() {
        queryOrderJob?.cancel()
        queryOrderJob = null
    }

    override fun onCleared() {
        super.onCleared()
        cancelJob()
    }
}