package com.cheng.blzb.ui.fragment.mine

import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.BidDetailEntity
import com.cheng.blzb.bean.BidItemEntity
import com.cheng.blzb.bean.CompanyEntity
import com.cheng.blzb.bean.OrderEntity
import com.cheng.blzb.bean.SubscriptionEntity
import com.cheng.blzb.bean.SubscriptionMessageEntity
import com.cheng.blzb.bean.UserEntity
import com.cheng.blzb.bean.WxServiceEntity
import com.cheng.blzb.common.Constants
import com.cheng.blzb.net.ApiFactory
import com.example.base.extensions.toast
import com.example.base.utils.L
import com.example.base.utils.Utils
import com.example.base.viewmodel.BaseViewModel
import com.tencent.mm.opensdk.constants.Build
import com.tencent.mm.opensdk.modelbiz.WXOpenCustomerServiceChat
import com.tencent.mm.opensdk.openapi.WXAPIFactory

class MineViewModel : BaseViewModel() {
    val userInfoLiveData = MutableLiveData<UserEntity>()
    val companyLiveData = MutableLiveData<CompanyEntity>()
    val subscribeLiveData = MutableLiveData<List<SubscriptionEntity>>()
    val favoriteLiveData = MutableLiveData<List<BidItemEntity>>()
    val historyLiveData = MutableLiveData<List<BidDetailEntity>>()
    val orderLiveData = MutableLiveData<List<OrderEntity>>()

    fun userInfo(showLoading: Boolean = false) {
        if (showLoading) showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.userInfo()
            if (response.status) {
                userInfoLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun getCompanyInfo() {
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getCompanyInfo()
            if (response.status) {
                companyLiveData.postValue(response.data)
            } else toast(response.message, true)
        }, {
            setError(it)
            L.d(it)
        })
    }

    fun getSubscriptionList() {
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getSubscriptionList()
            if (response.status) {
                subscribeLiveData.postValue(response.data.items)
            }
        }, {
            L.d(it)
        })
    }

    fun getFavoriteList() {
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getFavoriteList()
            if (response.status) {
                favoriteLiveData.postValue(response.data.items)
            }
        }, {
            L.d(it)
        })
    }

    fun getHistoryList() {
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getViewHistoryList()
            if (response.status) {
                historyLiveData.postValue(response.data.items)
            }
        }, {
            L.d(it)
        })
    }

    fun getOrderList(status: String) {
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getOrderList(status)
            if (response.status) {
                orderLiveData.postValue(response.data)
            }
        }, {
            L.d(it)
        })
    }

    fun wxService() {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.wxService()
            if (response.status) {
                contactClientService(response.data)
            } else {
                toast(response.message, true)
            }
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    private fun contactClientService(data: WxServiceEntity) {
        val api = WXAPIFactory.createWXAPI(Utils.getApp(), Constants.WechatAppId)
        if (!api!!.isWXAppInstalled) {
            toast("未安装微信客户端")
            return
        }
        if (api.wxAppSupportAPI >= Build.SUPPORT_OPEN_CUSTOMER_SERVICE_CHAT) {
            val req = WXOpenCustomerServiceChat.Req()
            req.corpId = data.corpid
            req.url = data.address
            api.sendReq(req)
        }
    }
}