package com.zj365.health.vm

import androidx.lifecycle.MutableLiveData
import com.xty.base.vm.BaseVm
import com.xty.common.Const
import com.xty.network.model.MusicBean
import com.xty.network.model.ProposalBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import org.json.JSONObject
class HealthProposalVm : BaseVm() {

    val proposalLive by lazy{MutableLiveData<MutableList<MutableList<ProposalBean>>>()}
    val musicLive by lazy{MutableLiveData<MutableList<MusicBean>>()}
    fun getAllData(userId:String,titleList: Array<String>) {
        startHttp {
            withContext(Dispatchers.IO) {
                val listTitle= mutableListOf<String>() // 标题
                val listTitlePotiosion= mutableListOf<Int>() // 标题位置
                var list= mutableListOf<MutableList<ProposalBean>>()
                var data1 = async { getData(1,userId) }
                var value1=data1.await()
                if(value1.isNotEmpty()){
                    list.add(value1)
                }else{
                    list.add(mutableListOf())
                }
                listTitle.add(titleList[0])
                listTitlePotiosion.add(0)

                var data2 = async { getData(2,userId) }
                var value2=data2.await()
                if(value2.isNotEmpty()){
                    list.add(value2)
                }else{
                    list.add(mutableListOf())
                }
                listTitle.add(titleList[1])
                listTitlePotiosion.add(1)
                var data3 = async { getData(3,userId) }
                var value3=data3.await()
                if(value3.isNotEmpty()){
                    list.add(value3)
                }else{
                    list.add(mutableListOf())
                }
                listTitle.add(titleList[2])
                listTitlePotiosion.add(2)

                var data4 = async { getData(4,userId) }
                var value4=data4.await()
                if(value4.isNotEmpty()){
                    list.add(value4)
                }else{
                    list.add(mutableListOf())
                }
                listTitle.add(titleList[3])
                listTitlePotiosion.add(3)


                var data5 = async { getData(5,userId) }
                var music = async { getMusical() } //返回的是一个list数组
                var value5=data5.await()
                if(value5.isNotEmpty()){
                    musicLive.postValue(music.await())
                    list.add(value5)
                }else{
                    list.add(mutableListOf())
                }
                listTitle.add(titleList[4])
                listTitlePotiosion.add(4)

                var data6 = async { getData(6,userId) }
                var value6=data6.await()
                if(value6.isNotEmpty()){
                    list.add(value6)
                }else{
                    list.add(mutableListOf())
                }
                listTitle.add(titleList[5])
                listTitlePotiosion.add(5)
                proposalLive.postValue(list)
            }

        }
    }

    private suspend fun getData(position: Int,userId:String): MutableList<ProposalBean> {
        var json=JSONObject()
        json.put("type",position)
        json.put("id",userId)
        var response = apiInterface().getHealthAgree(retrofits.getRequestBody(json.toString()))
        return response.data
    }

    private suspend fun getMusical():MutableList<MusicBean>{
        var response=  apiInterface().getMusicList()
        if( Const.nowMusicBean!=null){
            response.data.forEach {
                if(it.fileId==Const.nowMusicBean!!.fileId){
                    it.isPlayer=Const.nowMusicBean!!.isPlayer
                    return@forEach
                }
            }
        }

        return  response.data
    }



}