package com.swago.room.gift

import android.app.Application
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.gift.GiftAllModel
import com.swago.baseswago.model.live.gift.GiftSendModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.BaseViewModel

/**
 *@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?>()
    }

    fun sendGift(
        room_id: String,
        gift_id: String,
        gift_batch: Int,
        gift_receive_id: String,
        isCombo:Int = 0,
        gift_num: Int = 1,
    ) {
        requestData2 {
            requestData {
                val data = ApiManager.roomApi.sendGift(room_id, gift_id, gift_batch, gift_receive_id, isCombo,gift_num)
                sendResultLiveData.value = data
            }

            requestError {
                if (it is SwagoException){
                    Toast.makeText(AppContext.getContext(), it.message, Toast.LENGTH_SHORT).show()
                    if (it.code == 517){
                        sendResultLiveData.value = null
                    }
                }
            }
        }
    }

}