package com.abbidot.tracker.deprecated.ui.activity.vm

import android.content.Intent
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.AlipayOrderBean
import com.abbidot.tracker.bean.SetMealBean
import com.abbidot.tracker.bean.WXPayOrderBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.deprecated.ui.activity.profile.recharge.PaymentResultActivity
import com.abbidot.tracker.retrofit2.NetworkApi
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2022/7/29/029.
 * @link
 * @description:
 */
class PayCNViewModel : ViewModel() {

    val mGetWxPayOrderLiveData = MutableLiveData<Result<WXPayOrderBean>>()
    val mGetAlipayOrderLiveData = MutableLiveData<Result<AlipayOrderBean>>()

    /**
     * 获取微信支付Prepay_id
     */
    fun getWxPayOrder(activity: BaseActivity<*>, setMealBean: SetMealBean, deviceId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val userName = MMKVUtil.getString(MMKVKey.UserName)
            val result = NetworkApi.getWxPayOrder(setMealBean, deviceId, userId, userName)
            mGetWxPayOrderLiveData.value = result
        }
    }

    /**
     * 获取支付宝支付订单
     */
    fun getAlipayOrder(activity: BaseActivity<*>, setMealBean: SetMealBean, deviceId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val userName = MMKVUtil.getString(MMKVKey.UserName)
            val result = NetworkApi.getAlipayOrder(setMealBean, deviceId, userId, userName)
            mGetAlipayOrderLiveData.value = result
        }
    }

    /**
     * 支付宝支付回调
     */
    fun alipayResultCallback(activity: BaseActivity<*>, resultStatus: String?, price: Double) {
        //返回参数说明:https://opendocs.alipay.com/open/204/105302
        when (resultStatus) {
            "9000" -> {
                activity.showToast(R.string.txt_pay_success)
                //支付成功
                LogUtil.e("WXPayEntryActivity", "支付成功")
                val intent = Intent(activity, PaymentResultActivity::class.java)
                intent.putExtra(ConstantString.Amount, price)
                intent.putExtra(ConstantString.Type, ConstantInt.Type1)
                activity.startActivity(intent)
            }
            //"8000"代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认,最终交易是否成功以服务端异步通知为准
            "8000" -> activity.showToast("支付结果确认中")
            "6001" -> activity.showToast(R.string.txt_cancel_pay)
            "6002" -> activity.showToast("网络异常")
            "5000" -> activity.showToast("重复请求")
            else -> activity.showToast(R.string.txt_pay_fail)
        }
    }
}