package com.swago.room.vm

import android.app.Application
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import com.google.protobuf.Api
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.http.SwagoException
import com.swago.baseswago.inter.ApiManager
import com.swago.baseswago.model.RedEnvelope
import com.swago.baseswago.model.live.ErrorModel
import com.swago.baseswago.model.live.RankListModel
import com.swago.baseswago.model.live.RedReceiveModel
import com.swago.baseswago.model.live.ShareConfig
import com.swago.baseswago.model.live.game.GameListModel
import com.swago.baseswago.util.*

/**
 *@date 2021/12/1 20:24
 *description:
 */
class RoomOtherVm (application: Application) : BaseViewModel(application){

    val rankListLiveData by lazy {
        MutableLiveData<List<RankListModel.ListBean>?>()
    }

    val totalLiveData by lazy {
        MutableLiveData<String>()
    }


    fun getRankList(roomId:String,rankType:Int,page:Int){
        requestData2 {
            requestData {
                val data = ApiManager.roomApi.getRankList(roomId,rankType,page)
                rankListLiveData.value = data.list
                totalLiveData.value = data.total
            }

            requestError {
                Toast.makeText(AppContext.getContext(), it.message, Toast.LENGTH_SHORT).show()
                rankListLiveData.value = null
            }
        }
    }


    val shareConfigLiveData by lazy {
        MutableLiveData<ShareConfig>()
    }
    /**
     * 获取分享链接
     */
    fun getShareConfig(){
        requestData {
            val data = ApiManager.roomApi.getShareConfig()
            shareConfigLiveData.value = data
        }
    }

    /**
     *红包列表
     */
    val redEnvelopeListLiveData by lazy {
        MutableLiveData<ArrayList<RedEnvelope>>()
    }
    fun getRedEnvelopeList(){
        LogUtil.d("加入房间","红包")
        requestData {
            SwagoRoomManager.iRoomInfo?.let {
                redEnvelopeListLiveData.value = ApiManager.roomApi.getRedEnvelopeList(it.getRoomId())
            }
        }
    }

    val sendSuccessLiveData by lazy {
        MutableLiveData<Int>()
    }

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

    /**
     * 发红包
     */
    fun sendRedEnvelope(coins:String,count:Int){
        requestData2(false) {
            requestData {
                SwagoRoomManager.iRoomInfo?.let {
                    ApiManager.roomApi.sendRedEnvelope(it.getRoomId(),coins,count)
                    sendSuccessLiveData.value = 200
                    SwagoLoading.cancelLoadingDialog()
                }
            }

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

    /**
     * 抢红包
     */
    val receiveRedEnvelopeResultLiveData by lazy {
        MutableLiveData<String>()
    }
    val receiveErrorResultLiveData by lazy {
        MutableLiveData<Int>()
    }
    fun getRedEnvelopeCoin(redEnvelopeId:String){
        requestData2 {
            requestData {
                SwagoRoomManager.iRoomInfo?.let {
                    val data  = ApiManager.roomApi.getRedEnvelopeCoin(it.getRoomId(),redEnvelopeId)
                    receiveRedEnvelopeResultLiveData.value = data.coin
                    UserInfo.getUserInfo()?.user_coins = data.user_coin
                }
            }

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


    /**
     * 公告变更
     */
    val noticeUpdateLiveData by lazy {
        MutableLiveData<Any>()
    }

    fun updateAudioNotice(content:String){
        requestData2 {
           requestData {
               SwagoRoomManager.iRoomInfo?.let {
                   ApiManager.roomApi.updateAudioNotice(it.getRoomId(),content)
                   noticeUpdateLiveData.value = null
               }
           }
            requestError {
                SwagoLoading.cancelLoadingDialog()
            }
        }
    }


    /**
     * 发送弹幕
     */
    val sendDanMuResultLiveData by lazy {
        MutableLiveData<Int>()
    }
    var sendDanMuNum = 0

    fun sendDanMu(message:String){
        requestData {
            SwagoRoomManager.iRoomInfo?.let {
                ApiManager.roomApi.sendDanMu(it.getRoomId(),message)
                sendDanMuNum ++
                sendDanMuResultLiveData.value = sendDanMuNum
            }
        }
    }

    /**
     * 红包领取详情
     */
    val redListLiveData by lazy {
        MutableLiveData<List<RedReceiveModel>?>()
    }

    fun getRedReceivedList(package_id:String){
        requestData2 {
            requestData {
                val data =  ApiManager.roomApi.getRedReceivedList(package_id)
                redListLiveData.value = data.list
            }
            requestError {
                redListLiveData.value = null
            }
        }
    }
}