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.baseroom.SwagoRoomManager
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.audio.AudioSendGiftModel
import com.swago.baseswago.model.live.audio.MaiUserInfo
import com.swago.baseswago.model.live.audio.MaiUserModel
import com.swago.baseswago.model.live.gift.GiftAllModel
import com.swago.baseswago.model.live.gift.GiftSendModel
import com.swago.baseswago.util.*
import com.swago.room.pk.PKStateManager
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONArray
import org.json.JSONObject
import retrofit2.http.Field

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

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

    /**
     * 获取礼物列表
     */
    fun getGiftList() {
        requestData {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                if (iRoomInfo.getRoomType() == 1){
                    val data = ApiManager.roomApi.getGiftList()
                    allGiftLiveData.value = data
                }else if (iRoomInfo.getRoomType() == 2){
                    val data = ApiManager.roomApi.getAudioGiftList()
                    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
                }
            }
        }
    }


    val sendResultAudioLiveData by lazy {
        MutableLiveData<AudioSendGiftModel?>()
    }

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

    /**
     * 语音房送礼
     */
    fun sendAudioGift(
        room_id: String,
        gift_id: String,
        gift_batch: Int,
        gift_receive_id:List<String>,
        gift_num: Int = 1,
    ) {
        requestData2(false) {
            requestData {
                val jsonArray = JSONArray()
                gift_receive_id.forEach {
                    jsonArray.put(it)
                }
                val jsonObject = JSONObject()
                jsonObject.put("room_id", room_id)
                jsonObject.put("gift_id", gift_id)
                jsonObject.put("gift_batch", gift_batch)
                jsonObject.put("gift_num", gift_num)
                jsonObject.put("gift_receive_id", jsonArray)
                val data = ApiManager.roomApi.sendAudioGift(jsonObject.toString().toRequestBody("application/json; charset=UTF-8".toMediaType()))
                sendResultAudioLiveData.value = data
                UserInfo.getUserInfo()?.let {
                    if (data.userCoins.toLong() < it.user_coins.toLong()){
                        it.user_coins = data.userCoins
                    }
                }
            }

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

    /**
     * 获取麦位列表上的用户信息
     */
    val maiUserListLiveData by lazy {
        MutableLiveData<List<MaiUserInfo>>()
    }
    fun getMaiUserList(){
        requestData {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                val data = ApiManager.roomApi.getMaiUserList(iRoomInfo.getBroadcastId())
                maiUserListLiveData.value = data.list
            }
        }
    }

}