package com.zj365.health.vm

import androidx.lifecycle.MutableLiveData
import com.luck.picture.lib.entity.LocalMedia
import com.xty.base.vm.BaseVm
import com.xty.common.util.CommonToastUtils
import com.xty.network.model.RespBody
import okhttp3.MultipartBody
import org.json.JSONObject
import java.io.File

class DataUploadVm: BaseVm() {
    val uploadPhoto by lazy {MutableLiveData<RespBody<String>>()}
    val uploadDbp by lazy {MutableLiveData<RespBody<String>>()}
    val uploadClu by lazy {MutableLiveData<RespBody<String>>()}
    val uploadDrug by lazy {MutableLiveData<RespBody<String>>()}


    /**
     * 电子化验单上传
     */
    fun upload(data1:MutableList<LocalMedia>,data2:MutableList<LocalMedia>,data3:MutableList<LocalMedia>,data4:MutableList<LocalMedia>,data5:MutableList<LocalMedia>,data6:MutableList<LocalMedia>){

        var urinePart:   MultipartBody.Part?=MultipartBody.Part.createFormData("","")
        var kidneyPart:   MultipartBody.Part?=MultipartBody.Part.createFormData("","")
        var electrolytePart:   MultipartBody.Part?=MultipartBody.Part.createFormData("","")
        var cardiacPart:   MultipartBody.Part?=MultipartBody.Part.createFormData("","")
        var tumorPart:   MultipartBody.Part?=MultipartBody.Part.createFormData("","")

        var urineList= mutableListOf< MultipartBody.Part>().apply {  MultipartBody.Part.createFormData("","") }
        var kidneyList= mutableListOf< MultipartBody.Part>().apply {  MultipartBody.Part.createFormData("","") }
        var electrolyteList= mutableListOf< MultipartBody.Part>().apply {  MultipartBody.Part.createFormData("","") }
        var cardiacList= mutableListOf< MultipartBody.Part>().apply {  MultipartBody.Part.createFormData("","") }
        var tumorList= mutableListOf< MultipartBody.Part>().apply {  MultipartBody.Part.createFormData("","") }
        var otherList= mutableListOf< MultipartBody.Part>().apply {  MultipartBody.Part.createFormData("","") }
        data1.forEach {
            var file=File(it.compressPath)
            urineList.add(  MultipartBody.Part.createFormData("urine",file.name,retrofits.getMultiPartFileRb(file)))
        }
        data2.forEach {
            var file=File(it.compressPath)
            kidneyList.add(  MultipartBody.Part.createFormData("kidney",file.name,retrofits.getMultiPartFileRb(file)))
        }
        data3.forEach {
            var file=File(it.compressPath)
            electrolyteList.add(  MultipartBody.Part.createFormData("electrolyte",file.name,retrofits.getMultiPartFileRb(file)))
        }
        data4.forEach {
            var file=File(it.compressPath)
            cardiacList.add(  MultipartBody.Part.createFormData("cardiac",file.name,retrofits.getMultiPartFileRb(file)))
        }
        data5.forEach {
            var file=File(it.compressPath)
            tumorList.add(  MultipartBody.Part.createFormData("tumor",file.name,retrofits.getMultiPartFileRb(file)))
        }
        data6.forEach {
            var file=File(it.compressPath)
            tumorList.add(  MultipartBody.Part.createFormData("other",file.name,retrofits.getMultiPartFileRb(file)))
        }


        startHttp {
           var body= apiInterface().uploadAssay(urineList,kidneyList,electrolyteList,cardiacList,tumorList,otherList)
            body.getCodeStatus(uploadPhoto,nowData)
        }

    }

    /**
     * 血压上传
     */
    fun uploadDbp(dbp:String,sbp:String, measuringTime:String,medic:String){
        if(dbp.isEmpty()){
            CommonToastUtils.showToast("请输入舒张压")
            return
        }
        if(sbp.isEmpty()){
            CommonToastUtils.showToast("请输入收缩压")
            return
        }
        if(measuringTime.isEmpty()){
            CommonToastUtils.showToast("请选择时间")
            return
        }
        var json= JSONObject()
        json.put("dbp", dbp)
        json.put("sbp", sbp)
        json.put("measuringTime" , measuringTime)
        json.put("type" , medic)
        startHttp {
            var body=apiInterface().uploadDbpInfo(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(uploadDbp,nowData)
        }
    }

    /**
     * 血糖上传
     */
    fun uploadCluInfo(measuringNum:String,measuringTime:String,type:String,remark:String){
        var json=JSONObject()
        if(measuringNum.isEmpty()){
            CommonToastUtils.showToast("请输入血糖测量值")
            return
        }
        if(measuringTime.isEmpty()){
            CommonToastUtils.showToast("请选择时间")
            return
        }
        json.put("measuringNum", measuringNum)
        json.put("measuringTime" , measuringTime)
        json.put("type" , type)
        json.put("remark" , remark)
        startHttp {
            var body=apiInterface().uploadCluInfo(retrofits.getRequestBody(json.toString()))
            body.getCodeStatus(uploadClu,nowData)

        }
    }
    /**
     * 用药上传
     */
    fun uploadDrugInfo(  list:MutableList<LocalMedia>){

      /*  if(metering.isEmpty()){
            ToastUtils.show("请输入用量")
            return
        }
        if(takeTime.isEmpty()){
            ToastUtils.show("请选择时间")
            return
        }*/
        if(list.isEmpty()){
            CommonToastUtils.showToast("请拍摄照片")
            return
        }

        var listPart= mutableListOf<MultipartBody.Part>()
        list.forEach {
            var file=File(it.compressPath)
            var part=MultipartBody.Part.createFormData("file",file.name,retrofits.getMultiPartFileRb(file))
            listPart.add(part)
        }

/*        var json=JSONObject()
        json.put("metering",metering)
        json.put("takeTime",takeTime)
        json.put("type",type)
        json.put("remark",remark)*/
    /*   var  metering=retrofits.getRequestBody(metering)
       var  takeTime=retrofits.getRequestBody(takeTime)
       var  type=retrofits.getRequestBody(type)
       var  remark=retrofits.getRequestBody(remark)*/
        startHttp {
            var body=apiInterface().uploadDrugInfo(listPart)
            body.getCodeStatus(uploadDrug,nowData)
        }
    }
}