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

import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.CorpEntity
import com.cheng.blzb.bean.CouponEntity
import com.cheng.blzb.bean.OrderEntity
import com.cheng.blzb.bean.OrderPayEntity
import com.cheng.blzb.bean.UserEntity
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 com.ylqh.cube.bean.CouponActivityEntity
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import okhttp3.RequestBody.Companion.toRequestBody

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

    val userInfoLiveData = MutableLiveData<UserEntity>()
    val goodsListLiveData = MutableLiveData<List<VipGoodsEntity>>()
    val corpInfoLiveData = MutableLiveData<CorpEntity>()
    val createOrderLiveData = MutableLiveData<OrderPayEntity>()
    val orderInfoLiveData = MutableLiveData<OrderEntity>()
    val payTipsLiveData = MutableLiveData<List<String>>()
    val couponListLiveData = MutableLiveData<List<CouponEntity>>()
    val couponActivityLiveData = MutableLiveData<List<CouponActivityEntity>>()
    val getCouponLiveData = MutableLiveData<Any>()

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

    fun getGoodsList(type: String, showLoading: Boolean = false) {
        if (showLoading) showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getGoodsList(type)
            if (response.status) {
                goodsListLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun getCorpInfo() {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getSeatInfo()
            if (response.status) {
                corpInfoLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun payCreateOrder(goos_id: String, pay_type: String, source: String, seatCount: String, coupon: String? = "") {
        showDialog()
        launchOnUiTryCatch({
            val json = JsonObject()
            json.addProperty("goods_id", goos_id)
            json.addProperty("pay_type", pay_type)
            json.addProperty("source", source)
            json.addProperty("pay_source", "app")
            json.addProperty("coupon", coupon ?: "")

            val extra = JsonObject()
            extra.addProperty("add_corp_limit", seatCount)
            json.add("extra", extra)
            L.d("TAG-->>$json")
            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 couponList() {
        val params = HashMap<String, String>()
        params["page"] = "1"
        params["size"] = "50"
        params["status"] = "1"
        params["expire"] = "1"
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.couponList(params)
            if (response.status) {
                couponListLiveData.postValue(response.data.items)
            } else toast(response.message, true)
        },{
            setError(it)
            L.d(it)
        })
    }

    fun couponActivityList() {
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.couponActivityList("cli_pay_return")
            if (response.status) {
                couponActivityLiveData.postValue(response.data)
            }
        },{
            L.d(it)
        })
    }

    fun getActivityCoupon(ids: String) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getActivityCoupons(ids)
            if (response.status) {
                getCouponLiveData.postValue(Any())
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

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

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