package com.swago.room.gift

import android.app.Application
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import com.swago.baseswago.http.SwagoException
import com.swago.baseswago.inter.ApiManager
import com.swago.baseswago.model.live.ErrorModel
import com.swago.baseswago.model.live.gift.GiftAllModel
import com.swago.baseswago.model.live.gift.GiftSendModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.BaseViewModel
import com.swago.baseswago.util.LogUtil
import com.swago.baseswago.util.UserInfo
import com.swago.room.pk.PKStateManager

/**
 *@date 2021/12/12 17:03
 *description:
 */
class GiftVm(application: Application) : BaseViewModel(application) {

    val allGiftLiveData by lazy {
        MutableLiveData<List<GiftAllModel>>()
    }

    /**
     * 获取礼物列表
     */
    fun getGiftList() {
        requestData {
            val data = ApiManager.roomApi.getGiftList()
            allGiftLiveData.value = data
        }
    }


    /**
     * 送礼
     */
    val sendResultLiveData by lazy {
        MutableLiveData<GiftSendModel?>()
    }

    val sendGiftCodeLiveData by lazy {
        MutableLiveData<ErrorModel>()
    }

    fun sendGift(
        room_id: String,
        gift_id: String,
        gift_batch: Int,
        gift_receive_id: String,
        isCombo:Int = 0,
        gift_num: Int = 1,
    ) {
        requestData2(false) {
            requestData {
                val data = ApiManager.roomApi.sendGift(room_id, gift_id, gift_batch, gift_receive_id, isCombo,gift_num,PKStateManager.pkId)
                LogUtil.d("送礼","接口调用成功--${System.currentTimeMillis()}")
                sendResultLiveData.value = data
                UserInfo.getUserInfo()?.let {
                    if (data.user_coins.toLong() < it.user_coins.toLong()){
                        it.user_coins = data.user_coins
                    }
                }
            }

            requestError {
                if (it is SwagoException){
                    val errorModel = ErrorModel(it.code,it.message?:"")
                    sendGiftCodeLiveData.value = errorModel
                }
            }
        }
    }

}