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.*
import okhttp3.MultipartBody
import org.json.JSONArray
import org.json.JSONObject
import java.io.File

class PayCancerVm : BaseVm() {

    val payObservable by lazy { MutableLiveData<RespBody<PayBean>>() }
    val payGoodsObservable by lazy { MutableLiveData<RespBody<PayGoodsBean>>() }
    val uploadSign by lazy { MutableLiveData<RespBody<Any>>() }
    val uploadImage by lazy { MutableLiveData<RespBody<Any>>() }
    val cancerGoodsBeanObservable by lazy { MutableLiveData<RespBody<MutableList<CancerGoodsBean>>>() }
    val saveObservable by lazy { MutableLiveData<RespBody<Any>>() }
    val getBloodObservable by lazy { MutableLiveData<RespBody<PaingBean<CollectBloodBean>>>() }
    val deleteOb by lazy { MutableLiveData<RespBody<Any>>() }
    val deleteImageOb by lazy { MutableLiveData<RespBody<Any>>() }
    val isCanReservationOb by lazy { MutableLiveData<RespBody<String>>() }
    val orderListData by lazy { MutableLiveData<RespBody<PaingBean<CancerGoodsBean>>>() }

    val sugarMedicalListLiveData by lazy { MutableLiveData<RespBody<MutableList<CancerGoodsBean>>>() }

    fun payUrl(goodsId: String?, channelName: String, exchangeCode: String) {
        if (goodsId.isNullOrEmpty()) {
            return
        }
        startHttp {
            var json = JSONObject()
            json.put("goodsId", goodsId)
            json.put("channelName", channelName)
            if (channelName.equals("EXCHANGE_CODE")) {
//                json.put("exchangeCode", exchangeCode)
                json.put("exchangeCode", exchangeCode)
            }
            var response = apiInterface().payUrl(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(payObservable, nowData)
        }
    }

    fun getPayGood() {
        startHttp {
            var json = JSONObject()
            var response = apiInterface().getPayGood(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(payGoodsObservable, nowData)
        }
    }

    fun uploadSign(orderNo: String, signatureUrl: String) {
        startHttp {
            var json = JSONObject()
            json.put("orderNo", orderNo)
            json.put("signatureUrl", signatureUrl)
            var response = apiInterface().uploadSign(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(uploadSign, nowData)
        }
    }

    fun uploadImages(orderNo: String, list: MutableList<String>) {
        startHttp {
            var json = JSONObject()
            json.put("orderNo", orderNo)
            var jsonArray = JSONArray()
            var builder = java.lang.StringBuilder()
            for (s in list) {
                builder.append(s)
                val index = list.indexOf(s)
                if (index != 0 || index != list.size-1){
                    builder.append(",")
                }
            }
            json.put("images",builder.toString())
            var response = apiInterface().uploadImages(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(uploadImage, nowData)
        }

    }


    val imageLive by lazy { MutableLiveData<RespBody<MutableList<String>>>() }
    val imageBeanLive by lazy { MutableLiveData<RespBody<ImageBean>>() }
    fun uploadImagesFile(path: String) {
        if (path.isEmpty()) {
            return
        }
        var f = File(path)
        var rb =
            MultipartBody.Part.createFormData("file", f.name, retrofits.getMultiPartFileRb(f))
        startHttp {
            var body = apiInterface().uploadImageFile(rb)
            body.getCodeStatus(imageBeanLive, nowData)
        }
    }
    fun uploadImagesFile(path: List<String>) {
        if (path.isEmpty()) {
            return
        }
        var list = mutableListOf<MultipartBody.Part>()
        for (s in path) {
            if (s.contains("http")){
                continue
            }
            var f = File(s)
            var rb =
                MultipartBody.Part.createFormData("files", f.name, retrofits.getMultiPartFileRb(f))
            list.add(rb)
        }
        if (list.isEmpty()){
            return
        }
        startHttp {
            var body = apiInterface().uploadImagesFile(list)
            body.getCodeStatus(imageLive, nowData)
        }
    }

    fun getOrderList(userId:String?,pageNum:Int){
        startHttp {
            var json = JSONObject()
            json.put("userId",userId)
            json.put("pageNum",pageNum)
            json.put("pageSize",20)

            var response =
                apiInterface().getOrderList(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(orderListData, nowData)
        }
    }

    fun getSugarMedicineOrderList(pageNum:Int){
        startHttp {
            var json = JSONObject()
            json.put("pageNum",pageNum)
            json.put("pageSize",20)
            var response =
                apiInterface().getSugarMedicineOrderList(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(sugarMedicalListLiveData, nowData)
        }

    }

    fun getEstimateRecord() {
        startHttp {
            var json = JSONObject()

            var response =
                apiInterface().getEstimateRecord(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(cancerGoodsBeanObservable, nowData)
        }
    }
    fun saveInfo(name:String ,status:Int,phone:String) {
        startHttp {
            var json = JSONObject()
            json.put("realName", name)
            json.put("phone", phone)
            json.put("sex", status)
            var response =
                apiInterface().saveInfo(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(saveObservable, nowData)
        }
    }


    fun getBlood(page: Int) {
        startHttp {
            var json = JSONObject()
            json.put("pageNum",page)
            json.put("pageSize", Const.PAGE_SIZE)
            var response =
                apiInterface().getCollectBloodList(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(getBloodObservable, nowData)
        }
    }

    fun deleteImage(images: Long) {
        startHttp {
            var json = JSONObject()
            var response =
                apiInterface().deleteImage(images)
            response.getCodeStatus(deleteImageOb, nowData)
        }
    }

    fun deleteImages(orderNo: String) {
        startHttp {
            var json = JSONObject()
            var response =
                apiInterface().deleteImages(orderNo)
            response.getCodeStatus(deleteOb, nowData)
        }
    }

    fun deleteQuestion(orderNo: String) {
        startHttp {
            var json = JSONObject()
            json.put("orderNo",orderNo)
            var response =
                apiInterface().deleteQuestion(orderNo)
            response.getCodeStatus(deleteOb, nowData)
        }
    }

    fun isCanReservation() {
        startHttp {

            var response =
                apiInterface().isCanReservation()
            response.getCodeStatus(isCanReservationOb, nowData)
        }
    }


}