package com.zj365.mime.vm

import androidx.lifecycle.MutableLiveData
import com.xty.base.vm.BaseVm
import com.xty.common.TimeUtils
import com.xty.common.util.CommonToastUtils
import com.xty.network.model.ImageUploadBean
import com.xty.network.model.NotAccepted
import com.xty.network.model.RespBody
import com.xty.network.model.SettingBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import okhttp3.MultipartBody
import org.json.JSONArray
import org.json.JSONObject
import java.io.File

class BaseInfoVm : BaseVm() {
    val setBasic by lazy { MutableLiveData<NotAccepted>() }
    val allData by lazy { MutableLiveData<RespBody<SettingBean>>() }

    /**
     * 获取所有信息
     */
    fun getAllInfo() {
        startHttp {
            var body = apiInterface().getMySetting()
            body.getCodeStatus(allData, nowData)
        }
    }

    fun submit(
        path: String,
        name: String,
        sex: String,
        height: String,
        weight: String,
        area: String,
        urgent: String,
        mPhone: String,
        urgentPhone: String,
        urgentRelation: String,
        province: String,
        city: String,
        district: String,
        lat: String,
        lng: String,
        medicNames: String,
        medicIds: String,
        tumour: String,
        other: String,
        diseaseList: String,
        birthDay: String,
        birthdayType: Int
    ) {
//        if(icCard.length!=18){
//            ToastUtils.show("请输入正确的身份证号")
//            return
//        }


        startHttp {
            withContext(Dispatchers.IO) {
                var fileUrl = ""
                if (path.isNullOrEmpty()) {

                } else {
                    val image = async { uploadImage(path) }
                    val await = image.await()

                    fileUrl = await.url
                }
                val upload = async {
                    uploadInfo(
                        name,
                        sex,
                        height,
                        weight,
                        area,
                        urgent,
                        urgentPhone,
                        urgentRelation,
                        fileUrl,
                        province,
                        city,
                        district,
                        lat,
                        lng,
                        medicNames,
                        medicIds,
                        tumour,
                        other,
                        diseaseList,
                        birthDay,
                        birthdayType
                    )
                }

                val await = upload.await() //个人信息上传
                if (await.code != 0) {
                    if (await.code != 0) {
                        CommonToastUtils.showToast(await.msg)
                    }
                } else {
                    setBasic.postValue(await.data)
                }
            }
        }
    }

    private suspend fun uploadInfo(
        name: String,
        sex: String,
        height: String,
        weight: String,
        area: String,
        urgent: String,
        urgentPhone: String,
        urgentRelation: String,
        fileUrl: String = "",
        province: String,
        city: String,
        district: String,
        lat: String,
        lng: String,
        medicNames: String,
        medicIds: String,
        tumour: String,
        other: String,
        diseaseList: String,
        birthDay: String,
        birthdayType: Int
    ): RespBody<NotAccepted> {
        val json = JSONObject()
        json.put("name", name)
        json.put("sex", sex)
//        json.put("idCard",icCard)
        json.put("height", height)
        json.put("weight", weight)
        json.put("area", area)
        //  json.put("medicalHistory",medicNames)
        json.put("historyId", medicIds)
        json.put("latitude", lat)
        json.put("longitude", lng)
        json.put("province", province)
        json.put("city", city)
        json.put("district", district)
        json.put("tumour", tumour)
        json.put("other", other)
        if (diseaseList.isNullOrEmpty()) {
            json.put("diseaseList", "")
        } else {
            val array = JSONArray(diseaseList) //去掉转移的斜杠
            json.put("diseaseList", array)
        }


        json.put("urgent", urgent)
        json.put("urgentPhone", urgentPhone)
        json.put("urgentRelation", urgentRelation)
        json.put("birthday", birthDay)
        json.put("birthdayType", birthdayType)

        if (!fileUrl.isNullOrEmpty()) {
            json.put("avatarUrl", fileUrl)
        }
        val rb = retrofits.getRequestBody(json.toString())
        return apiInterface().setUpdateUser(rb)
    }

    private suspend fun uploadImage(path: String): ImageUploadBean {
        var f = File(path)
        var fileRb =
            MultipartBody.Part.createFormData("file", f.name, retrofits.getMultiPartFileRb(f))
        var body = apiInterface().uploadImage(fileRb)
        return body.data
    }
}