package com.zj365.dc.vm

import androidx.lifecycle.MutableLiveData
import com.xty.base.vm.BaseVm
import com.xty.common.Const
import com.xty.common.MMkvHelper
import com.xty.network.model.BannerBean
import com.xty.network.model.FamilyBean
import com.xty.network.model.HomeLayoutBean
import com.xty.network.model.RespBody
import com.xty.network.model.ScienceBean
import com.xty.network.model.VideoDoctorBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import org.json.JSONObject

class HomeVm: BaseVm() {

    val bannerLive by lazy { MutableLiveData<MutableMap<Int,MutableList<BannerBean>>>()  }
    val familyLive by lazy { MutableLiveData<RespBody<MutableList<FamilyBean>>>() }

    val scinceLive by lazy{MutableLiveData<RespBody<MutableList<ScienceBean>>>()}

    val layoutLive by lazy { MutableLiveData<RespBody<HomeLayoutBean>>() }

    val videoDoctorLive by lazy { MutableLiveData<RespBody<VideoDoctorBean>>() }


    fun getBanners(id: String?,location:Int){
        startHttp(false) {

            withContext(Dispatchers.IO){
                val userId = if (MMkvHelper.getLong(Const.USER_ID).toString() == "0") {
                    ""
                } else {
                    MMkvHelper.getLong(Const.USER_ID).toString()
                }
                var nowLine = async{apiInterface().getBanners(userId,location)}
                var showInfo = nowLine.await()
               // var value=apiInterface().getBanners(retrofits.getRequestBody(json.toString()))
                var dataMap = setMapValue(bannerLive.value,showInfo.data,location)
                if (dataMap != null){
                    bannerLive.postValue(dataMap)
                }
               // value.getCodeStatus(bannerLive,nowData)
            }

        }
    }

    fun <T> setMapValue(map: MutableMap<Int, MutableList<T>>?, list: MutableList<T>?, type: Int): MutableMap<Int, MutableList<T>>? {
        if (list == null) {
            return null
        } else {  //当得到的值不是空的时候
            var mapNew = mutableMapOf<Int, MutableList<T>>()
            if (map == null) {
                mapNew[type] = list
                return mapNew
            } else {
                map[type] = list
                return map
            }
        }
    }

    fun getMyFamilyList(){
        startHttp (false){
            var json= JSONObject()

            var request=  apiInterface().getMyFamily(retrofits.getRequestBody(json.toString()))
            request.getCodeStatus(familyLive,nowData)
        }
    }

    fun getScinceList(){
        startHttp {
            var body= apiInterface().getScinceList()
            body.getCodeStatus(scinceLive,nowData)
        }
    }

    fun getHomeLayout(){
        startHttp {
            var body= apiInterface().getHomeLayout()
            body.getCodeStatus(layoutLive,nowData)
        }
    }

    fun getVideoDoctorType(){
        startHttp {
            var body= apiInterface().getVideoDoctorType()
            body.getCodeStatus(videoDoctorLive,nowData)
        }
    }

}