package cn.muchinfo.rma.view.base.home


import androidx.lifecycle.MutableLiveData
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.AccountData
import cn.muchinfo.rma.global.data.NoticeData
import cn.muchinfo.rma.global.data.database.BannerEntity
import cn.muchinfo.rma.global.database.DataBase
import cn.muchinfo.rma.netManage.base.ResponseCallback
import cn.muchinfo.rma.netManage.utils.MyOkHttpUtils
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.base.BaseViewModel
import cn.muchinfo.rma.view.base.app.BaseResult
import cn.muchinfo.rma.view.base.app.Constant
import com.blankj.utilcode.util.SPUtils
import mtp.polymer.com.autowidget.utils.TaskUiModel
import okhttp3.Call
import java.lang.Exception

class HomeViewModel : BaseViewModel(){

    /**
     * 加载状态控制
     */
    val loadingDialogStatus: MutableLiveData<TaskUiModel> = MutableLiveData()

    val bannerList : MutableLiveData<List<String>> = MutableLiveData()

    /**
     * 使用的当前的资金账户信息
     */
    val usedAccountData : MutableLiveData<AccountData> = MutableLiveData()

    /**
     * 公告信息
     */
    val noticeDataList : MutableLiveData<List<NoticeData>> = MutableLiveData()

    /**
     * 在请求之前先获取数据库存储的banner信息
     */
    fun getBanner(){
        val dao = DataBase.getInstance().bannerDao()
        dao.getBannerList().let {
            getBannerUrlList(it)
        }
        val params = mutableMapOf<String,String>()
        params.put("imageType","1")
        MyOkHttpUtils().query(URL = SPUtils.getInstance().getString(Constant.goCommonSearchUrl) + "/Common/QueryImageConfigs",
            callback = object : ResponseCallback<BaseResult<List<BannerEntity>>>() {
                override fun onResponse(response: BaseResult<List<BannerEntity>>?, id: Int) {
                    dao.insertBanner(banners = response?.data ?: arrayListOf())
                    getBannerUrlList(response?.data ?: arrayListOf())
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {

                }

            },params = params,type = "1")
    }

    /**
     * @param datalist List<BannerEntity>
     */
    fun getBannerUrlList(datalist : List<BannerEntity>){
        val bannerUrlList = arrayListOf<String>()
        datalist.forEach {
            bannerUrlList.add(SPUtils.getInstance().getString(Constant.openApiUrl) + it.imagepath.substring(1))
        }
        bannerList.postValue(bannerUrlList)
    }

    /**
     * 更新hoefragment的account
     */
    fun initAccountData(){
        val accountDataList = GlobalDataCollection.instance?.accountDataList
        if (accountDataList.isNullOrEmpty().not()){
            usedAccountData.postValue(accountDataList?.get(0))
        }
    }

    /**
     * 通知公告查询
     */
    fun queryNotice(){
        val params = mutableMapOf<String, String>().apply {
            put("loginID", GlobalDataCollection.instance?.loginRsp?.loginID.toString())
        }

        MyApplication.getInstance()?.commonManager?.queryNotice(params = params){ isSuccess, respData, error ->
            if (isSuccess){
                noticeDataList.postValue(respData)
            }
        }
    }

}